【发布时间】:2016-04-30 11:51:04
【问题描述】:
我有一个文本文件:
recipName=叉子朋友=Cup sonName=Spork feature=hair sendName=Spoon"
我想要做的是将 = 符号之前的所有单词复制到一个字符数组,并将 = 右侧的内容复制到另一个字符数组或字符串。
这是我目前的代码:
int main (int argc, char * argv[])
{
char data[100];
char line[5][100];
char key[5][100];
char value[5][100];
FILE * fdata = fopen(argv[1], "r"); //read data.txt file
FILE * ftemp = fopen(argv[2], "r"); //read and write to template.txt file
if (fdata == NULL)
{
printf("could not read file.\n");
}
int i = 0;
while (fgets(data, 100, fdata) != NULL)
{
printf("data: %s", data);
//this is where i get stuck, idk how to utilize this loop to copy the variable and variable names from the data.txt file i was given...thanks for the help
++i;
}
fclose(fdata);
fclose(ftemp);
return 0;
}
【问题讨论】:
-
data是char的数组,因此(可能是嵌套的)循环可用于按顺序检查和复制单个字符。另外,请查看标准头文件<string.h>中的函数文档,例如strtok()。鉴于您在阅读数据后显示的代码完全为零,因此我怀疑很多人会提供更多细节。 -
在阅读帮助中心关于为 SO 写好问题的信息时,有一个大意为:“SO 是为了解决问题,而不是获得帮助”。此时您正在寻求帮助。
-
你可能需要一个结构体。