简体   繁体   中英

How to read data from a txt file?

I have a .txt file with "./a.out,file.txt,./a.out2,file2.txt"

How can I store "./a.out", "file.txt", "./a.out" and "file2.txt"?

I was trying to use fgets() and sscanf().

while (fgets(buffer, 80, fp) != NULL) {
    sscanf(line, "%s, %s, %s, %s\n", a, b, c, d);
    printf("a = %s\n", a);
    printf("b = %s\n", b);
    printf("c = %s\n", c);
    printf("d = %s\n", d);
}

[but if i print them out, i will read the whole inline into char* a]

Please look at the following question for how to read in a file to a string: How to read the content of a file to a string in C? . Then you can use strntok() with the delimiter ",".

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM