Reading and outputting integers from a file in C -



Reading and outputting integers from a file in C -

i created file content: '12 7 -14 3 -8 10'

i want output numbers of type integer. after compiling , running program, first number '12'

here's code:

#include <stdio.h> main(){ file *f; int x; f=fopen("c:\\users\\emachines\\desktop\\ind\\in.txt", "r"); fscanf(f, "%d", &x); printf("numbers: %d", x); fclose(f); }

what doing wrong?

you scan one integer file using fscanf , print it.you need loop integers.fscanf returns number of input items matched , assigned.in case,fscanf returns 1 on successful scanning. read integers file until fscanf returns 0 this:

#include <stdio.h> int main() // utilize int main { file *f; int x; f=fopen("c:\\users\\emachines\\desktop\\ind\\in.txt", "r"); if(f==null) //if file failed open { printf("opening file failed.exiting..."); homecoming -1; } printf("numbers are:"); while(fscanf(f, "%d", &x)==1) printf("%d ", x); fclose(f); return(0); //main returns int }

c file output fscanf

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -