我正在解析 C 程序匹配特定关键字的文件,这是我的示例代码...
#include <stdio.h>
int main() {
FILE *infile = fopen("Somefile.txt", "r");
char buffer[256];
char value[128];
while (fgets(buffer, sizeof(buffer), infile))
if (1 == sscanf(buffer, " x = %s", value))
printf("Value = %s\n", value);
return 0;
}
一些文件.txt
some junk
#a comment
a = 1 ; a couple other variables.
b = 2
x = 3
x = 4
x=5
x = Hi hello
输出:
Value = 3
Value = 4
Value = 5
Value = Hi
问题:当 x 包含像“Hi Hello”这样的值时,它只解析“Hi”,我想解析 x 的整个值而不丢失空间。
请建议我解决方案。
谢谢。