【发布时间】:2016-04-25 17:18:23
【问题描述】:
我有一个基本的 .txt 文件,其中可能包含未知数量的完全采用这种格式的数据,我需要提取 '=' 标识符之后的第二部分。例如:
variable1=Hello
variable2=How
variable3=Are
variable4=You?
我需要提取“Hello”“How”“Are”和“You?”分别并将它们存储到一个数组中(删除/忽略变量名)并能够单独调用每个单词。我在 C 中做这个,这就是我目前拥有的。
#include <stdio.h>
#include <string.h>
int main()
{
char*result;
char copy[256];
FILE * filePtr;
filePtr = fopen("testfile.txt", "r+");
strcpy(copy, "testfile.txt");
while(fgets(copy, 256, filePtr)!= NULL)
{
result = strchr(copy, '=');
result = strtok(NULL, "=");
printf("%s",result);
if(result != 0)
{
*result = 0;
}
result = strtok(copy, "=");
}
return 0;
}
我目前的输出是
(null)How
Are
You?
【问题讨论】: