0

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.

riverwastaken
  • 285
  • 6
  • 19
  • 1
    Unrelated to your problem, but please not that passing an input-only stream (like e.g. `stdin`) to `fflush` is explicitly mentioned as *undefined behavior* in the C specification. Some compilers allow it as a non-portable extension, but please don't use it. – Some programmer dude Nov 25 '19 at 13:49
  • 1
    By the way, with `write(fd,str,11)` you write `11` characters from a string containing only `10` characters (null-terminator included). Going out of bounds of an array is *always* undefined behavior. Use `sizeof str` to get the actual size of the array, or `strlen(str) + 1`. Or optionally define the array to always be `11` characters large. – Some programmer dude Nov 25 '19 at 13:51
  • @Someprogrammerdude the 11 characters are leftover cause I used to set a different value to str, thanks for pointing it out. As for fflush, what should I use instead? – riverwastaken Nov 25 '19 at 13:53
  • 1
    If you stop using [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)) you won't have such problems with the sizes if you change contents. And instead of `fflush(stdin)` read character by character in a loop until you read a newline. – Some programmer dude Nov 25 '19 at 13:58

1 Answers1

3

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.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • `O_APPEND` is to prefer in most cases, because using `lseek()` is prone to a race condition when two or more processes concurrently write to a file. – Ctx Nov 25 '19 at 14:14