c - Program stops while reading input -


i read input following loop

do {       i=0;             {           line[i]=fgetc(stdin);           i++;        }while(i<100 && line[i-1]!='\n' && line[i-1]!=eof);        //parsing input   }while(line[i-1]!=eof); 

my input looks this

$gprmc,123519,a,4510.000,n,01410.000,e,010.0,010.0,120113,003.1,w*4b $gprmc,123520,a,4520.000,n,01650.000,e,010.0,010.0,230394,003.1,w*4b $gprmc,123521,a,4700.000,n,01530.000,e,010.0,010.0,230394,003.1,w*4f $gprmb,a,0.66,l,001,002,4800.24,n,01630.00,e,002.3,052.5,001.0,v*1d $gpgga,123523,5000.000,n,01630.000,e,1,08,0.9,100.0,m,46.9,m,,*68 

so problem after last line, when should read eof, stops on line line[i]=fgetc(stdin);. if copy input file , paste terminal or if run program < input.txt in terminal.but when run in terminal, paste there input , manually add eof (^d) stops.. can tell me problem?

you reading 100 characters char line[]. terminate either 100 characters read in or '\n', or eof; specification of fgets().

so consider using 1 call fgets(), matches code logic. fgets, amounts this:

while(fgets(line, 100, stdin)!=null )  // \n or 100 chars, null return means eof {    char *p=strchr(line, '\n');    if(p!=null) *p=0x0;     // parsing input } // here should check null caused system errors , not eof -- maybe using feof(stdin) 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -