io - How to read array saved in binary mode to text file in C -
i have array of string saved so:
char clientdata[5][128]; char buffer[256]; ... input values array ... file *f = fopen("client.txt", "w+b"); fwrite(clientdata, sizeof(char), sizeof(clientdata), f); ... read array file ... fclose(f);
now want read array file in above code. tried:
fread(clientdata, sizeof(char), sizeof(clientdata), f);
then tried use sprintf on clientdata:
sprintf(buffer,"%s",clientdata[1]);
this gave me error :
request member in clientdata not structure or union
what did wrong?
as others have noted, have not provided explain
request member in not structure or union
would have come from, in code example, not see buffer defined anywhere... in case:
brute force method: (compiles, builds , runs 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 128 bytes longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg1\n", "this string 128 bytes longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg2\n", "this string 128 bytes longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg3\n", "this string 128 bytes longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg4\n", "this string 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; }
Comments
Post a Comment