0
#include <stdio.h>

int main(){

    FILE *fp;
    char name[25];
    int i;
    float cgpa;

    fp = fopen("FileExercise.txt", "w");

    if(fp==NULL) printf("File doesn't exist.\n");
    else{
        printf("File has opened.\n");
        for(i=0; i<5; i++){
            printf("Enter roll-%d student name: ", i+1);
            gets(name);
            fputs(name, fp);
            fprintf(fp, "\t%d", i+1);

            printf("Enter students cgpa: ");
            scanf("%f", &cgpa);
            fprintf(fp, "\t%0.2f", cgpa);

            fprintf(fp, "\n");
        }
        printf("File has written successfully.\n");
    }

    return 0;
}

I was trying to to make a file of students. That includes students name, roll & cgpa. For the 1st students it was working. But from the second student I can't take student name as input also can't write student name in file. Now, What should I do?

  • 5
    Never ***ever*** use `gets`. It's so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it has even been removed from the C language (since the C11 standard). Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. Anyone telling you to use `gets` should be considered misinformed or way too old, or suspicious. – Some programmer dude Dec 28 '22 at 05:44
  • 2
    As for your problem, don't generally mix formatted input with `scanf` with `fgets`. One of the function reads the newline added by the `Enter` key, and the other does *not*. – Some programmer dude Dec 28 '22 at 05:45
  • 5
    The general advice is actually to kind of forget that `scanf` exists to begin with. Doing input with it can easily become very complicated. Instead use only `fgets` to read input, then use other functions to parse the input (for example [`strtod`](https://en.cppreference.com/w/c/string/byte/strtof) or `sscanf` (but then remember to check what it [*returns*](https://en.cppreference.com/w/c/io/fscanf#Return_value))). – Some programmer dude Dec 28 '22 at 05:48
  • 3
    Telling the user the file has been written while their data is still in a buffer is not advised... Instead of leaving it to the OS to flush buffers and close files, good code does that itself. Nothing is a problem until it's a problem... – Fe2O3 Dec 28 '22 at 05:59
  • 2
    I agree that for line-based user input, you should always only use the function `fgets` for input, because that function always reads exactly one line of input at once (assuming the the input buffer you supply is sufficiently large to store the entire line). Reading partial lines makes your program unnecessarily complicated and can lead to bugs, as you have just discovered yourself. Once you have the line of input as a string, you can then do with it what you want, for example use the functions `strtod` or `sscanf`, as already stated by someone else. – Andreas Wenzel Dec 28 '22 at 06:09
  • @Someprogrammerdude has already pointed out for gets (deprecated). from man page `Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.` – Hackaholic Dec 28 '22 at 07:12

0 Answers0