1

I have to recieve user input with this style:

U word word2 word3
U word word2 word3
R word
R word
X

I want to write it to a file and then read what was written but the program is in a endless loop, and the file is being created but its empty, it seems to me the program never leaves the while loop that writes to a file for some reason.

void write_to_file(FILE *fp){
    char buffer[37];
    while(fgets(buffer, 37, stdin)){
        fprintf(fp, "%s", buffer);
    }
    fclose(fp);
}

void read_from_file(FILE *fp){
    char buffer[37];
    char tipo;
    char input_a[6];
    char input_b[26];
    while(fgets(buffer, 37, fp)) {
        sscanf(buffer, "%c %s %[^\n]", &tipo, input_a, input_b);
        switch(tipo) {
            case 'U' :
                // do stuff
                break;
            case 'R' :
                // do stuff
                break;
            case 'X' :
                exit(0);
                break;
            default :
                printf("Invalid Operation\n");
        }
    }
}

int main(){
    FILE *fp = fopen("input.txt","a+");
    write_to_file(fp);
    read_from_file(fp);
    return 0;
}

I opened the file with a+ because in case the file already exists i want to write after the last line

Thanks in advance.

  • When this loop `while(fgets(buffer, 37, stdin))` fails ? – Achal Jun 02 '18 at 11:49
  • @achal that loop should end when there is no more user input, but it doesnt –  Jun 02 '18 at 11:51
  • Thats what I asked. you need to put some condition to break it. How will you inform to `fgets()` about "there is no more user input" ? – Achal Jun 02 '18 at 11:53
  • @achal well 'fgets()' returns null when there isnt anything more to read but even if i do 'fgets(buffer, 37, stdin)!=NULL' the file is still empty for some reason –  Jun 02 '18 at 11:58
  • Thats if you are reading from some text file as if nothing left in the file & it reaches `EOF` it returns NULL. Here your case is different, you are reading from `stdin` stream ? – Achal Jun 02 '18 at 12:01
  • @achal yes i am reading the user input from stdin –  Jun 02 '18 at 12:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172297/discussion-between-achal-and-mdordio). – Achal Jun 02 '18 at 12:05

1 Answers1

0

the program is in a endless loop ? Its because of below while() loop

while(fgets(buffer, 37, stdin)) {
}

As man 3 fgets says

fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read.

you can end this loop by hitting ctrl+d but if you press ctrl+d your main process a.out gets terminated. One way to solve this problem is give one extra \n or ENTER as input at last and then chen the return value of fgets().

Here is the sample code

void write_to_file(FILE *fp){
        char buffer[37], *ptr = NULL;
        while((ptr = fgets(buffer, 37, stdin))!=NULL) { /* It return NULL upon reaching EOF */
                if(*ptr != '\n') { /* at last user enters \n, compare here, if true , break the loop */
                        fprintf(fp, "%s", buffer);
                }
                else
                        break;
        }
        fclose(fp);
}
Achal
  • 11,821
  • 2
  • 15
  • 37