Search code examples
cfilefgetsfreadscanf

C Read a column of a line from file


I am reading /proc/diskstats (you may or may not know it) but a file none the less, which outputs many lines and some columns/fields. I'm asking if someone can show me how to retrieve the column of a line. for that small piece of data. Thanks.


Solution

  • I've taken a look. the output looks like this:

    8 17 sdb1 15 0 38 28 0 0 0 0 0 28 28

    So we do have a few numbers (seem to be long integers) and we do have a string at the third place.

    Now overall we can read the output line by line. For that fgets is the Standard C choice. You then can probably use sscanf to pick out the different elements. Without having it tested. something like

    sscanf(readline, "%d%s%d....", &var1, & var2 ....
    

    Should do the "trick" of that does not work a combination of you might try to read the line with strtok.

    Hope that gives you an idea.