Questions tagged [fgets]

Anything related to C or C++ standard library functions `fgets` (C) or `std::fgets` (C++). These functions are used to read a sequence of characters from an input stream and to write it into a character buffer as a string.

Anything related to C or C++ standard library functions fgets (defined in <stdio.h> C standard header) or std::fgets (defined in <cstdio> C++ standard header). These functions are used to read a sequence of characters from an input stream and to write it into a character buffer as a string.

See CPPreference.com:

2053 questions
345
votes
15 answers

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)) { fprintf(stderr, "Error reading Name.\n"); …
sfactor
  • 12,592
  • 32
  • 102
  • 152
306
votes
13 answers

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 this warning: (.text+0x34): warning: the `gets' function is dangerous and should not be used. I remember this has something to do with stack protection and security, but…
Vinit Dhatrak
  • 6,814
  • 8
  • 27
  • 30
139
votes
11 answers

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.
Dan
  • 1,407
  • 3
  • 11
  • 4
49
votes
6 answers

How to read from stdin with fgets()?

I've written the following code to read a line from a terminal window, the problem is the code gets stuck in an infinite loop. The line/sentence is of undefined length, therefore I plan to read it in parts into the buffer, then concatenate it to…
robdavies35
  • 567
  • 2
  • 5
  • 6
48
votes
7 answers

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, mostly because of my limited knowledge of how the…
Marko
  • 491
  • 1
  • 5
  • 6
41
votes
6 answers

Difference between scanf() and fgets()

I want to know what is the difference between fgets() and scanf(). I am using C as my platform.
Biswajyoti Das
  • 7,881
  • 11
  • 34
  • 26
37
votes
5 answers

fgets() and fread() - What is the difference?

I understand the differences between fgets() and fgetss() but I don't get the difference between fgets() and fread(), can someone please clarify this subject? Which one is faster? Thanks!
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
25
votes
0 answers

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 dangerous and should not be used. What's a better…
andandandand
  • 21,946
  • 60
  • 170
  • 271
22
votes
7 answers

fgets doesn't work after scanf

#include #include #include void delspace(char *str); int main() { int i, loops; char s1[101], s2[101]; scanf("%d", &loops); while (loops--) { fgets(s1, 101, stdin); fgets(s2, 101,…
Vayn
  • 2,507
  • 4
  • 27
  • 33
22
votes
5 answers

Does fgets() always terminate the char buffer with \0?

Does fgets() always terminate the char buffer with \0 even if EOF is already reached? It looks like it does (it certainly does in the implementation presented in the ANSI K&R book), but I thought I would ask to be sure. I guess this question applies…
Ree
  • 6,061
  • 11
  • 48
  • 50
21
votes
10 answers

PHP - Returning the last line in a file?

I'm guessing it's fgets, but I can't find the specific syntax. I'm trying to read out (in a string I'm thinking is easier) the last line added to a log file.
waxical
  • 3,826
  • 8
  • 45
  • 69
18
votes
4 answers

Difference between fgets and fscanf?

I have a question concerning fgets and fscanf in C. What exactly is the difference between these two? For example: char str[10]; while(fgets(str,10,ptr)) { counter++; ... and the second example: char…
Chris
  • 6,093
  • 11
  • 42
  • 55
18
votes
3 answers

How do I eliminate line break from fgets function in PHP?

I am attempting to make a gallery that calls the image names from a flat file database using the PHP 'fgets' function. There are different sections in the gallery, each with it's own default image, and a small list of images that the users can…
Shreger
  • 183
  • 1
  • 1
  • 5
18
votes
5 answers

fseek() by line, not bytes?

I have a script that parses large files line by line. When it encounters an error that it can't handle, it stops, notifying us of the last line parsed. Is this really the best / only way to seek to a specific line in a file? (fseek() is not usable…
jasonbar
  • 13,333
  • 4
  • 38
  • 46
18
votes
4 answers

the output of ftell in php function

Here is my code:
showkey
  • 482
  • 42
  • 140
  • 295
1
2 3
99 100