我正在尝试编写一个程序来读取未知大小/行大小的文件但我在检测新行字符时遇到了一些问题.
当我运行程序时,它永远不会到达while循环中的行点的末尾,readFile并且会不断运行.如果我运行打印每个字符,它会打印出一些未知的字符.
我已经尝试设置ch为一个int值和类型转换为char进行\n比较.它没有达到这个EOF条件,所以我不确定发生了什么.
码:
void readFile(FILE* file)
{
int endOfFile = 0;
while (endOfFile != 1)
{
endOfFile = readLine(file);
printf("%d\n", endOfFile);
}
}
int readLine(FILE* file)
{
static int maxSize = LINE_SIZE;
int currentIndex = 0;
int endOfFile = 0;
char* buffer = (char*) malloc(sizeof(char) * maxSize);
char ch;
do
{
ch = fgetc(file);
if ((ch != EOF) || (ch != '\n'))
{
buffer[currentIndex] = (char) ch;
currentIndex += 1;
}
if (currentIndex == maxSize)
{
printf("Reallocating string buffer");
maxSize *= 2;
buffer = (char*) realloc(buffer, maxSize);
}
} while ((ch != EOF) || (ch != '\n'));
if (ch == EOF)
{
endOfFile = 1;
}
parseLine(buffer);
free(buffer);
return endOfFile;
}
Run Code Online (Sandbox Code Playgroud)
如果有人可以帮助我,我会非常感激,因为我已经坚持这个问题很长一段时间了.提前致谢.
(ch != EOF) || (ch != '\n')
Run Code Online (Sandbox Code Playgroud)
这总是如此.
你想要一个&&(AND),在你的if和你的while,否则它永远不会停止.