fscanf 匹配一行,但不匹配另一行类似的行
我正在尝试读取文件(特别是 /proc/stat)以从中获取数据。在 C 语言中,有多种方法可以实现此目的,但(到目前为止)我使用的是 fscanf()
。 (但是,我并没有特别依赖它 - 它似乎很适合我想做的事情。如果有另一种更好的方法 - 请提出建议)。我看到的问题是 fscanf() 将读取一行(具有特定的格式字符串),但如果我更改格式字符串以定位另一行,则拒绝收集任何数据。例子使这一点更加清楚。
#include <stdio.h>
char *
get_cpu_perc() {
unsigned long long cpu0_user=0;
FILE* file = fopen("/proc/stat", "r");
int fsf_ret;
fsf_ret = fscanf(file, "cpu %llu", &cpu0_user);
printf("%llu\n", cpu0_user);
printf("%d\n", fsf_ret);
return cpu0_user;
}
int
main(){
get_cpu_perc();
return 1;
}
上面的代码运行良好 - 它选择以“cpu”开头的行中的第一个数字。我想将其分成每个核心的总数 - 这意味着我需要将 fscanf() 调用更改为
fscanf(file, "cpu0 %llu", &cpu0_user);
但是,我在该行上没有任何匹配项。这可能是显而易见的,但我对 C 非常陌生。(这并不是说我不愿意学习,而是我不知道如何真正做到这一点)。
因为这确实不是 *nix 特定的问题,所以下面的几行复制了我在实时 /proc/stat 上运行时看到的行为。如果您愿意的话,可以保存它并对其进行测试。
cpu 5885032 59114 1477054 15427556 39113 0 36078 0 0 0
cpu0 2888239 29861 682033 7814849 22952 0 24266 0 0 0
cpu1 2996792 29253 795020 7612706 16160 0 11812 0 0 0
我的问题是:如何让 fscanf() 与第二行和第三行匹配,而不仅仅是第一行? (这可能与这个问题有关,但老实说 -我什至没有做任何如此花哨的事情,也没有搞乱正则表达式,也许我错过了一些东西?)
I'm attempting to read a file (particularly /proc/stat) to get data out of it. There are a multitude of ways to do this in C, but (so far) I'm using fscanf()
. (However, I'm not particularly tied to it - it just seems to be well suited to what I want to do. If there's another, better way - please suggest it). The problem I'm seeing is that fscanf()
will read the one line (with a particular format string), but refuses to glean any data if I change the format string to target a different line. Examples make this much clearer.
#include <stdio.h>
char *
get_cpu_perc() {
unsigned long long cpu0_user=0;
FILE* file = fopen("/proc/stat", "r");
int fsf_ret;
fsf_ret = fscanf(file, "cpu %llu", &cpu0_user);
printf("%llu\n", cpu0_user);
printf("%d\n", fsf_ret);
return cpu0_user;
}
int
main(){
get_cpu_perc();
return 1;
}
The above works well - it selects the first number on the line starting with 'cpu '. I'd like to split this out into a per-core total - meanining I need to chage the fscanf() call to
fscanf(file, "cpu0 %llu", &cpu0_user);
However, I don't get any match on that line. It's probably obvious, but I'm extremely green when it comes to C. (Which isn't to say I'm unwilling to learn, but rather than I'm uninitiated into how this should really be done).
Since this really isn't a *nix specific problem, below is a few lines that replicate the behavior I see when I run this against a live /proc/stat. You could save this and test against it, if you felt so inclined.
cpu 5885032 59114 1477054 15427556 39113 0 36078 0 0 0
cpu0 2888239 29861 682033 7814849 22952 0 24266 0 0 0
cpu1 2996792 29253 795020 7612706 16160 0 11812 0 0 0
My question is this: how do I get fscanf() to match against the second and third lines and not just the first? (This is possibly related to this question, but honestly - I'm not even doing anything so fancy nor messing with regex's here. Maybe I'm missing something?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以做到:
fscanf(file, "%*s %llu", &cpu0_user)
%*s
告诉 fscanf 读取字符串但不将其分配给任何事物。This should do it:
fscanf(file, "%*s %llu", &cpu0_user)
The
%*s
tells fscanf to read a string but not assign it to anything.fscanf() 不搜索输入文本。如果流开头的文本与格式不匹配,则不会返回任何内容。
您可以在这里使用多种方法。一种可能是使用 fgets() 获取一行,查看前几个字符以确定它是哪一行,然后使用具有正确格式的 fscanf() 。
fscanf() doesn't search the input text. If the text at the beginning of the stream does not match the format, nothing is returned.
There are many approaches you could use here. One might be to use fgets() to get a line, look at the first few characters to determine which line it is, and then use fscanf() with the proper format.