我对C程序解析为特定的关键字匹配文件的文件解析, 这里是我的示例代码...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;
}
Somefile.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包括类似“你好你好”的值,那么它只是解析“喜”只是,我想要解析x的全部价值而不会损失空间。
请给我建议解决方案。
谢谢。
标准C没有真正为您想要的功能而设计的功能。但是几乎所有的编译器厂商都提供了* do *做你想做的功能。或者,阅读文件并自己进行格式解析。 – 2012-03-21 05:04:10
http://stackoverflow.com/questions/6853789/how-do-i-scan-spaces-into-a-a-string – vrdhn 2012-03-21 05:25:02