0

我保存了一个字符串数组,如下所示:

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);

现在我想从上面代码中的文件中读取数组。我试过了:

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

然后我尝试在clientdata上使用sprintf:

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

这给了我错误:

请求 clientdata 中的成员不是结构或联合

我做错了什么?

4

1 回答 1

1

正如其他人所指出的,您没有提供任何解释

请求非结构或联合的成员

例如,在您的代码中,我们没有看到任何地方定义的缓冲区......无论如何:

蛮力方法:(按 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;   
}
于 2013-09-03T19:58:46.630 回答