我打开输入文件。 我读了它,将它存储在一个二维数组中,分成 token 。
首先strtok“\n”并存储它 其次 strtok for ":"忽略它 然后 strtok 为“,”并存储它。
我想为每一行搜索一个单词(我在第 5 步中存储了它)
为了搜索,我使用 strstr 但我做不到。 我在下面添加我的输入文件和代码:
int main (){
FILE *fp;
fp = fopen ("C:\\input.txt", "r");
char *name[10][10];
char row[100];
char *token, *tkn, *tk;
int h = 0, f = 0, l = 0;
for (h = 0; h < 9; h++){
for (f = 0; f < 9; f++)
name[h][f] = NULL;
}
while (fgets (row, sizeof (row), fp)){
token = strtok (row, "\n");
name[l][0] = strdup (token);
//printf("%s",name[l][0]);
tkn = strtok (token, ":");
tk = strtok (tkn, ",");
m[l][1] = strdup (tk);
if (strstr (name[l][1], name[l + 1][0]) == 0
|| strstr (name[l][1], name[l + 2][0]) == 0
|| strstr (name[l][1], name[l + 3][0]) == 0)
{
}
l++;
}
fclose (fp);}
我想用这个函数插入一个链接列表。
我的输入文件:
George :Math1,History2,Math2
ELizabeth :Math2,Germany1,spanish1
Adam :Germany1,History2,Math1
我想创建一个包含讲座名称的图表,并且我想使用邻接列表。对于邻接列表,我需要连接两个讲座,但我坚持这一步。如何搜索整行然后存储数组。例如我搜索 Math1
。它有 2 行。然后我想为 Math1
创建一个 adj 列表 Math1 ->History2->Math2->Germany1
。请帮助我
最佳答案
主要回答你的评论,你的if
语句如果这样写可能会更清晰
if (!strstr (name[l][1], name[l + 1][0])
|| !strstr(name[l][1], name[l + 2][0])
|| !strstr(name[l][1], name[l + 3][0]))
{}
else
{
InsertVertex{nodeHeader, studentName, courseName);
}
您必须定义并初始化nodeHeader、studentName 和 courseName
使用调试器确认您的数组已正确填充,然后逐步执行您的代码 - 并让我们知道您的进展情况。
你确实有太多的错误无法全部列出......:你的 l 最终将跨越你的数组边界,你一次处理一行,但在数组索引中硬编码 0 和 1,并且在开始处理之前,您可能需要了解所有行的内容。
我首先让 InsertVertex 处理手动创建的数据,然后将其添加到循环中并进行调试,直到它处理文件为止
关于c - 邻接表,分割一个数组并在c中搜索该数组中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27648648/