为什么`getc`函数没有给我正确的整数?

huangapple go评论54阅读模式
英文:

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 &lt;stdio.h&gt;
int main(){
    FILE *pointer_to_text;
    pointer_to_text = fopen(&quot;kot.txt&quot;,&quot;w&quot;);
    fprintf(pointer_to_text,&quot;%c&quot;,&#39;c&#39;);
    int c = getc(pointer_to_text);
    printf(&quot;%d\n&quot;,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 或调用文件定位函数(如 fseekrewind)。否则,你的程序将引发未定义行为。请查看 fopen 的文档以获取更多信息。

在写入文件后,文件位置将位于文件的末尾。因此,如果你想读取之前写入的内容,必须首先调用 rewind 将文件位置移回文件的开头。

英文:

In the line

pointer_to_text = fopen(&quot;kot.txt&quot;,&quot;w&quot;);

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 &quot;w+&quot; instead of &quot;w&quot;.

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 &lt;stdio.h&gt;

int main(void) 
{
    FILE *file = fopen(&quot;kot.txt&quot;, &quot;w&quot;);
    if (!file) {
        return 1;
    }
    if (fprintf(file, &quot;%c&quot;, &#39;c&#39;) != 1) {
        return 2;
    }
    if (fclose(file) != 0) {
        return 3;
    }
    file = fopen(&quot;kot.txt&quot;, &quot;r&quot;);
    if (!file) {
        return 4;
    }
    const int ch = getc(file);
    if (ch == EOF) {
        return 5;
    }

    printf(&quot;%c\n&quot;, ch);  // &lt;- printf failures are typically not checked, since
                         // it&#39;s rare that you can recover from that.

    if (fclose(file) != 0) {
        return 6;
    }

    return 0;
}

huangapple
  • 本文由 发表于 2023年5月10日 13:05:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定