473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading array of strings from file with char pointer array in C

Question: Is it possible to use a char pointer array ( char *<name>[] ) to read an array of strings from a file in C?

Given: code is written in ANSI C; I know the exact nature of the strings to be read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the amount and size of strings in the array won't be known until run time (in the example I have it in the code explicitly, but in practice the arguments will be taken in though argv)

Basically, I'm dumping the input arguments into a file and rereading them back out.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int binsize(char name[], int size[])
  5. {
  6.     FILE *fp;
  7.     if ((fp = fopen(name, "rb")) == 0) {
  8.         fprintf(stderr, "Unable to open %s", name);
  9.         return 1;
  10.     }
  11.     fread(size, sizeof(size[0]), 1, fp);
  12.     fclose(fp);
  13.     return 0;
  14. }
  15.  
  16. int readbin(char name[], char *strings[])
  17. {
  18.     FILE *fp;
  19.     int i;
  20.     int size[1];
  21.     if ((fp = fopen(name, "rb")) == 0) {
  22.         fprintf(stderr, "Unable to read %s", name);
  23.         return 1;
  24.     }
  25.     fread(size, sizeof(size[0]), 1, fp);
  26.     int stringlengths[size[0]];
  27.     fread(stringlengths, sizeof(stringlengths[0]), size[0], fp);
  28.     for (i = 0; i < size[0]; i++) {
  29.         fread(&strings[i], sizeof(char), stringlengths[i], fp);
  30.     }
  31.     fclose(fp);
  32.     return 0;
  33. }
  34.  
  35. int writebin(char name[],  char *strings[], int numberofstrings)
  36. {
  37.     FILE *fp;
  38.     int i;
  39.     if ((fp = fopen(name, "wb")) == 0) {
  40.         fprintf(stderr, "Unable to write to %s", name);
  41.         return 1;
  42.     }
  43.     fwrite(&numberofstrings, sizeof(numberofstrings), 1, fp);
  44.     int stringlengths[numberofstrings];
  45.     for (i = 0; i < numberofstrings; i++)
  46.         stringlengths[i] = strlen(strings[i]) + 1; /* + 1 because strlen does not include '\0' */
  47.     fwrite(stringlengths, sizeof(stringlengths[0]), numberofstrings, fp);
  48.     for (i = 0; i < numberofstrings; i++)
  49.         fwrite(strings[i], sizeof(char), stringlengths[i], fp);
  50.     fclose(fp);
  51.     return 0;
  52. }
  53.  
  54. int main(int argc, char *argv[])
  55. {
  56.     char *strings[] = {"Howdy", "ADAS", "hjkl"};
  57.     writebin("test.bin", strings, 3);
  58.     int size[1];
  59.     binsize("test.bin", size);
  60.     char *readstrings[size[0]];
  61.     readbin("test.bin", readstrings);
  62.     printf("%s\n", &readstrings[0]);
  63.     system("PAUSE");
  64.     return(0);
  65. }
  66.  
The code above compiles, but it has a few quirks. First the ouput:"HOWDADAShjkl", note the Y is stripped from HOWDY, and the seperate strings are all combined into one big one. If "printf("%s\n", &readstrings[0]);" has the 0 changed to 1, the output is "ADAShjkl". I have checked the output of the fprints and fwrites and they both are coming out fine with 6, 5, 5. Also, note the & needed for string array in printf and fread. They shouldn't need it since they are being passed a pointer, but the program barfs it isn't there.

For example, this works fine.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char *strings[] = {"Howdy", "ADAS", "hjkl"};
  4.     printf("%s\n", strings[0]);
  5.     system("PAUSE");
  6.     return(0);
  7. }
  8.  
I have also tried using a text based file and using fscanf(<filepointer>, "%s", <char *array[]>); and the same situation occurs. I also can't use a multidimensional char array since the functions have to have a fixed number of columns/strings in order for it to compile. Would a pointer to pointer to char array work? Or is the only option to use malloc/calloc and read the whole thing in through a buffer and then scanf it back out (please, no!).
Sep 13 '06 #1
2 22541
Banfa
9,065 Expert Mod 8TB
You can't read data into a char * because a char * is a pointer to data and unless you allocate some data to it you have no-where to store the data you read.

The reason you second example works is because data has been allocated to the pointer in the assignment statement in the DATA segment of the program.

You have you pointers and arrays confused, to start with the simple ones

int size[1];

Appears a lot. There is little point in declaring an array of size 1, you may as well declare

int size;

and use the & operator to get a pointer to it when required

int readbin(char name[], char *strings[]);

This is a slightly archaich way of declaring a function and many people today believe confusing, this declaration is directly equivilent to

int readbin(char *name, char **strings);

leading on to

Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i < size[0]; i++) {
  2.         fread(&strings[i], sizeof(char), stringlengths[i], fp);
  3.  
strings is of type char ** so strings[i] is of type char * and &strings[i] is of type char ** (again). What this call actually does is read the data from the file and place it in the location reserved string[i] which is supposed to be a char * and there is 4 bytes in size.

Since &string[1] = &string[0] + 4

when you read the second string you over write the last 2 bytes of string[0] (y and the zero terminator. On the final write to &string[2] you write off the end of data allocated to string which my system detected and raised an exception.

fread expects a void *, you need to pass type char * (since you are using a char array) and you need to allocate data to that array. You have passed the function an array of char * not allocated to anything, this should fix it

Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i < size[0]; i++) {
  2.         strings[i] = malloc(stringlengths[i]);
  3.         fread(strings[i], sizeof(char), stringlengths[i], fp);
  4. }
  5.  
But note you will need to free the memory once you have finished with it.

You have a similar error where you call printf, you pass &readstrings[0] but readstrings is of type char * array so readstrings[0] is a char * but &readstrings[0] is a char **. You pass a char ** to printf but it is expecting a char *.
Sep 13 '06 #2
Thanks for the help and explanation Banfa. I'm working off of The C Programming Language , so my programming methods might have been a tad bit old, heh.
Sep 13 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Allen Seelye | last post by:
Newbie here. I'm having a hell of a time with this. It takes input from the console into an array and then dumps it into a file. that all is pretty straight forward and works just fine. The output...
4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
9
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it....
1
by: MyCGal | last post by:
Hi, I have this code I wrote to copy any length lines into array of pointers. The problem is after storing the individual lines into the char pointer array, the dispaly() chops off some lines...
2
by: saravanakumar | last post by:
Hi, Can any one explain me the difference between Array of pointers and pointer array? Regards Saravanan
3
by: compileMe | last post by:
I'm new to everything in c++. I have a question concerning reading ip addresses from a text file into a char* array. I have googled this question and every answer alludes my issue. Here's...
4
by: jla1357 | last post by:
In my program I need to read in a file, search for 100 unique words, and count how many times a found word has been found all by using an array. This is what I have so far, but when the file is found...
6
by: scoobydoo666 | last post by:
Hi Could any one tell me how to print size of char pointer array? for example char *ptrArray=new; cout<<sizeof(ptrArray); The above code will print 4 (pointer size) instead of...
1
by: Drake250 | last post by:
I've been trying to play around with reading in an XML file that would look something like this into a 2D array with no luck: <?xml version="1.0" ?> <map index="0" name="Test" cellsx="2" cellsy="2"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.