Questions tagged [fgetc]

Anything related to C or C++ standard library functions `fgetc` (C) or `std::fgetc` (C++). These functions are used to read a single character from a stream.

Anything related to C or C++ standard library functions fgetc (defined in <stdio.h> C standard header) or std::fgetc (defined in <cstdio> C++ standard header). These functions are used to read a single character from a stream.

See CPPreference.com:

294 questions
3
votes
6 answers

why is while written here with no body?

why is the while loop written in this code with no body? what does it do? The index and length are both integers and I am reading from a txt file. // ignore alphabetical strings too long to be words if (index > LENGTH) { …
A.Emad
  • 321
  • 2
  • 3
  • 9
3
votes
3 answers

fgetc() doesn't stop in EOF

tried to look on forums but could not reslove. I'm trying to read from text. Text is: "To b" But while using fgetc(), EOF is not reached, and at the end I get '\n' and then infinity 'y' samples. Here's my code: Node* getBinTree(FILE *fsrc){ …
noyuzi
  • 41
  • 3
3
votes
1 answer

Counting words in a file in C

I'm writing a function that counts the number of words in a file. Words may be separated by any amount of whitespace characters. There can be integers in a file, but the program should only count words which have at least one alphabetic character.…
caddy-caddy
  • 205
  • 1
  • 5
  • 11
3
votes
1 answer

fgetc is always returning value 1

there is the following function: void readAndPrint(FILE * f) { int c; while(c = fgetc(f) != EOF) { printf("%d", c); } } In the main() body I used the following code to use the above function: FILE * pFile; pFile=fopen…
Prz3m3k
  • 605
  • 6
  • 14
2
votes
1 answer

How does fgetc work internally

Does it actually read character by character or does it read some bytes into the kernel buffer and return to the user- character by character? Is it the same with fgets? Let me say I use glibc and a gcc compiler.
Sharat Chandra
  • 4,434
  • 7
  • 49
  • 66
2
votes
5 answers

Different output content file copy in C

Hello i had a simple copy file program in C but i cant explain why i get different output in the destination file when i use the 2nd method. The correct output with for loop: I am the worst programmer in the world! :D And this is bla bla bla bla …
BugShotGG
  • 5,008
  • 8
  • 47
  • 63
2
votes
1 answer

Using fscanf *after* fgetc issue

In C, there are many posts concerning using fgetc after fscanf, dealing with an additional \n, but I am seeing another issue when using them in the reverse order; fscanf after fgetc. When using fscanf after fgetc, I get a different fscanf-result to…
foam78
  • 264
  • 1
  • 8
2
votes
2 answers

How do I use sscanf to get names and print them out the way i want them to

I'm making a program that takes names from the user seperated by commas. The program allows the user to put as many or as little spaces as they want between the commas. So for example: If I were to type something like Smith, John or Smith,John I…
user798774
  • 403
  • 2
  • 8
  • 20
2
votes
2 answers

How do I store repeated null characters in a string in C from fgetc?

EDIT: This simply doesn't work reliably with strings. I have changed the entire system to work with int arrays. Eliminated a bunch of other headaches, too. The working version of my MVC is: #include #include int main (){ …
2
votes
1 answer

Storing separate characters while looping with fgetc()

So I have been trying to go trough .txt file character by character using fgetc(). I want to store the first and the third character into two seperate variables. This is an example input file. What I want to do in this case is to store the first…
2
votes
1 answer

fgetc doesn't return last line of my file

This is my first time writing C and I am trying to open a file and read its content. I am using fgetc to get the lines of my .txt file, but it is only returning the contents without the last line of my .txt file. How can I ensure fgetc returns the…
Jamie L.
  • 49
  • 2
  • 7
2
votes
2 answers

fopen got some strange behaviors

I have file called denem.txt and has content 123456789 123456789 123456789 When we open a file with mode 'a+b' PHP is supposed to place the pointer to end of the file which means when I try to get a character with fgetc function it should return…
ufucuk
  • 475
  • 9
  • 21
2
votes
1 answer

Issue with reading a file and saving values on structure

This is part of a project that i'm doing. Essentially i want to read a text file called "Circuit" that has this inside: Circuit title example V1 1 0 24 V2 3 0 15 R1 1 2 10000 R2 2 3 8100 R3 2 0 4700 To give you some context, the values represent a…
Real
  • 91
  • 6
2
votes
1 answer

Strange chars while reading from file with fgetc

When I printf the variable answer it contains several strange chars. What might be the reason? int flag=0; char answer[512]; char a[2]; a[1]='\0'; int c; int status=1; do { c = fgetc(pp); if( feof(pp) ) { break ; } …
Tosh
  • 132
  • 1
  • 11
2
votes
1 answer

How to check if a string is in a binary file

i am making a program that gets two binary files, and checks if the second file (string) is in the first file. I tried to use strstr function but it doesnt work. This is that part of my code: Am i reading the files right? fseek(fileToCheckv, 0 ,…
user8097385
  • 109
  • 2
  • 7
1 2
3
19 20