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

How to determine number of characters that were read with fgets()?

This is the description of fgets() from the man page: char *fgets(char *s, int size, FILE *stream); ... RETURN VALUE fgets() returns s on success, and NULL on error or when end of file occurs while no characters have been read. It doesn't…
James Ko
  • 32,215
  • 30
  • 128
  • 239
10
votes
3 answers

Reading text-file until EOF using fgets in C

what is the correct way to read a text file until EOF using fgets in C? Now I have this (simplified): char line[100 + 1]; while (fgets(line, sizeof(line), tsin) != NULL) { // tsin is FILE* input ... //doing stuff with line } Specifically I'm…
Johan
  • 863
  • 3
  • 13
  • 28
10
votes
4 answers

How to use fgets if you don't know the number of characters to be read?

I need to read a file and send the text from it to a string so I can parse it. However, the program won't know exactly how long the file is, so what would I do if I wanted to use fgets(), or is there a better alternative? Note: char *fgets(char…
vette982
  • 4,782
  • 8
  • 34
  • 41
10
votes
6 answers

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); char aName[] = "bob"; …
Blackbinary
  • 3,936
  • 18
  • 49
  • 62
10
votes
1 answer

fgets to read line by line in files

I understand that fgets reads until EOF or a newline. I wrote a sample code to read lines from a file and I observed that the fgets gets executed more than the desired number of times. It is a very simple file with exactly two blank lines, ie. I…
codeara
  • 190
  • 1
  • 3
  • 11
10
votes
5 answers

Read file lines backwards (fgets) with php

I have a txt file that I want to read backwards, currently I'm using this: $fh = fopen('myfile.txt','r'); while ($line = fgets($fh)) { echo $line."
"; } This outputs all the lines in my file. I want to read the lines from bottom to top. …
Nir Tzezana
  • 2,275
  • 3
  • 33
  • 56
9
votes
1 answer

Use input (stdin) in debug console VScode

I try to debug some code in vs code. Everything works fine, but when I trying input something in console nothing happens. my code: #include #define SIZE 20 int main() { char arr[SIZE]; fgets(arr, SIZE, stdin); …
senoron
  • 315
  • 1
  • 2
  • 11
9
votes
5 answers

How do I read white space using scanf in c?

Problem: I need to be able identify when two whitespaces occur consecutively. I have read the following questions: how to read a string from a \n delimited file how to read scanf with spaces And I am aware of scanf problems:…
MykC
  • 188
  • 1
  • 3
  • 13
9
votes
5 answers

In Perl, can I limit the length of a line as I read it in from a file (like fgets)?

I'm trying to write a piece of code that reads a file line by line and stores each line, up to a certain amount of input data. I want to guard against the end-user being evil and putting something like a gig of data on one line in addition to…
NG.
  • 22,560
  • 5
  • 55
  • 61
9
votes
1 answer

C child read giving "resource temporarily unavailable"

So I have a file stream from a parent process to a child - and most of the time it works fine. However, when reading from it multiple times quickly, using fgets() will return NULL and the error is set to "resource temporarily unavailable". The…
Gary
  • 926
  • 3
  • 12
  • 24
9
votes
5 answers

C++: Store read binary file into buffer

I'm trying to read a binary file and store it in a buffer. The problem is, that in the binary file are multiple null-terminated characters, but they are not at the end, instead they are before other binary text, so if I store the text after the '\0'…
schacker22
  • 439
  • 2
  • 5
  • 12
9
votes
9 answers

Use of undefined constant STDIN - assumed 'STDIN' in C:\wamp\www\study\sayHello.php on line 5

I want to Learn php & mySQL and I purchased a book (php&mySql: the missing manuals 2edition) I installed Wampserver2.4 on win8 64bit machine. Server Configuration Apache Version : 2.4.4 PHP Version : 5.4.12 in first lesson i got this error…
user3206343
  • 93
  • 1
  • 1
  • 3
9
votes
7 answers

Can C's fgets be coaxed to work with a string *not* from a file?

Specifically, the code sample here works great, but only when the string is stored in a file. Sometimes I need it to process a generated string (stored in a string variable), but I'm having trouble convincing fgets's third parameter to work with…
Monte Hurd
  • 4,349
  • 5
  • 30
  • 35
9
votes
3 answers

carriage return by fgets

I am running the following code: #include #include #include int main(){ FILE *fp; if((fp=fopen("test.txt","r"))==NULL){ printf("File can't be read\n"); exit(1); } char str[50]; …
Vaibhav Agarwal
  • 1,124
  • 4
  • 16
  • 25
8
votes
5 answers

How to fgets() a specific line from a file in C?

So, I'm trying to find a way to fgets() a specific line in a text file in C, to copy the contents of the line into a more permanent buffer: Essentially, I was wondering if there was a way to do that without something similar to the following…
gfppaste
  • 1,111
  • 4
  • 18
  • 48