All Questions

151
votes
11answers
92763 views

Why is the gets function so dangerous that it should not be used?

When I try to compile C code that uses the gets() function with GCC, I get a warning: (.text+0x34): warning: the `gets' function is dangerous and should not be used. I remember this has someth...
137
votes
13answers
198263 views

Removing trailing newline character from fgets() input

I am trying to get some data from the user and send it to another function in gcc. The code is something like this. printf("Enter your Name: "); if (!(fgets(Name, sizeof Name, stdin) != NULL)) { ...
7
votes
5answers
8849 views

fgets doesn't work after scanf

#include <stdio.h> #include <string.h> #include <ctype.h> void delspace(char *str); int main() { int i, loops; char s1[101], s2[101]; scanf("%d", &loops); whi...
29
votes
7answers
72983 views

C - scanf() vs gets() vs fgets()

I've been doing a fairly easy program of converting a string of Characters (assuming numbers are entered) to an Integer. After I was done, I noticed some very peculiar "bugs" that I can't answer, mos...
6
votes
6answers
15233 views

strcmp on a line read with fgets

I'm trying to compare two strings. One stored in a file, the other retrieved from the user (stdin). Here is a sample program: int main() { char targetName[50]; fgets(targetName,50,stdin); ...
25
votes
0answers
15894 views

Safe Alternative to gets

I wanna read a whole line from standard input, including the whitespace between two words. When using gets on gcc I get the following message: send.c:(.text+0x2a): warning: the `gets' function is d...
78
votes
10answers
181098 views

Read each line of txt file to new array element

I am trying to read every line of a text file into an array and have each line in a new element. My code so far. <?php $file = fopen("members.txt", "r"); $i = 0; while (!feof($file)) { $line_of_t...
4
votes
2answers
6111 views

fgets instructions gets skipped.Why?

Whenever I do a scanf before a fgets the fgets instruction gets skipped. I have come accross this issue in C++ and I remember I had to had some instrcution that would clear the stdin buffer or somethi...
11
votes
3answers
894 views

Is fgets() returning NULL with a short buffer compliant?

In unit testing a function containing fgets(), came across an unexpected result when the buffer size n < 2. Obviously such a buffer size is foolish, but the test is exploring corner cases. Simpli...
8
votes
5answers
32795 views

fgets() includes the newline at the end

fgets(input,sizeof(input),stdin); if (strcmp(input, "quit") == 0){ exit(-1); } If I type quit, it does not exit the program; I'm wondering why this is the case. By the way input is declared as ch...

Previous Next