-2

I am new to C language and i just started working with files. I have a code which writes some values from an array to a file, and then i want to print everything from the file. However, the fgets doesnt get anything from f. The string s is empty. What am i doing wrong? Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {

    FILE *f;
    if ((f=fopen("fis.txt","r+"))==NULL) { printf ("Error\n");
        exit(1);
    }

    float *v; int n;
    char s[1000];
    scanf("%d",&n);
    v=malloc(n*sizeof(float)); int x;
    for (int i=0;i<n;i++) {
        scanf("%f",&v[i]);
        x=fprintf(f,"%f\n",v[i]);

        if (x<0) perror("Error:");
    }

    fflush(stdin);
    fgets(s,sizeof(s),f); perror("err ");//NO ERROR
    printf("%d",strlen(s));//it's 0
    printf("%s",s);//nothing
    perror("err ");//NO ERROR
    printf("\n");

    free(v);
    fclose(f);
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
Mary Poppins
  • 79
  • 13
  • 1
    Please format your code properly before posting – klutt Dec 27 '18 at 22:47
  • After you finish writing the file, you could just close it and then call fopen again to reopen it. The first call to `fopen` could use "w" as the mode to write a new file, and the second time you could use "r" to read from it. – bruceg Dec 27 '18 at 22:52
  • I am sorry for the format of my code.Thanks – Mary Poppins Dec 27 '18 at 22:58

1 Answers1

1

You are about reading from a file to which you have previously written. Every time when switching between reading and writing, you need to either flush the buffer or use fseek to position the file pointer properly (cf., for example, this SO answer). Note that you are flushing stdin, which does not make sense here (and if it ever makes sense, I'm not sure).

So a call like

fseek(f,0,SEEK_SET)

before your first fgets should solve the problem.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • Thank you. Now i can read from the file. However, the s string does not contain all the values in f, only the first one. I dont know why it cannot read it completely. – Mary Poppins Dec 27 '18 at 22:58
  • 1
    @MaryPoppins Since you do the `scanf/fprintf` in a loop, you'll need to put the subsequent `fgets` in a loop. – Craig Estey Dec 27 '18 at 22:59
  • `fgets` reads until the first new line; you are writing every float in a new line, that's why. You'll need a loop around your `fgets`... – Stephan Lechner Dec 27 '18 at 22:59
  • In my previous question ( https://stackoverflow.com/questions/53944779/how-to-write-string-to-file-in-different-lines-in-c) i was told that " Alternatively, you could just read with fgets, which does include the newline in the string" . I used fgets and i was able to read the entire file, with the '\n' . I am confused.. – Mary Poppins Dec 27 '18 at 23:04
  • If there is only one new line in the file, and if this new line is at the end of the file, then you get the entire file. If you have more new lines, `fgets` will read only until the next one. Clear? – Stephan Lechner Dec 27 '18 at 23:06
  • Yes. Thank you. – Mary Poppins Dec 27 '18 at 23:14