get(file Pointer) 的返回值不变
returned value from fget(filePointer) doesn't changed
我尝试使用 unsigned char ch1 = fgetc(filePointer);
提取一个字符,但返回的值始终是 255
。这是我的代码:
#include <stdio.h>
#include "anotherCFile.c"
int main(int argc, char **argv)
{
int ch;
FILE* filePointer;
filePointer = fopen("text.txt", "w+");
ch = fgetc(stdin);
while (ch!=EOF) {
fputc(ch, filePointer);
ch = fgetc(stdin);
}
unsigned char ch1 = fgetc(filePointer);
fclose(filePointer);
printf("%c", ch1);
return 0;
}
对于我检查过的任何输入 (s.a。ABC
),输出是 �
。当我将行 printf("%c", ch1);
更改为 printf("%d", ch1);
时,输出总是 255
。我想获取我在输入中输入的字符。
(text.txt
文件已正确创建)。谢谢。
当您写入 *FILE ptr
时,它的指针被提前并设置到文件末尾,准备追加下一个字符。
要在不重新打开文件并重新使用同一文件指针的情况下读回一些数据,您可能需要先倒回指针。
尝试添加
fseek(filePointer, 0, SEEK_SET);
读回数据之前
在 unsigned char ch1 = fgetc(filePointer);
时,filepointer
指向 EOF(-1)
,在下一个语句中,您将打印 ASCII
的 255(bcz ch is declared as unsigned)
值。
EOF
定义为 #define EOF (-1)
printf("%c\n,ch1); /* EOF is not printable char so it prints � */
printf("%d\n",ch1); /* fgetc() returns int, As char read is stored in a
variable of type int and that is EOF,and -1
equivalent unsigned value is 255 */
我想获取我在输入中输入的字符。?在阅读之前做 rewind() or fseek()
。
rewind(filepointer);/* this is required to get first char of file */
unsigned char ch1 = fgetc(filePointer);
fclose(filePointer);
printf("%c", ch1);
我尝试使用 unsigned char ch1 = fgetc(filePointer);
提取一个字符,但返回的值始终是 255
。这是我的代码:
#include <stdio.h>
#include "anotherCFile.c"
int main(int argc, char **argv)
{
int ch;
FILE* filePointer;
filePointer = fopen("text.txt", "w+");
ch = fgetc(stdin);
while (ch!=EOF) {
fputc(ch, filePointer);
ch = fgetc(stdin);
}
unsigned char ch1 = fgetc(filePointer);
fclose(filePointer);
printf("%c", ch1);
return 0;
}
对于我检查过的任何输入 (s.a。ABC
),输出是 �
。当我将行 printf("%c", ch1);
更改为 printf("%d", ch1);
时,输出总是 255
。我想获取我在输入中输入的字符。
(text.txt
文件已正确创建)。谢谢。
当您写入 *FILE ptr
时,它的指针被提前并设置到文件末尾,准备追加下一个字符。
要在不重新打开文件并重新使用同一文件指针的情况下读回一些数据,您可能需要先倒回指针。
尝试添加
fseek(filePointer, 0, SEEK_SET);
读回数据之前
在 unsigned char ch1 = fgetc(filePointer);
时,filepointer
指向 EOF(-1)
,在下一个语句中,您将打印 ASCII
的 255(bcz ch is declared as unsigned)
值。
EOF
定义为 #define EOF (-1)
printf("%c\n,ch1); /* EOF is not printable char so it prints � */
printf("%d\n",ch1); /* fgetc() returns int, As char read is stored in a
variable of type int and that is EOF,and -1
equivalent unsigned value is 255 */
我想获取我在输入中输入的字符。?在阅读之前做 rewind() or fseek()
。
rewind(filepointer);/* this is required to get first char of file */
unsigned char ch1 = fgetc(filePointer);
fclose(filePointer);
printf("%c", ch1);