Search code examples
cbinaryfilesfwrite

print multiple strings separated by tokens into binary file using fwrite


I have to save multiple strings that are stored in different variables separated by token to binary file and then read it. I did it with fprintf and everything works (but since it's ASCII it's not what i need) i have to do it with fwrite and i don't know how.

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

token separating the words is | i tried to use struct but it's either wrong or i messed up

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

here's how i wrote the fread and use the saved values

                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;

Solution

  • As you mentioned, you wanted to store it in a binary file, below code is one example. This may not be exact answer for you, but i'm sure will help as reference

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