fgetc 函数无法正常工作
the function fgetc is not working properly
我正在测试 fgetc() 函数,但它不能正常工作(我以前使用过这个函数,所以我知道它是如何工作的)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file = NULL;
int n;
file = fopen("test.txt", "w+");
if(file != NULL)
{
fputs("ab", file);
printf("%c", fgetc(file));
}
else
{
printf("error");
}
return 0;
}
输出应该是 "a" 但它是其他东西
您的错误是您正在尝试读取一个已打开用于写入的文件。您应该在其中写入,然后关闭文件并重新打开以供阅读。此代码将显示我所说的内容:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fileRead, *fileWrite = NULL;
int n;
fileWrite = fopen("test.txt", "w+");
if(fileWrite != NULL)
{
fputs("ab", fileWrite);
fclose(fileWrite);
}
else
{
printf("error");
}
// Open again the file for read
fileRead = fopen("test.txt", "r");
printf("%c", fgetc(fileRead));
fclose(fileWrite);
// End function
return 0;
}
文件已打开以供写入和读取,但您需要 fseek
到文件中的正确位置(此处为开头)。特别是,在写作和阅读之间切换时,您需要 fseek
或 fflush
.
When the "r+", "w+", or "a+" access type is specified, both reading
and writing are enabled (the file is said to be open for "update").
However, when you switch from reading to writing, the input operation
must encounter an EOF marker. If there is no EOF, you must use an
intervening call to a file positioning function. The file positioning
functions are fsetpos, fseek, and rewind. When you switch from writing
to reading, you must use an intervening call to either fflush or to a
file positioning function.
无论如何,写入文件后,文件指针位于错误的位置以读取刚刚写入的内容。
所以代码变成
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *file = NULL;
file = fopen("test.txt", "w+");
if(file != NULL) {
fputs("ab", file);
fseek(file, 0, SEEK_SET);
printf("%c", fgetc(file));
fclose(file);
}
else {
printf("error");
}
return 0;
}
如果你想继续写入文件,你必须fseek
到它的末尾。
我正在测试 fgetc() 函数,但它不能正常工作(我以前使用过这个函数,所以我知道它是如何工作的)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *file = NULL;
int n;
file = fopen("test.txt", "w+");
if(file != NULL)
{
fputs("ab", file);
printf("%c", fgetc(file));
}
else
{
printf("error");
}
return 0;
}
输出应该是 "a" 但它是其他东西
您的错误是您正在尝试读取一个已打开用于写入的文件。您应该在其中写入,然后关闭文件并重新打开以供阅读。此代码将显示我所说的内容:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fileRead, *fileWrite = NULL;
int n;
fileWrite = fopen("test.txt", "w+");
if(fileWrite != NULL)
{
fputs("ab", fileWrite);
fclose(fileWrite);
}
else
{
printf("error");
}
// Open again the file for read
fileRead = fopen("test.txt", "r");
printf("%c", fgetc(fileRead));
fclose(fileWrite);
// End function
return 0;
}
文件已打开以供写入和读取,但您需要 fseek
到文件中的正确位置(此处为开头)。特别是,在写作和阅读之间切换时,您需要 fseek
或 fflush
.
When the "r+", "w+", or "a+" access type is specified, both reading and writing are enabled (the file is said to be open for "update"). However, when you switch from reading to writing, the input operation must encounter an EOF marker. If there is no EOF, you must use an intervening call to a file positioning function. The file positioning functions are fsetpos, fseek, and rewind. When you switch from writing to reading, you must use an intervening call to either fflush or to a file positioning function.
无论如何,写入文件后,文件指针位于错误的位置以读取刚刚写入的内容。
所以代码变成
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *file = NULL;
file = fopen("test.txt", "w+");
if(file != NULL) {
fputs("ab", file);
fseek(file, 0, SEEK_SET);
printf("%c", fgetc(file));
fclose(file);
}
else {
printf("error");
}
return 0;
}
如果你想继续写入文件,你必须fseek
到它的末尾。