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
16
votes
2 answers

Reading from file using fgets

I am reading from file of format 1 32 43 23 32 43 123 43 54 243 123 2222 2 Here is my code snippet. string[100]; while(!feof(fp)) fgets(string,100,fp) Now, when I put every string, in the last string I am getting repetition and some more…
Kraken
  • 23,393
  • 37
  • 102
  • 162
16
votes
2 answers

How to read non-ASCII characters from CLI standard input

If I type å in CMD, fgets stop waiting for more input and the loop runs until I press ctrl-c. If I type a "normal" characters like a-z0-9!?() it works as expected. I run the code in CMD under Windows 7 with UTF-8 as charset (chcp 65001), the file is…
Sawny
  • 1,404
  • 2
  • 14
  • 31
16
votes
3 answers

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. Simplified code: #include #include…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
16
votes
1 answer

Return value of fgets()

I have just recently started working with I/O in C. Here is my question - I have a file, from which I read my input. Then I use fgets() to get strings in a buffer which I utilise in some way. Now, what happens if the input is too short for the…
yadav_vi
  • 1,289
  • 4
  • 16
  • 46
16
votes
4 answers

fgets() function in C

I know everybody has told me to use fgets and not gets because of buffer overflow. However, I am a bit confused about the third parameter in fgets(). As I understand it, fgets is dependent on: char * fgets ( char * str, int num, FILE * stream…
emily
  • 161
  • 1
  • 1
  • 3
15
votes
5 answers

How to prevent fgets blocks when file stream has no new data

I have a popen() function which executes tail -f sometextfile. Aslong as there is data in the filestream obviously I can get the data through fgets(). Now, if no new data comes from tail, fgets() hangs. I tried ferror() and feof() to no avail. How…
SinisterDex
  • 811
  • 2
  • 10
  • 13
13
votes
7 answers

Read a line of input faster than fgets?

I'm writing a program where performance is quite important, but not critical. Currently I am reading in text from a FILE* line by line and I use fgets to obtain each line. After using some performance tools, I've found that 20% to 30% of the time my…
dreamlax
  • 93,976
  • 29
  • 161
  • 209
13
votes
5 answers

C fgets versus fgetc for reading line

I need to read a line of text (terminated by a newline) without making assumptions about the length. So I now face to possibilities: Use fgets and check each time if the last character is a newline and continuously append to a buffer Read each…
nc3b
  • 15,562
  • 5
  • 51
  • 63
13
votes
2 answers

Is it possible to distinguish the error returned by fgets

Upon looking at the ISO C11 standard for fgets §7.21.7.2, 3, the return value is stated regarding the synopsis code: #include char *fgets(char* restrict s, int n, FILE* restrict stream); The fgets function returns s if successful. If…
Miket25
  • 1,895
  • 3
  • 15
  • 29
13
votes
2 answers

How can I get a string from input without including a newline using fgets?

I'm trying to write an inputted string elsewhere and do not know how to do away with the new line that appears as part of this string that I acquire with stdin and fgets. char buffer[100]; memset(buffer, 0, 100); fgets(buffer, 100, stdin); …
countofmontecristo
  • 381
  • 3
  • 5
  • 15
13
votes
5 answers

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 char *input;.
Man Person
  • 1,122
  • 8
  • 20
  • 34
12
votes
2 answers

Reaching EOF with fgets

I'm writing a function that perform some authentications actions. I have a file with all the user_id:password:flag couples structured like this: Users.txt user_123:a1b2:0 user_124:a2b1:1 user_125:a2b2:2 This is the code: int main(){ /*...*/ …
Fabio Carello
  • 1,062
  • 3
  • 12
  • 30
11
votes
5 answers

Difference between read() and fgets() in C

I want to read from a stdin stream. Is there any difference in using read() or fgets() to read from the stdin stream. I am attaching the following two pieces of code with fgets and read. With fgets I can use a java program to write and read from…
ssD
  • 339
  • 2
  • 9
  • 22
11
votes
5 answers

Clear input buffer after fgets() in C

#include int main() { char name[10]; for(int i=0;i<=10;i++) { printf("Who are you? "); if(fgets(name,10,stdin)!=NULL) printf("Glad to meet you, %s.\n",name); } return(0); } When I input something greater than 10…
3lokh
  • 891
  • 4
  • 17
  • 39
11
votes
2 answers

Do we need to close the read end of a pipe explicitly whose write end has already been closed?

I have this following scenario. I create a pipe. Forked a child process. Child closes read end of the pipe explicitly and writes into the write end of the pipe and exits without closing anything ( exit should close all open file/pipe descriptors…
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125