0

I am learning how to work with files in C. So far I can write (create) txt files using fopen + fprintf function, but I did not understand how the read and write parameter works.

Whenever I use a+, w+ or r+, my program only writes the information, but does not read it. I have to close the file and reopen it in read only mode. The following code explains better:

This code does not work for me:

#include<stdio.h>
#include<stdlib.h>

int main(void){

    FILE * myfile = nullptr;

    myfile = fopen("./program.txt", "a+"); // I also tried w+ and r+

    int num1 = 4;
    int num2 = 0;

    fprintf(myfile, "%d", num1);
    fscanf(myfile, "%d", &num2);  // the atribution does not occur
                                  // num2 keeps previous value
    printf("%d", num2);
    fclose(myfile);

return (0);}

This works fine:

#include<stdio.h>
#include<stdlib.h>

int main(void){

    FILE * myfile = nullptr;

    myfile = fopen("./program.txt", "w");

    int num1 = 4;
    int num2 = 0;

    fprintf(myfile, "%d", num1);
    fclose(myfile);                //close the file!

    myfile = fopen("./program.txt", "r"); // reopen as read only!
    fscanf(myfile, "%d", &num2);
    printf("%d", num2);
    fclose(myfile);

return (0);}

Is there any way to work with a file (read and modify it) without need to close it each time?

user10906383
  • 323
  • 1
  • 8
  • myfile = fopen("./program.txt", "a+"); should work fine (https://stackoverflow.com/questions/5787867/file-read-write-to-same-file) – caxapexac Feb 24 '19 at 04:00
  • 3
    You have to move the cursor back to the start before `fscanf`, i.e. `fseek(myfile, 0, SEEK_SET);` or so. – Henri Menke Feb 24 '19 at 04:00
  • You will be able to edit existing file (Append) something or read/remove somehing – caxapexac Feb 24 '19 at 04:01
  • "Doctor it hurts when I do that". — "Don't do that, then". – n. m. could be an AI Feb 24 '19 at 04:04
  • `nullptr` is not valid C and `int main(void)` is an incorrect signature for `main`. Also `return` is a keyword an not a function so you don't need parentheses, in fact, if you omit `return 0;` at the end of `main`, the compiler will insert it for you. – Henri Menke Feb 24 '19 at 04:04
  • Possible duplicate https://stackoverflow.com/questions/1713819/why-is-fseek-or-fflush-always-required-between-reading-and-writing-in-the-update – n. m. could be an AI Feb 24 '19 at 04:12
  • 1
    @HenriMenke `int main(void)` is a perfectly good signature for `main`. Good catch about nullptr. OP needs to figure out what language they are trying to learn. – n. m. could be an AI Feb 24 '19 at 04:15
  • @HenriMenke `int main(void)` is *a* correct signature for `main`. Unlike `int main()`, which is the endorsed one for C++ but considered obsolescent by C. – Antti Haapala -- Слава Україні Feb 24 '19 at 08:40
  • @n.m. I checked the C standard and you are right. I was under the impression that the signature of `main` was implementation-defined and therefore one was ought to use `int main()` to indicate the unspecified number of argument but apparently that is not so. http://c0x.coding-guidelines.com/5.1.2.2.1.html – Henri Menke Feb 24 '19 at 21:15

1 Answers1

3

When you want to read back in what you just wrote, you have to move the file cursor back to the start (or whatever position you want to start reading at). That is done with fseek.

#include<stdio.h>
#include<stdlib.h>

int main(void) {
    FILE * myfile = NULL;

    myfile = fopen("./program.txt", "a+"); // I also tried w+ and r+

    int num1 = 4;
    int num2 = 0;

    fprintf(myfile, "%d", num1);
    fseek(myfile, 0, SEEK_SET);
    fscanf(myfile, "%d", &num2);  // the atribution does not occur
                                  // num2 keeps previous value
    printf("%d", num2);
    fclose(myfile);
}

Live example on Wandbox

Henri Menke
  • 10,705
  • 1
  • 24
  • 42