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

Why does ftell skips some positions in the file?

I have a text file named myfile.txt which reads: line 1 l My code: #include int main(){ FILE *f = fopen("myfile.txt","r"); if(f==NULL){ FILE *fp=fopen("myfile.txt","w"); fclose(fp); f =…
4
votes
2 answers

integer variable used with fgetc()

I was trying to understand some basic code and got slightly confused by following code int main () { FILE *fp; int c; int n = 0; fp = fopen("file.txt","r"); if (fp == NULL) { perror("Error in opening file"); …
Tony.H
  • 49
  • 1
  • 5
4
votes
1 answer

Writing my own Cat function in C

Hi i don't know how to simulate my own Cat function in C, i know how it works when no arguments are set and i already get it, but my problem is when i tried to open a file and then print itself... my code until now: #include #include…
Vzqivan
  • 361
  • 3
  • 6
  • 16
4
votes
2 answers

C - reading past end of file with fgetc

I have the weirdest thing happening, and I'm not quite sure why it's happening. Basically what I need to do is use fgetc to get the contents of a simple ASCII file byte by byte. The weird part is it worked, but then I added a few more characters and…
user1777900
  • 975
  • 3
  • 13
  • 27
4
votes
8 answers

Searching for a word in a text using C, and display the info after that word

Say I have a text file like this: User: John Device: 12345 Date: 12/12/12 EDIT: I have my code to successfully search for a word, and display the info after that word. However when I try to edit the code to search for 2 or 3 words and display the…
Dave Wang
  • 109
  • 4
  • 12
4
votes
4 answers

fgetc stops when recognizing new line (\n)

I have this code: while( (cCurrent = fgetc(fp)) != EOF) { } The problem is, when it hits a new line it stops reading. What would be a good way to ignore the newline character? Edit: I'm trying to create a file crypter. It is able to encrypt the…
Lior
  • 5,841
  • 9
  • 32
  • 46
3
votes
4 answers

How do i print escape characters as characters?

I'm trying to print escape characters as characters or strings using this code: while((c = fgetc(fp))!= EOF) { if(c == '\0') { printf(" \0"); } else if(c == '\a') { printf(" \a"); } else if(c == '\b') …
user798774
  • 403
  • 2
  • 8
  • 20
3
votes
2 answers

How do I get fgetc to print all the characters of a file inside of a loop

I'm playing around with fgetc and was wondering how I would get my loop to print the line section once then print the first 16 characters. Here is my code: int main(int argc, char * argv[]) { FILE *fp; if((fp = fopen(argv[1], "rb+")) ==…
user798774
  • 403
  • 2
  • 8
  • 20
3
votes
2 answers

C - Copying text from a file results in unknown characters being copied over as well

When running the following C file, copying the character to fgetc to my tmp pointer results in unknown characters being copied over for some reason. The characters received from fgetc() are the expected characters. However, for some reason when…
3
votes
1 answer

Double parenthesis around expression in While loop

I was attempting to read a file as such: char c; while ((c = fgetc(fp) != EOF)) { ... } And was not stepping into the loop. Why doesn't this code work? I realized the error was failing to place the wrapping parenthesis before the not equals…
socrates
  • 327
  • 2
  • 11
3
votes
3 answers

Trying to read an unknown string length from a file using fgetc()

So yeah, saw many similar questions to this one, but thought to try solving it my way. Getting huge amount of text blocks after running it (it compiles fine). Im trying to get an unknown size of string from a file. Thought about allocating pts at…
Hacktivator
  • 63
  • 3
  • 7
3
votes
3 answers

Reading numbers from a file into an array

I would like to read 3-digit-numbers with spaces inbetween from a file with the fgetc()-command and put them into an array, which is not currently working, as the resulting array has completely different objects in it. What am I doing wrong? (I used…
Chris1024
  • 33
  • 4
3
votes
2 answers

Why does fgetc() return int instead of char?

I would like to copy binary file source to file target. Nothing more! The code is inspired from many examples found on the Internet. #include int main(int argc, char **argv) { FILE *fp1, *fp2; char ch; fp1 =…
geohei
  • 696
  • 4
  • 15
3
votes
2 answers

fgetc not displaying correctly after space

#include #include #include int main() { FILE *userfile, *pwfile, *usernamesPasswords, *readUsernamesPasswords; char u, p, user[20][25], pass[20][20], userLine[25], passLine[20]; int…
John
  • 31
  • 1
3
votes
0 answers

fgetc causes a segfault after running the second time

I have an application that tries to read a specific key file and this can happen multiple times during the program's lifespan. Here is the function for reading the file: __status _read_key_file(const char * file, char ** buffer) { FILE * pFile…
VoltairePunk
  • 275
  • 4
  • 13
1
2
3
19 20