【问题标题】:Why does (while .. getchar()) does not write to my file, in C?为什么 (while .. getchar()) 不会在 C 中写入我的文件?
【发布时间】:2021-05-30 00:15:38
【问题描述】:

我需要编写一个程序,要求用户输入字符串,每个字符串在用户按下“Enter”时结束。

  • 程序需要接收文件名作为参数,每次操作都应打开和关闭文件,并且对于输入的每个字符串,程序应将字符串附加到文件末尾(在新行上)。

这是我目前的代码:

 int is_file_exists(char *file_name)
{
    
    FILE *file;
    if ((file = fopen(file_name,"r"))!=NULL)    
        {
            /* file exists */
            fclose(file);
            return 1;
        }    
    else  
        {
            //File not found, no memory leak since 'file' == NULL
            //fclose(file) would cause an error
            return 0;
        }
        
}

int main(int argc, char **argv)
{
    char c;
    FILE *file;

    if (argc >= 2)
    {
         if (is_file_exists(argv[1]))
         {
             file = fopen(argv[1], "w");
         }
         else
         {
             return 0;
         }
    }
    else
    {
         file = fopen("file.txt", "w");
    }

    while ((c = getchar()) != EOF)
    {
        putc(c, file);
    }

    return 0;
}

到目前为止,代码已编译并正在创建文件,但其中没有写入任何内容。

编辑:我还需要一些函数指针,请参阅我的 cmets on selected answer

【问题讨论】:

  • 首先c 必须是int,因为EOFint。事实上,循环永远不会结束。其次,您是否退出程序,如果是,如何退出?我问是因为您没有关闭或刷新文件。如果您还没有退出程序或者如果您强制终止程序,它可能无法正确写入文件。
  • 你检查过你终端当前位置file.txt的内容吗?
  • @AntoninGAVREL 是的。我正在通过“ls”和“gedit”或“cat”重新检查。
  • @kaylum 我已将其更改为 int 但它仍然没有写入文件。我通过 CTRL+Z 退出程序,稍后我需要添加一个特殊的“exit”输入,如果键入它将终止程序。
  • 在 Linux 上? ctrl-z 暂停程序。尝试 ctrl-d 发送 EOF。

标签: c file argv getchar write


【解决方案1】:

我认为其中一个问题是您正在打开和关闭一个文件,然后又重新打开它。最好只使用指针将其保持打开状态,同时测试打开文件是否没有问题。另一个问题是您在文件中写入,您不喜欢将文本附加到它吗?好吧,这是你的决定。至于代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h> // exit

typedef struct mystruct {
    char *exit_word;
    void (*exit_fptr)(int); // man exit
    int (*strcmp_fptr)(const char *, const char*); // man strcmp
}              t_mystruct;

int is_file_exists(char *filename, FILE **file)
{
    return (*file = fopen(filename,"a")) > 0;
}

#define BUFF_SIZE 1024

int main(int argc, char **argv)
{
    char c;
    FILE *file;
    t_mystruct s = {.exit_word = "-exit", .exit_fptr = &exit, .strcmp_fptr = &strcmp};

    if (argc >= 2) {
         if (!(is_file_exists(argv[1], &file)))
            return 0;
    }
    else
         file = fopen("file.txt", "a"); // open the file in append mode

    char buffer[BUFF_SIZE];
    while (42) {
        int i = 0;
        memset(buffer, 0, BUFF_SIZE);
        while ((c = getchar()) != '\n')
            buffer[i++] = c;
        if (!s.strcmp_fptr(buffer,s.exit_word)) {// exit if user type exit, allow you to fclose the file
            fclose(file);
            s.exit_fptr(EXIT_SUCCESS); // better to use the define
        }
        buffer[i] = '\n';
        fputs(buffer, file);
    }
    fclose(file);
    return 0;
}

【讨论】:

  • 谢谢! 1. while(42) 只是为了让它永远运行,对吗?你也可以写while(1)等等? 2. 我实际上需要创建一个内部有 3 个定义的结构:string ("-exit") 和 2 function pointers。一个指针指向类似 strcmp 的 CMP 函数,该函数检查用户的输入是否与结构中的字符串 -exit 匹配。如果是这样,它将运行第二个函数指针,该指针将指向将退出程序的函数。你能帮我解决这个问题吗?或者这个帖子太多了?
  • 是的,你可以使用 while(1) 或任何数字,当然让我编辑
  • 这里memset的目的是什么?我可以用别的东西代替它吗?
  • 将缓冲区重置为 0;您可以通过在其位置移动 char buffer[BUFF_SIZE] 来替换它
  • while ((c = getchar()) != '\n' &amp;&amp; i &lt; BUFF_SIZE)这一行,你这里不开块{ ...... }为什么?
【解决方案2】:

您的代码可以运行

记得在输入完成后按Ctrl+d。该文件将包含您预期的内容

您的代码等待 EOF 退出循环。 Ctrl+d 是输入EOF的一种方式,否则程序永远不会结束。 putc 将首先写入缓存,然后写入磁盘。这是文件系统的一种优化机制。您可以选择在打开文件时通过 DirectIO 避免这种情况。

程序正常终止后,文件会自动关闭,然后将缓存中的数据复制到磁盘;

但是当程序异常终止时,缓存中的数据可能会丢失。

文件应该关闭

需要关闭。 open 和 close 应该像 malloc 和 free 一样成对组织。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 2019-08-01
    相关资源
    最近更新 更多