我试图测试我的程序,看看它是否会获取 path.txt 文件,并输出第一行分隔文件和目录字符串,但是当我运行程序时,什么也没有出现。似乎它正在经历一个无限循环。如何解决?
文本文件:path.txt
a/a1.txt
a/a2.txt
a/b/b3.txt
a/b/b4.txt
a/c/c4.txt
a/c/c5.txt
a/c/d/d6.txt
a/c/d/g
a/c/d/h
a/c/e/i/i7.txt
a/c/f/j/k/k8.txt
代码
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct MyPath{
char *element;
struct MyPath *next;
} tMyPath;
int main(void)
{
FILE *pFile;
pFile = fopen("path.txt", "r");
char inputstr[1024];
tMyPath *curr, *first = NULL, *last = NULL;
//get the text file, and put it into a string inputstr
if (pFile != NULL)
{
while(!feof(pFile))
{
fgets(inputstr, sizeof(inputstr), pFile);
}
fclose(pFile);
}
else
{
printf("Could not open the file.\n");
}
//using tokens to get each piece of the string
//seperate directories and text files
char *token = strtok(inputstr, "/");
while (token != NULL)
{
//print
printf("%s\n", token);
//basecase
token = strtok(NULL, "/");
return 0;
}
}