How to read in a text file of tab-separated integers in C?

后端 未结 3 1711
小蘑菇
小蘑菇 2021-01-21 03:12

I have a file of simply tab-separated integers (a .txt file) and I wish to read them in with just C, line by line. So, say each line has 5 integers. How can I accomplish this?

3条回答
  •  粉色の甜心
    2021-01-21 03:48

    I'd do something like this:

    int storedVals[MAX_STORED_VALS];
    int bf;
    int ii=0;
    
    while (!feof(fp) && ii

    fscanf automatically does white space trimming. So as long as there's a space in your scan string, it'll get rid of zero or more \t (tabs) and \n (newlines) to find the next integer. Of course, this doesn't do much by way of error correction.

提交回复
热议问题