By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,963 Members | 1,749 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,963 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
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

P: 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

Post your reply

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