C, reading a multiline text file -


i know dumb question, how load data multiline text file?

while (!feof(in)) {     fscanf(in,"%s %s %s \n",string1,string2,string3); } 

^^this how load data single line, , works fine. have no clue how load same data second , third lines.

again, realize dumb question.

edit: problem not solved. have no idea how read text file that's not on first line. how this? sorry stupid question.

try like:

/edited/

char line[512]; // or large think these lines  in = fopen ("multilinefile.txt", "rt");  /* open file reading */ /* "rt" means open file reading text */ int cur_line = 0; while(fgets(line, 512, in) != null) {      if (cur_line == 2) { // 3rd line      /* line, 512 chars in.  done if null */      sscanf (line, "%s %s %s \n",string1,string2,string3);      // should store or manipulate strings       break;      }      cur_line++; }  fclose(in);  /* close file */ 

or maybe even...

char line[512]; in = fopen ("multilinefile.txt", "rt");  /* open file reading */ fgets(line, 512, in); // throw out line 1  fgets(line, 512, in); // on line 2 sscanf (line, "%s %s %s \n",string1,string2,string3); // line 2 loaded 'line' // stuff line 2  fgets(line, 512, in); // on line 3 sscanf (line, "%s %s %s \n",string1,string2,string3); // line 3 loaded 'line' // stuff line 3  fclose(in); // close file 

Comments