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
6
votes
1 answer

C - fgets segfault

I have the following code: int get_int(void) { char input[10]; fgets(input, 10, stdin); // Segfault here return atoi(input); } It gives me a segfault where marked. I have absolutely no idea what the issue is, because I have the…
6
votes
2 answers

C: Reading a text file (with variable-length lines) line-by-line using fread()/fgets() instead of fgetc() (block I/O vs. character I/O)

Is there a getline function that uses fread (block I/O) instead of fgetc (character I/O)? There's a performance penalty to reading a file character by character via fgetc. We think that to improve performance, we can use block reads via fread in the…
6
votes
4 answers

Reversing array in c - will not print -

The problem with this code is that it does not actually print anything after the user enters in some text in the command line. The purpose of the code is to accept the number of lines the user will enter in via command prompt after the file name.…
pewpew
  • 700
  • 2
  • 9
  • 32
6
votes
3 answers

Reading in a string with spaces in C

I am trying to read in a string that may or may not include spaces ex. "hello world". By doing the following with a number select menu that is inputted by the user. This is just a small replica of what I am trying to do. #include #include…
TheBoxOkay
  • 81
  • 1
  • 8
6
votes
2 answers

How to use fgets properly in a structure?

I can't work out what's the problem with my code. Here's my code: #include #include #define N 20 typedef struct _dog { char dogName[N],ownerName[N]; int dogAge; } Dog; int main() { //Dynamic array int…
2b1c
  • 75
  • 8
6
votes
4 answers

Fgets() keeps skipping first character

This is part of a larger program to emulate the cat command in unix. Right now trying to take input and send it to stdout: char in[1000]; int c = 0; while ((c = getchar()) != EOF) { fgets(in,1000,stdin); fputs(in, stdout); } This sends…
themacexpert
  • 83
  • 2
  • 5
6
votes
2 answers

Troubles with fseek() and reading from file

I have started learning C (via Youtube and K&R) some time ago and I am trying to write some programs as practice. Currently I want to create a program that reads words from a file and compares the beginning of it with the input from the user. The…
Milan Todorovic
  • 155
  • 1
  • 8
6
votes
5 answers

how to read a string from a \n delimited file

I'm trying to read a return delimited file. full of phrases. I'm trying to put each phrase into a string. The problem is that when I try to read the file with fscanf(file,"%50s\n",string); the string only contains one word. when it bumps with a…
maty_nz
  • 280
  • 2
  • 4
  • 16
6
votes
2 answers

PHP fastest method of reading server response

I'm having some real problems with the lag produced by using fgets to grab the server's response to some batch database calls I'm making. I'm sending through a batch of say, 10,000 calls and I've tracked the lag down to fgets causing the hold up in…
Peter John
  • 1,859
  • 4
  • 15
  • 14
5
votes
2 answers

strstr not functioning

Why does this particular piece of code return false on the strstr() if I input "test"? char input[100]; int main() { fgets(input, 100, stdin); printf("%s", input); if(strstr("test message", input)) { printf("strstr…
KWJ2104
  • 1,959
  • 6
  • 38
  • 53
5
votes
4 answers

C scanf() and fgets() problem

I'm trying to read user input and store it as a string including the whitespace. I did a search for a solution and was pointed to fgets() or scanf(%[^\n], str). But both these solutions give me an error. This is what I have: //MAX_CHARACTERS is…
coril
  • 69
  • 1
  • 1
  • 4
5
votes
3 answers

fsockopen connection does not close until timeout

Background: I have to create a plain site that accepts incoming posted XML and sends the XML to a server via a socket connection and in turn display the XML sent back from the server. Easy peasy. Problem: I had no problem utilising fsockopen() to…
vandiedakaf
  • 514
  • 1
  • 6
  • 18
5
votes
1 answer

Is my usage of fgets() and strtok() incorrect for parsing a multi-line input?

I'm writing an implementation of the Moore Voting algorithm for finding the majority element (i.e. the element which occurs more than size/2 times) in an array. The code should return the majority element if it exists or else it should return -1.…
user10648668
5
votes
2 answers

C fgets() how to tell if line is greater than specified size

I am using fgets() to read lines from popen("ps -ev", "r") and I cannot find out how to know if fgets() reads a line partially or fully, and if partially how to read/throw away the excess. When reading each line from popen(), I am reading in the…
dmoini
  • 313
  • 2
  • 15
5
votes
4 answers

Difference between readline vs fread/fgets in php

I have always used readline in my console commands before, but today I've come across the fread and fgets functions and my question is: what is the difference in using these two approaches: // first $inputLine = readline(); // second $inputLine =…
hvertous
  • 1,133
  • 11
  • 25