Search code examples
cfilebinaryfileslseek

Store strings in binary file without them being overwritten


So first of all, here is the relevant part of my code and I will then proceed to explain.

scanf("%d", &option);
    fflush (stdin);

    //Insert
    if(option==1){

        printf("enter name\n");
        fgets(name,MAX_SIZE,stdin);
        printf("name: %s",name);

        char str[]="alpha one";
        fd=open(argv[1],O_RDWR|O_CREAT|O_TRUNC,S_IRWXU);
        write(fd,str,strlen(str) + 1);

        pcounter = updateCounter(pcounter, str);

        lseek(fd, 0, SEEK_END);

        char passpos[5];
        sprintf(passpos,"%d",pcounter); //int to string
        fd=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,S_IRWXU);
        write(fd,passpos,3);
    }

The reason I am redefining str as "alpha one" is because, even though I am trying to clear the buffer after scanf with fflush, fgets still gets a "/n" as input (I assume) and when I print "name" I get blank. Any help with this would be valuable even though its not the main issue.

This is all in a loop, so I thought if I keep pressing "1" when prompted, my binary file should look like: alpha onealpha one alpha one ...

However, it looks like: alpha one, no matter how many times I press 1.

Same thing with my other binary file that stores the counter, it gets updated correctly so after the first loop it has "9" stored, but after the second only "18" when I would be expecting: 9 18

I tried removing lseek altogether, and also setting it to CURR instead of END. Same results. I have max warnings turned on and I get none when compiling.

I should point out that this is homework so I cannot use fopen etc, only system commands.

Here is the updateCounter function in case someone wants to run it:

int updateCounter(int pcounter, char *str){
int m,charcount = 0;
for(m=0; str[m]; m++) {
        charcount ++;
}
printf("chars: %d \n", charcount);

pcounter = pcounter + charcount;
printf("pcounter = %d \n", pcounter);

return pcounter;
}

Thanks for any help.


Solution

  • You are opening the file with the O_TRUNC flag. This tells the computer to delete all the data from the file. If you don't want to delete all the data from the file then don't use O_TRUNC.

    Also, make sure to seek to the end of the file before writing any data. Otherwise, you will overwrite the data at the beginning of the file instead. You could use lseek to seek to the end, or you could also use the O_APPEND flag when opening the file to automatically seek to the end.