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

scanf() is missing a first letter

I want to use a scanf() function in STM32F401RE_NUCLEO with IAR compiler. This is my overloaded fgetc function. int fgetc(FILE *f) { char ch; while (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) == RESET); HAL_UART_Receive(&UartHandle,…
HoYa
  • 324
  • 3
  • 17
2
votes
1 answer

How to ignore end of line while reading file in C?

I'm a noob in C-Programming so bear me and please correct me if I've the wrong approach. I'm facing problem while reading file character by character in C. As you can see below, apparently, my file has 400 characters but when I try to read them…
Hassan Murtaza
  • 963
  • 6
  • 15
2
votes
4 answers

Trouble with characters in C

Why does this not compile? Cant see the error #include #include #include int main(void) { char *c; FILE *f = fopen("file.txt", "r"); if(f == NULL) { printf("Could not open file"); } …
user265767
  • 559
  • 3
  • 12
  • 27
2
votes
2 answers

fgetc fails to load last character of file

I am trying to load a file into my program so I can work with the bytes on an individual basis, but when I am loading the file it stops loading prematurely; always by 1 character. If there is only one character in the file it does not load it. Is…
2
votes
2 answers

How should I ignore newline from stdin using fgetc?

I have a problem with filling a fixed size 2D array with the fgetc() function. My program should read only '-', '+' or '!' and the chars entered on one line must be equal to size of the columns. Here is the problematic part of my code: for(i = 0; i…
Filip N.
  • 141
  • 1
  • 3
  • 11
2
votes
1 answer

Merging fgetc and putchar in while loop

I am writing a simple code to print the content of the file to stdout. When i use this : while((c=fgetc(fp))!=EOF)putchar(c); It works like it should but i wanna to merge putchar and fgetc. So i wrote while(putchar(fgetc(fp))!=EOF); But it…
Chaker
  • 1,197
  • 9
  • 22
2
votes
2 answers

Counting number of lines in the file in C

I'm writing a function that reads the number of lines in the given line. Some text files may not end with a newline character. int line_count(const char *filename) { int ch = 0; int count = 0; FILE *fileHandle; if ((fileHandle =…
caddy-caddy
  • 205
  • 1
  • 5
  • 11
2
votes
1 answer

C - Getting a Debug Assertion Failed Error by reading a csv-file

i am just doing a program in C, which has to read different files in csv-format. Every file consists of two lines. The first line describes what topics are saved, while the second lines contains the data of the topics. Every file has 6 columns.…
LAN
  • 21
  • 1
2
votes
3 answers

Returning a string [char pointer] from a function

I am writing a program that returns a string from stdin, but i am getting warning that it returns an adress of local wariable. How can i return the string? thanks in advance #include char* readLine() { int i; char input[1024]; …
nocturne
  • 627
  • 3
  • 12
  • 39
2
votes
1 answer

C- Checking if the first character in the first comand line arguement contains a particular char

If the first character of the first argument == "-" then enter the if statement. The error I get is "passing argument 1 of ‘strcmp’ makes pointer from integer without a cast" I have also tried this with fgetc, written a little differently, but still…
JRX
  • 35
  • 1
  • 1
  • 7
2
votes
0 answers

Am I nuts or did I find an issue with C's design?

Ok, so fgetc() returns an unsigned char cast to int, and EOF at... EOF. What if you're trying to read in a config file to a char array, and your implementation's char is signed? The standard for C99 says that not only is the result…
Subsentient
  • 554
  • 3
  • 12
2
votes
1 answer

How to read past EOF from getc?

I am writing a XOR encryption program which works fine during encryption but during decryption the char ca2=fgetc(f); gets stuck at one point and no decryption takes place after that my best guess about the problem is (the encrypted file…
user1232296
  • 217
  • 2
  • 6
2
votes
1 answer

EOF is sent prematurely using "fgetc"

I am writing a program that prints out the Firefox cookies.sqlite file. int printfile(FILE* cookiesfile) { int c; //fseek(cookiesfile,0x18260,SEEK_SET); do{ c=fgetc(cookiesfile); printf("%c",c); }while(c != EOF); …
Daniel Szanto
  • 89
  • 1
  • 9
2
votes
5 answers

Why does this program behave like `tail -f`

int main(int argc, char *argv[]) { int c = EOF; FILE *fp = fopen("./temp.txt", "r"); assert(fp!=NULL); while (1) { c = fgetc(fp); if (EOF != c) { putchar(c); } } return 0; } temp.txt is a…
Grove
  • 21
  • 2
2
votes
2 answers

Almost the same code, but different output, why?

I'm working with UTF-8 encoded text files,and cant find proper solution... after I couldn't solve a problem with string, I'm trying fgetc() now, but it also doesn't work. This code: $file = fopen("t1.txt","r+"); while (! feof ($file)) { $c=…
Hurrem
  • 193
  • 2
  • 4
  • 15