0

I have an array of string saved like so:

char clientdata[5][128];
char buffer[256];
... input values into array ...
FILE *f = fopen("client.txt", "w+b");
fwrite(clientdata, sizeof(char), sizeof(clientdata), f);
... read the array from file ...
fclose(f);

Now I want to read the array from the file in that above code. I tried:

fread(clientdata, sizeof(char), sizeof(clientdata), f);

Then I tried to use sprintf on clientdata:

 sprintf(buffer,"%s",clientdata[1]);

this gave me the error :

request for member in clientdata not a structure or union

What did I do wrong?

mugetsu
  • 4,228
  • 9
  • 50
  • 79

1 Answers1

1

As others have noted, you have not provided anything to explain where

request for member in something not a structure or union

would have come from, in your code for example, we do not see buffer defined anywhere... In any case:

Brute force method: (compiles, builds and runs as is in ANSI C)

#include <ansi_c.h>
#define newBinaryFile "C:\\tempExtract\\newbinaryfile.bin"
int main(void)
{
    FILE *fp;
    int i;
    char clientDataNew[5][128] = {"","","","",""};
    char clientdata[5][128] = {  "This string is 128 bytes longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg1\n",
                                 "This string is 128 bytes longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg2\n",
                                 "This string is 128 bytes longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg3\n",
                                 "This string is 128 bytes longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg4\n",
                                 "This string is 128 bytes longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg5\n"};

    fp = fopen (newBinaryFile, "wb");

    fwrite(clientdata, sizeof(char), sizeof(clientdata), fp);
//  for(i=0;i<5;i++)
//  {
//      fputs(clientdata[i], fp);
//  }
    fclose (fp);

    fopen(newBinaryFile, "rb");

    fread(clientDataNew, sizeof(char),sizeof(clientdata),fp);

    for(i=0;i<5;i++)
    {
        (fgets (clientDataNew[i], 128, fp));
    }
    fclose(fp);
    for(i=0;i<5;i++)
    {
        printf("%s", clientDataNew[i]); 
    }
    getchar();
    return 0;   
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • is it not possible to do w+b instead of having to reopen the file? – mugetsu Sep 04 '13 at 18:31
  • Yes - look at ***[fopen(...,"wb")](http://pubs.opengroup.org/onlinepubs/009696799/functions/fopen.html)***. Also ***[fseek](http://pubs.opengroup.org/onlinepubs/009696799/functions/fseek.html)*** may come in handy for resetting the pointer to the stream – ryyker Sep 09 '13 at 22:11