-1

I wrote o program which have to work in inf while loop. In the loop, program have to save some datas to file and close this file. In the loop there is a sleep function which sleep program for 10 seconds. Then I want to check what is in the file but it is empty. If the program run without loop everything is OK

The full code is rather huge so I include a short version

main()
{
    FILE* fp;
    while(1)
    {
        fp=fopen(SAVE_FILE, "awt");
        if(fp==NULL)
        {
            printf("Error while opening the save file \n");
        }
        fprintf(fp, "%s",'this is saved text');
        fclose(fp);
        sleep(10);
    }
}

Have any idea how to close this file correctly to be able to read datas while sleep functiion?

(This is running on linux)

kmbm
  • 115
  • 2
  • 9
  • 2
    `a` and `w` modes are mutually exclusive. Can you remove `w` and recheck? – wRAR Apr 28 '16 at 10:19
  • 2
    Not directly related to your question, but `printf("Error while opening the save file \n");` should be followed by `continue;`., otherwise you'll end up with a `fprintf(fp, ...)` where `fp`is `NULL`. – Jabberwocky Apr 28 '16 at 10:25
  • wRAR it doesn't help – kmbm Apr 28 '16 at 10:25
  • Did you compile withg all warnings enabled ? – Jabberwocky Apr 28 '16 at 10:26
  • 1
    Why do you close this file in every iteration? Just do `fsync` to be sure that data will be in file! – Eddy_Em Apr 28 '16 at 10:26
  • yes i have. But there is no warnings with this – kmbm Apr 28 '16 at 10:27
  • 2
    @Eddy_Em are you sure that `fsync` flushes stdio buffers? You certainly need a `fflush(fp)` before calling `fsync`. – Jabberwocky Apr 28 '16 at 10:28
  • @MichaelWalz, There's better turn off buferisation. As for me, I don't like at all to use in such things buffering I/O. Better `write` than `fwrite`. – Eddy_Em Apr 28 '16 at 10:30
  • 2
    @kmbm `'this is saved text'` between `'` instead of `"` : is this your _actual_ code ? compiling with `-Wall` should produce a warning such as `warning: format ‘%s’ expects argument of type ‘char *’, ` – Jabberwocky Apr 28 '16 at 10:31
  • @Michael Walz I made this mistake when I wrote this code here. In my real code i have " " – kmbm Apr 28 '16 at 10:40
  • @kmbm sigh... so you retype your code here ? Can't you copy/paste it ?? Now that you know it's wrong, you can edit your question. – Jabberwocky Apr 28 '16 at 11:24
  • `"Error while opening the save file \n"` sure looks like an error message. Error messages belong on stderr. That's why it's called "stderr". Try `perror( SAVE_FILE );` – William Pursell Apr 28 '16 at 13:37

1 Answers1

1

Your code must be

int main()
{
    FILE* fp;
    while(1)
    {
        fp=fopen(SAVE_FILE, "at");
        if(fp==NULL)
        {
            printf("Error while opening the save file \n");
        }
        else
        {
           fprintf(fp, "%s\n","this is saved text");
           fclose(fp);
        }
        sleep(10);
    }
}

You wrote the string to be saved between '. You must use " for string literals.

Furthermore you could open file with a option only.

You should always use -Wall option when compiling. gcc shows you a warning for your code.

test.c:662:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
         fprintf(fp, "%s",'this is saved text');
LPs
  • 16,045
  • 8
  • 30
  • 61