【发布时间】:2012-11-09 21:47:31
【问题描述】:
作为我的项目的一部分,我正在尝试从 C 语言文件中读取。
我的目的是将文件中的单词(由空格、逗号、分号或换行符分隔)解析为标记。
为此,我必须逐字阅读。
do {
do {
tempChar = fgetc(asmCode);
strcpy(&tempToken[i], &tempChar);
i++;
} while (tempChar != ' ' || tempChar != ':' || tempChar != ';' || tempChar != ',' || tempChar != '\n');//reading until the given parameters
i = 0;
//some other code with some other purpose
} while (tempChar != EOF);//reading until the end of the file
即使下面的代码从文件中读取,它也不会停止读取,因为它没有应用 while 内部的条件。
我在这里做错了什么?
附: tempChar 和 tempToken 都定义为 char 变量。还有一个
【问题讨论】:
-
strcpy(&tempToken[i], &tempChar);strcpy 需要一个以 0 结尾的char数组作为源,您正在传递一个char的地址,并且知道内存中的内容。 -
如果你到达 EOF,你的内部循环没有转义测试,你应该使用 && 而不是 ||做你想做的事。此外,根据上述评论者,tempToken[i] = tempChar,而不是 strcpy。
-
你的意思是
&&,而不是||,顺便说一句? -
您好,您为什么不阅读完整的文件并将其存储在字符串中,然后使用strtok? cplusplus.com/reference/clibrary/cstring/strtok
-
fgetc()的返回类型是int,而不是char。