I need to save lines from a text file into a string and then insert them into a data structure, but with my solution (which I assume is really bad) - I only save words into my line
.
FILE * ifile = fopen("input.txt", "r");
char line[256];
while(fscanf(ifile, "%s\n", line) == 1) {
//inserting "line" into data structure here - no problem with that one
}
It's almost always a bad idea to use the fscanf() function as it can leave your file pointer in an unknown location on failure.
You should use fgets() to get each line.
#define SIZE_LINE 256
FILE *ifile = fopen ("input.txt", "r");
if (ifile != NULL) {
while (fgets (buff, SIZE_LINE, ifile)) {
/* //inserting "line" into data structure here */
}
fclose (ifile);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.