file io - Why we always have to use fgetc command in C programming instead of fscanf which do the same thing but prints strange results? -


in c programming whenever use fgetc(file) read chars until end of file works. when use similar fscanf(file, "%c") function prints strange characters. code:

#include <stdio.h> #include <stdlib.h>  int main() {     char c;     file * file = fopen("d\\filename.txt", "r");     while (c != eof) {         fscanf(file, "%c", &c);         printf("%c", c);     }     return 0; } 

but when use fgetc instead of fscanf, works. , prints each character present in file.

can answer why works this?

notice

c=fscanf(file,"%c"); 

is undefined behavior (here explaining why should afraid of it, when program seems apparently "work"), , every c compiler (e.g. gcc invoked gcc -wall -wextra -g) should warn (if enable warnings). when coding in c should learn how use debugger (e.g. gdb).

you should read documentation of fscanf(3). want code

char c= '\0'; if (fscanf(file, "%c", &c) <= 0) break; 

you'll better take habit of initializing every variable; optimizing compiler remove initialization if useless, , warn unitialized variables otherwise.

notice using fgetc(3) in case preferable. need declare c integer, not character, , code:

do {   int c=fgetc(file);   if (c==eof) break; } while (!feof(file)); 

notice in above loop feof(file) never true (because fgetc have given eof before), you'll better replace while(!feof(file)) while(true)

it simpler read (by other developers, or in couple of months) working on same code, , faster. implementations of fscanf based somehow on fgetc or related thing.

also, take habit of testing input. input file might not expect.

on recent systems, encoding today utf-8. aware (human language) characters encoded in several bytes (e.g. french accentuated e letter é, or russian yery letterЫ, or euro sign , or mathematical for all sign , letters or glyphs in other languages, etc....). should consider using utf-8 library (e.g. libunistring) if care (and should care utf-8 in serious software!).

nota bene: if young , learning programming, better (imnsho) learn scheme sicp, using e.g. racket before learning c or java. c not beginners imho.

ps character type (often byte) char in lower cases.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -