PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 31, 2022

[FIXED] How do you detect ctrl+D in C?

 October 31, 2022     c, eof, fgets, segmentation-fault, stdin     No comments   

Issue

So I read in a line using fgets

line = fgets(l, BUFSIZ, stdin);

And I from what I understand control+d is EOF so I tried

if(line[0] == EOF)
     continue;

to get back to the top of the loop. But this led to segfaults... Is there another way?


Solution

Since the machine generates EOF on Ctrl + D, you should be checking fgets() for NULL, as fgets() is obliged to return NULL on end of file.

line = fgets(l, BUFFSIZ, stdin)
if (line == NULL)
    continue;

In your code, you are trying to dereference a pointer that's NULL leading to segfault.



Answered By - Arjun Sreedharan
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing