C语言中的Sscanf或Strtok文件解析
我必须从不同的文件中读取大量的纯文本数据行,格式如下:C语言中的Sscanf或Strtok文件解析,c,file-io,C,File Io,我必须从不同的文件中读取大量的纯文本数据行,格式如下: Variable:Value //Format 1 Variable2:Value .... //Numerous lines of any of the three formats Variable0 Variable1 Variable2:Value //Format 2 ... STATIC*:a b c d //Format 3 格式1看似微不足道,但由于格式2,单独处理可能
Variable:Value //Format 1
Variable2:Value
.... //Numerous lines of any of the three formats
Variable0 Variable1 Variable2:Value //Format 2
...
STATIC*:a b c d //Format 3
格式1看似微不足道,但由于格式2,单独处理可能毫无意义,因为格式2可以在冒号之前有任意长度的变量。实际上,Format1只是format2只有一个变量的情况
格式2的最小形式将类似于格式1。格式2在最终变量/值对之前可以有任意数量的文本“变量”。
在上面的示例中,Variable0理想情况下会指向Variable1,Variable1会指向Variable2及其值:value
格式3将有一个静态字符串,在该静态字符串后面有一个标识符(由*表示)。后跟四个整数值
我有数百个文件包含这三种格式的数据。我现在正试图弄清楚如何用C高效地读取数据(因为我被要求使用C) Strtok似乎让我失望,因为如果我只是用空格分隔每一行,格式2就会产生问题 Sscanf似乎更为理想,因为我可以指定某种类型的格式来帮助处理格式1和格式3,但在处理格式2时似乎仍然不够 甚至有可能让其中一个对所有三个都有效吗?如果不是,我可能忽略了一个更理想的函数是什么 谢谢。分线 你有结肠。查找该值,将其设置为零,并设置指向下一个字符的指针,即始终为值。然后往后退一步,直到你到达一条线的起点或一个空格 所以 然后对碎片做你想做的 如果是星号大小写,请检查
变量的最后一个字符
int var1, var2, var3, var4;
if(variable[strlen(variable-1)]=='*')
sscanf(value, "%d %d %d %d", &var1, &var2, & var3, &var4);
else var1=atoi(value);
我想你已经有一行文字了
解析变量的算法:
char* variables = strtok(line, ":");
char* value = strtok(NULL, " ");
char* variable = strtok(variable, " ");
while ( variable != NULL )
{
// Do something with the variable.
...
// Get the next variable
variable = strtok(NULL, " ");
}
解析算法:
char* staticString = strtok(line, ":");
char* values = strtok(NULL, " ");
char* value = strtok(values, " ");
while ( value != NULL )
{
// Do something with the value
...
// Get the next value
value = strtok(NULL, " ");
}
回显输入的示例程序
#include <stdio.h>
#include <string.h>
// Function to parse the "Variable:value" lines.
void parseLine1(char* line)
{
char* variables = strtok(line, ":");
char* value = strtok(NULL, " ");
char* variable = strtok(variables, " ");
while ( variable != NULL )
{
// Do something with the variable.
printf("variable: %s\n", variable);
// Get the next variable
variable = strtok(NULL, " ");
}
// Do something with the value.
printf("value: %s\n", value);
}
// Function to parse the "STATIC*:values" lines.
void parseLine2(char* line)
{
char* staticString = strtok(line, ":");
char* values = strtok(NULL, ":");
char* value = strtok(values, " ");
// Do something with the static string.
printf("staticString: %s\n", staticString);
while ( value != NULL )
{
// Do something with the value
printf("value: %s\n", value);
// Get the next value
value = strtok(NULL, " ");
}
}
int main()
{
char line[100];
strcpy(line, "Variable:200");
parseLine1(line);
strcpy(line, "Variable1:Joe");
parseLine1(line);
strcpy(line, "Variable0 Variable1:10.5");
parseLine1(line);
strcpy(line, "Variable0 Variable1 Variable2:-59.7");
parseLine1(line);
strcpy(line, "STATIC*:10 20 30 40");
parseLine2(line);
return 0;
}
#包括
#包括
//函数来解析“变量:值”行。
无效解析行1(字符*行)
{
char*variables=strtok(行“:”);
char*value=strtok(空,“”);
char*variable=strtok(变量“”);
while(变量!=NULL)
{
//对变量执行一些操作。
printf(“变量:%s\n”,变量);
//获取下一个变量
变量=strtok(空,“”);
}
//做一些有价值的事情。
printf(“值:%s\n”,值);
}
//函数来解析“STATIC*:values”行。
无效解析行2(字符*行)
{
char*staticString=strtok(行“:”);
char*values=strtok(NULL,“:”);
char*value=strtok(值“”);
//对静态字符串执行一些操作。
printf(“staticString:%s\n”,staticString);
while(值!=NULL)
{
//做一些有价值的事情
printf(“值:%s\n”,值);
//获取下一个值
值=strtok(空,“”);
}
}
int main()
{
字符行[100];
strcpy(行,“变量:200”);
第1行(第1行);
strcpy(行,“变量1:Joe”);
第1行(第1行);
strcpy(行,“变量0变量1:10.5”);
第1行(第1行);
strcpy(行,“变量0变量1变量2:-59.7”);
第1行(第1行);
strcpy(行,“静态*:10 20 30 40”);
第2行(第2行);
返回0;
}
#include <stdio.h>
#include <string.h>
// Function to parse the "Variable:value" lines.
void parseLine1(char* line)
{
char* variables = strtok(line, ":");
char* value = strtok(NULL, " ");
char* variable = strtok(variables, " ");
while ( variable != NULL )
{
// Do something with the variable.
printf("variable: %s\n", variable);
// Get the next variable
variable = strtok(NULL, " ");
}
// Do something with the value.
printf("value: %s\n", value);
}
// Function to parse the "STATIC*:values" lines.
void parseLine2(char* line)
{
char* staticString = strtok(line, ":");
char* values = strtok(NULL, ":");
char* value = strtok(values, " ");
// Do something with the static string.
printf("staticString: %s\n", staticString);
while ( value != NULL )
{
// Do something with the value
printf("value: %s\n", value);
// Get the next value
value = strtok(NULL, " ");
}
}
int main()
{
char line[100];
strcpy(line, "Variable:200");
parseLine1(line);
strcpy(line, "Variable1:Joe");
parseLine1(line);
strcpy(line, "Variable0 Variable1:10.5");
parseLine1(line);
strcpy(line, "Variable0 Variable1 Variable2:-59.7");
parseLine1(line);
strcpy(line, "STATIC*:10 20 30 40");
parseLine2(line);
return 0;
}