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

Why does alarm() cause fgets() to stop waiting?

I am playing around with signals in C. My main function basically asks for some input using fgets(name, 30, stdin), and then sits there and waits. I set an alarm with alarm(3), and I reassigned SIGALRM to call a function myalarm that simply calls…
Aaron Parisi
  • 464
  • 7
  • 15
8
votes
4 answers

C : Best way to go to a known line of a file

I have a file in which I'd like to iterate without processing in any sort the current line. What I am looking for is the best way to go to a determined line of a text file. For example, storing the current line into a variable seems useless until I…
Badda
  • 1,329
  • 2
  • 15
  • 40
8
votes
4 answers

Fscanf or Fgets? Reading a file line after line

i have to write a program in C to read a file containing several line of text, each line contains two variables: a number (%f) and a string: EX: file.txt ============ 24.0 Torino 26.0 Milano 27.2 Milano 26.0 Torino 28.0 Torino 29.4 Milano There is…
Lc0rE
  • 2,236
  • 8
  • 26
  • 34
8
votes
2 answers

Reading data from fsockopen using fgets/fread hangs

Here is the code that I am using: if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15))) echo "Could not connect to host"; $server_response = fread($fp, 256); echo $server_response; fwrite($fp, "C01…
victor_golf
  • 195
  • 2
  • 2
  • 10
7
votes
2 answers

Delete first X lines from a file PHP

I was wondering if anyone out there knew how this could be done in PHP. I am running a script that involves opening a file, taking the first 1000 lines, doing some stuff with those lines, then the php file opens another instance of itself to take…
Eric Strom
  • 715
  • 9
  • 20
7
votes
6 answers

Open file and read from file Objective-c

I'm trying to open a file, and read from it.. but I'm having some issues. FILE *libFile = fopen("/Users/pineapple/Desktop/finalproj/test242.txt","r"); char wah[200]; fgets(wah, 200, libFile); printf("%s test\n", wah); this prints: \377\376N test …
user503707
7
votes
3 answers

Reading and writing to a file at the same time in C

Supposed to swap every two lines in a file until just one line remains or all lines are exhausted. I don't want to use another file in doing so. Here's my code: #include int main() { FILE *fp = fopen("this.txt", "r+"); int i = 0; …
ps_
  • 97
  • 2
  • 12
7
votes
3 answers

Replacing gets() with fgets()

I've been testing this struct out and I get warning about using gets. Somebody mentioned to use fgets instead and replace the end with '\0'. Any recommendation how I can change my code to do that? void regCars(Car reg[], int *pNrOfCars) { …
xxFlashxx
  • 261
  • 2
  • 13
7
votes
2 answers

Automatic space after fgets' result?

it seems as if fgets puts a space after everything it returns. Here's some example code: "; $FileHandle = @Fopen($_SERVER{'DOCUMENT_ROOT'} . "/file.txt", "r"); If…
Chris
  • 2,905
  • 5
  • 29
  • 30
7
votes
5 answers

How to read and overwrite text file in C?

I have a text file text.txt that reads (for simplicity purposes) this is line one this is line two this is line three Again for simplicity's sake, I am just trying to set the first character in each line to 'x', so my desired result would be xhis…
Corey
  • 115
  • 1
  • 1
  • 6
7
votes
3 answers

Using fgets() with char* type

I have a simple question about using fgets() with char* string. .... char *temp; FILE fp=fopen("test.txt", "r"); fgets(temp, 500, fp); printf("%s", temp); .... This code didn't work well. But after I modified char *temp to char temp[100];, the…
user3033077
  • 69
  • 1
  • 1
  • 4
7
votes
5 answers

Mimic Python's strip() function in C

I started on a little toy project in C lately and have been scratching my head over the best way to mimic the strip() functionality that is part of the python string objects. Reading around for fscanf or sscanf says that the string is processed…
sudharsh
  • 187
  • 2
  • 2
  • 10
6
votes
4 answers

How to detect empty string from fgets

I'm trying to detect the input of fgets from stdin as empty (when I press enter without entering in anything). Here is my program: int main() { char input[1000]; printf("Enter :"); fgets(input, 1000, stdin); input[strlen(input) - 1]…
KWJ2104
  • 1,959
  • 6
  • 38
  • 53
6
votes
5 answers

equivalent of fgets on a buffer?

I was originally parsing a file line by line using fgets(). Now I changed things so that I already have my entire file in a buffer. I still like to read that buffer line by line for parsing purposes. Is there something designed for this, or do I…
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
6
votes
5 answers

base64 encoded string gets truncated through fgets call while parsing IMAP

I am parsing emails with Zend_Mail, and strangely some content gets truncated without an obvious reason and malforms the email parts. For example Content-Disposition: attachment;…
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114