0

I am trying to read from stdin with fgets which is an alternate way to read apart from a file. The thought is that I should be able to write as many lines as I want and stop reading from stdin with CTRL+C is entered or any other bash terminating commands.

However the issue is that if I write a line, for instance:

mr:x:1171:1101:Mikael Rännar:/Home/staff/mr:/usr/local/bin/tcsh

and then I press ENTER to submit it and thereafter CTRL+C then it counts the line I'm terminating at the same as previous one. Here are a few examples:

mr:x:1171:1101:Mikael Rännar:/Home/staff/mr:/usr/local/bin/tcsh
<BLANKLINE>
*I PRESSED CTRL+C HERE*
Output:
Line 2: Encountered a <BLANKLINE>
Line 3: Encountered a <BLANKLINE>
1171:mr

Example 2:

mr:x:1171:1101:Mikael Rännar:/Home/staff/mr:/usr/local/bin/tcsh 
*I PRESSED CTRL+C HERE*
Output:
1171:mr
1171:mr

So my question is, how do I stop the program from copying the previous line when a terminating command is sent (CTRL+C)?

EDIT: This is my code I use to read from the stream

void readFile(FILE *fp, list *userList) {
    char line[1025];
    int lineNumber = 1;
    while (!feof(fp)) {
        user *newUser = malloc(sizeof(user));
        initializeStruct(newUser);
        fgets(line, 1025, fp);
        char *p = memchr(line, '\0', 1024);

        if (p == NULL) {
            fprintf(stderr, "Line %d: Line is too long!\n", lineNumber);
        } else {
            newUser->lineNumber = lineNumber;
            parseLine(line, newUser);
            list_insert(userList, newUser);
            lineNumber++;
        }
    }
}

When CTRL+D is pressed in bash, the fgets command uses the previous line entered.

Thanks

Kashif Faraz Shamsi
  • 513
  • 1
  • 7
  • 21
Cows42
  • 307
  • 3
  • 12
  • Show your code. It seems you have mistake in treating whitespace/unprintables. Try for an [mcve] and state the exact input which provokes the problem. – Yunnosch Sep 03 '17 at 11:05
  • http://www.cplusplus.com/reference/cstdio/fgets/ – zindarod Sep 03 '17 at 11:08
  • @Yunnosch I've added my read code in main post – Cows42 Sep 03 '17 at 11:11
  • @Zindarod Thank you, found the solution. "If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged)." All I have to edit in the code is fgets(line, 1025, fp); and make it if (fgets(line, 1025, fp) != NULL) – Cows42 Sep 03 '17 at 11:16
  • Cows42, you can make your own answer. Or you, @Zindarod, can make one with a little explanation. If you do not want to answer an off-topic or duplicate question (good style), then please flag accordingly. – Yunnosch Sep 03 '17 at 11:19
  • https://stackoverflow.com/q/5431941/905902 – wildplasser Sep 03 '17 at 11:21
  • Cows42, great that you found the problem. Documentation is your best friend, will save you a lot of headache in the future. @Yunnosch This question already has many answers in SO, as wildplasser pointed out. – zindarod Sep 03 '17 at 11:24
  • @AnttiHaapala Yeah I realized that. I use Windows but I have bash installed as well. – Cows42 Sep 03 '17 at 15:37

0 Answers0