【问题标题】:print multiple strings separated by tokens into binary file using fwrite使用 fwrite 将多个由标记分隔的字符串打印到二进制文件中
【发布时间】:2018-05-23 19:35:10
【问题描述】:

我必须将存储在不同变量中的多个字符串保存到二进制文件中,然后读取它。我用 fprintf 做的,一切正常(但因为它是 ASCII,所以不是我需要的)我必须用 fwrite 做,我不知道怎么做。

fprintf(f,"%s|%s|%d|%d|", guessWord, currentWord, wordlength, errors);

分隔单词的标记是 | 我尝试使用 struct 但它要么错误,要么我搞砸了

struct savetofile
{
    char sguessWord[20];
    char filler;
    char scurrentWord[20];
    char filler2;
    int swordlength;
    char filler3;
    int serrors;
    char filler4;
};

struct savetofile savetofile_1 = {guessWord,'|',currentWord,'|',wordlength,'|',errors,'|'};
fwrite(&savetofile_1,1,sizeof(savetofile_1),f);

这是我如何编写 fread 并使用保存的值

                n2 = fread(c2, 1, 50000, f);
                c2[n2] = '\0';
                char *token = strtok(c2, "|");
                char *words[200] = {0};
                int i = 0;
                while(token != NULL)
                {
                    words[i] = malloc(strlen(token)+1);
                    strcpy(words[i],token);
                    token = strtok(NULL, "|");
                    i++;
                }
                strcpy(guessWord, words[0]);
                strcpy(currentWord, words[1]);
                int temp;
                temp = atoi(words[2]);
                wordlength = temp;
                int temp2;
                temp2 = atoi(words[3]);
                errors = temp2;

【问题讨论】:

  • 您的结构将由编译器插入填充以加快访问速度。手动考虑fwriteing struct 的各个成员。用\n 分隔它们可能是一个很好的选择,以便使用getline 轻松检索,或者您可以使用scanf/sscanf(sscanf 接受格式化输入)。

标签: c binaryfiles fwrite


【解决方案1】:

正如您所提到的,您想将其存储在二进制文件中,下面的代码就是一个示例。这对您来说可能不是确切的答案,但我相信会有所帮助

int main ()
{
    char guessWord[20] = "abcd";
    char currentWord[20] = "qwert";

    FILE *f =NULL;

    struct savetofile savetofile_1 , savetofile_2;

    memset(&savetofile_1,0, sizeof(savetofile_1)); //initializes the memory location 0
    memset(&savetofile_2,0, sizeof(savetofile_2));

    strcpy(savetofile_1.sguessWord,   guessWord );
    strcpy(savetofile_1.scurrentWord,   currentWord );
    savetofile_1.swordlength =20;
    savetofile_1.serrors = 3;
    savetofile_1.filler ='|';
    savetofile_1.filler2 ='|';
    savetofile_1.filler3 ='|';
    savetofile_1.filler4 ='|';

    f = fopen( "file.bin" , "wb" );                 //Creates in binary format, Also note file name *.bin

    if(f != NULL)
    {
        fwrite(&savetofile_1,1,sizeof(savetofile_1),f);
        fclose(f);                                  //Saves the file
    }


    f = fopen( "file.bin" , "rb" );                 //Opens in binary readmode

    if(f != NULL)
    {
        fread(&savetofile_2, sizeof(savetofile_2), 1, f);  //Reading the structure as is
        fclose(f);
    }

    printf(" %s -- %s -- %d -- %d \n", savetofile_2.scurrentWord,savetofile_2.sguessWord,
                            savetofile_2.swordlength, savetofile_2.serrors);

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2021-11-29
    • 2019-01-20
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 2016-08-03
    相关资源
    最近更新 更多