英文:
Why isnt the getc function giving me the right integer?
问题
I was trying to learn how to write and read in a text file in C and I wrote this code:
#include <stdio.h>
int main(){
FILE *pointer_to_text;
pointer_to_text = fopen("kot.txt","w");
fprintf(pointer_to_text,"%c",'c');
int c = getc(pointer_to_text);
printf("%d\n",c);
fclose(pointer_to_text);
}
I open the file with fopen()
, write a single char
to it using fprintf()
and then I want to back the value that I've just written.
However, getc()
returns 0
instead of the character that I've just written.
I was just curious why it does that and what am I missing.
英文:
I was trying to learn how to write and read in a text file in C and I wrote this code:
#include <stdio.h>
int main(){
FILE *pointer_to_text;
pointer_to_text = fopen("kot.txt","w");
fprintf(pointer_to_text,"%c",'c');
int c = getc(pointer_to_text);
printf("%d\n",c);
fclose(pointer_to_text);
}
I open the file with fopen()
, write a single char
to it using fprintf()
and then I want to back the value that I've just written.
However, getc()
returns 0
instead of the character that I've just written.
I was just curious why it does that and what am I missing.
答案1
得分: 4
在以下代码行中:
pointer_to_text = fopen("kot.txt", "w");
你以写入模式打开了文件。如果你想创建一个新文件(覆盖任何已存在的文件),但也想能够从中读取,你应该使用模式 "w+"
而不是 "w"
。
另外,如果你想从写入切换到读取,必须在中间调用 fflush
或调用文件定位函数(如 fseek
或 rewind
)。否则,你的程序将引发未定义行为。请查看 fopen
的文档以获取更多信息。
在写入文件后,文件位置将位于文件的末尾。因此,如果你想读取之前写入的内容,必须首先调用 rewind
将文件位置移回文件的开头。
英文:
In the line
pointer_to_text = fopen("kot.txt","w");
you opened the file in write mode. If you want to create a new file (overwriting any existing file) but also want to be able to read from it, you should use the mode "w+"
instead of "w"
.
Also, you cannot switch from writing to reading without an intervening call to fflush
or call to a file positioning function (such as fseek
or rewind
). Otherwise, your program will invoke undefined behavior. See the documentation for fopen
for further information.
After writing the file, the file position will be at the end of the file. Therefore, if you want to read what you have previously written, you must first call rewind
to go back to the start of the file.
答案2
得分: 2
以下是您要翻译的部分:
显然,您不能从已打开用于写入的文件中读取。还要记住,始终应检查IO。以下是您可以执行的方式:
#include <stdio.h>
int main(void)
{
FILE *file = fopen("kot.txt", "w");
if (!file) {
return 1;
}
if (fprintf(file, "%c", 'c') != 1) {
return 2;
}
if (fclose(file) != 0) {
return 3;
}
file = fopen("kot.txt", "r");
if (!file) {
return 4;
}
const int ch = getc(file);
if (ch == EOF) {
return 5;
}
printf("%c\n", ch); // <- 通常不检查printf失败,因为
// 很少能从中恢复。
if (fclose(file) != 0) {
return 6;
}
return 0;
}
英文:
Obviously you can't read from a file you've opened for writing. Also keep in mind that IO should always be checked. Here's how you may do it:
#include <stdio.h>
int main(void)
{
FILE *file = fopen("kot.txt", "w");
if (!file) {
return 1;
}
if (fprintf(file, "%c", 'c') != 1) {
return 2;
}
if (fclose(file) != 0) {
return 3;
}
file = fopen("kot.txt", "r");
if (!file) {
return 4;
}
const int ch = getc(file);
if (ch == EOF) {
return 5;
}
printf("%c\n", ch); // <- printf failures are typically not checked, since
// it's rare that you can recover from that.
if (fclose(file) != 0) {
return 6;
}
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论