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

Searching for strings that are NULL terminated within a file where they are not NULL terminated

I am writing a program that opens two files for reading: the first file contains 20 names which I store in an array of the form Names[0] = John\0. The second file is a large text file that contains many occurences of each of the 20 names. I need my…
KOB
  • 4,084
  • 9
  • 44
  • 88
0
votes
1 answer

find number of lines with length more than 5

Let's assume I've got a file a ab abc abcd abcde abcdef abcdefg abcdefgh abcdefgh abcdefg abcdef abcde abcd abc ab a I need to find lines with length >= 5. Here is the code int cOriginal(char *fileName) { FILE *file = fopen(fileName, "r"); …
lapots
  • 12,553
  • 32
  • 121
  • 242
0
votes
2 answers

Why fgets() works wrong?

Here's my code if(passwordCorretta(ds_sock)){ printf("CLIENT: password aggiungi corretta\n"); do{ printf("Cognome >> "); fgets(cognome,sizeof(cognome),stdin); //scanf("%s", cognome); printf("Nome >> "); …
Greg917
  • 13
  • 4
0
votes
2 answers

strcmp not working in while loop in c

Could somebody help me with this problem? I'm trying to fill a vector with number. The problem (I think) is the condition in there. It is supposed to get out of the while loop when I input an "f" but it doesn't work, although it hasn't got the top…
user2461687
  • 173
  • 1
  • 13
0
votes
1 answer

How to read -1 char from stdin?

I am really desperate trying to figure out how can I read char with value -1/255 because for most functions this means EOF. For example if I enter all characters from extended ASCII from low to high (decimal value) I end up with -1/255 which is EOF…
Peter
  • 449
  • 1
  • 3
  • 13
0
votes
1 answer

C Programming - fgets() infinite while loop

I am having an issue with taking in an augmented matrix from a text file. The program will simply segfault after successfully reading in all of the data from the text file, most likely because it is still looking for another token to reach or…
0
votes
1 answer

C issue in which a while(fgets) loop inside another while loop is only executed once

This is for a college introductory CS course I am taking. I have been stuck on this problem for a few days, and our department resources have been swamped with other students on this assignment. Searching has not been much help, but I'm not sure I'm…
Juggerbot
  • 103
  • 4
0
votes
1 answer

fgets() causing segmentation fault when reading file

I'm trying to use fgets() to read text from a file and I keep getting a segmentation fault. The program reads in the entire file and then after it reads the last line it crashes. Any help would be appreciated. #include #include…
0
votes
1 answer

C file reading doesn't read an array of numbers in as string

None of fgets() and fscanf() works, the file.txt has 100 6 0060001 6 4321298 3 5001 6 0604008 6 0102111 My code reads the first integer pretty well but the not the 7 digit number? Any help? int main(void) { int numTotal = 0; int maxShy =…
TPWang
  • 1,322
  • 4
  • 20
  • 39
0
votes
1 answer

How can I exit the while loop in queue?

How can I exit a while loop? I tried NULL, '\0' and '\n' but nothing works. I am trying to exit the loop if user types empty line or null character. I used == NULL but it doesn't go into the loop. How should I exit the fgets loop? Code typedef…
riar
  • 7
  • 4
0
votes
1 answer

fgets to include text after newline

I want to make an array of characters and store the whole input in it(even if the input contains "\n" at some point. When i use: char vhod[50]; fgets(vhod, sizeof(vhod), stdin); it stores the input UNTIL the first newline. How to get the whole…
Denko Mancheski
  • 2,709
  • 2
  • 18
  • 26
0
votes
2 answers

What's the proper way to safely discard stdin characters of variable length in C?

I am working on some example code in a C text called "Headfirst C". I wrote a practice application that demonstrates signal handling and after finishing the chapter decided to play around with it a bit. I'm an engineer used to working in LabVIEW…
Kin3TiX
  • 708
  • 1
  • 5
  • 18
0
votes
1 answer

using fork function after getting user's input

How do you get the fork function to work after getting the user's input from the fgets() function and getting tokens from the user's input? My code: #include #include #include #include #include…
user4728257
  • 25
  • 10
0
votes
2 answers

two consecutive fgets seg faults

I am currently trying to read in two strings s and t that will be input to stdio. They will be input on separate lines. The following code segfaults. #include #include #include int main() { char t[5000000]; …
Essam
  • 157
  • 1
  • 2
  • 12
0
votes
2 answers

How do I read in a string pass the newline character?

I have to read in a file such as apple grape banana And store it into a string, but fgets only reads up to the newline and stops, so its only reading in apple. How do I get around this? Or how can I store the three words all as separate strings? …
Carson
  • 1,147
  • 5
  • 19
  • 41
1 2 3
99
100