c++ - How to read a string file from second line in C? -
this code reads characters in file , calculates length of characters. how can read second line , ignore read first line?
this part of code:
int lena = 0; file * filea; char holder; char *seqa=null; char *temp=null; filea=fopen("d:\\str1.fa", "r"); if(filea == null) { perror ("error opening 'str1.fa'\n"); exit(exit_failure); } while((holder=fgetc(filea)) != eof) { lena++; temp=(char*)realloc(seqa,lena*sizeof(char)); if (temp!=null) { seqa=temp; seqa[lena-1]=holder; } else { free (seqa); puts ("error (re)allocating memory"); exit (1); } } cout<<"length seqa is: "<<lena<<endl; fclose(filea);
make counter of how many \n
have seen,and when ==1
goto read 2nd line.
int line=0; while((holder=fgetc(filea)) != eof) { if(holder == '\n') line++; if(holder == 1) break; /* 1 because count start 0,you know */ } if(holder == eof) { //error:there's no 2nd } while((holder=fgetc(filea)) != eof) { // holder contents begging 2nd line }
you can make more simple using fgets()
:
make 1 call , ignore it(by don't discard result-value,for error-checking);
make second call, , begging reading this.
note: i'm considering c language here.
Comments
Post a Comment