在我的 'writing a file' 程序中找不到错误

标签 c file-writing

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

    int main()

    {

        char file_name[100];
        char insert[100];

        printf("Enter the file name:");
        scanf("%s", file_name);
        FILE *f = fopen((strcat(file_name, ".txt")), "a");
        printf("Insert:");
        scanf ("%[^\n]%*s", insert);
        fprintf(f, "%s", insert);

    }

上述代码应该向文件写入/插入数据,但是,程序在打印“Insert:”后立即终止。我似乎在这里找不到问题。

最佳答案

scanf :

scanf("%s", file_name);

不会消耗您输入文件名后按的换行符。它保留在标准输入流(stdin)中。 scanf :

scanf ("%[^\n]%*s", insert);

扫描所有内容直到一个换行符,然后扫描并丢弃一个字符串。 <强> %[^\n]如果下一个字符是 \n 将会失败。所以,scanf ,在您的情况下失败并返回,因为下一个字符是 \n . insert保持未初始化并包含垃圾。

fprintf(f, "%s", insert);

将垃圾写入f .


如何解决这个问题?解决问题的两种方法:

  1. 使用 getchar();就在第一个 scanf 之后摆脱第一个 scanf 留下的换行符.
  2. 使用

    scanf (" %[^\n]%*s", insert);
    

    代替

    scanf ("%[^\n]%*s", insert);
    

    %[^\n]之前的空格扫描并丢弃所有 whitespace characters (如 \n 、空格等),如果有的话,直到第一个非空白字符,如下面 C11 标准的引用所示:

    7.21.6.2 The fscanf function

    [...]

    1. A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails

我更喜欢第二种解决方案,因为它处理任何 数量的空白字符。


这两种解决方案都有效,但是您会遇到另一个问题。在您为第二个 scanf 提供数据后,您的程序不会停止扫描!解决这个问题很容易。只需删除 %*s从第二个scanf .

想知道为什么吗?引用C11标准,

7.21.6.2 The fscanf function

[...]

  1. The conversion specifiers and their meanings are:
    [...]
    s Matches a sequence of non-white-space characters. 286
    [...]

所以,%s将跳过前导空白字符,并在遇到空白字符时停止扫描,或者,如果存在长度字段,直到指定长度或直到空白字符,以先发生者为准。粗体部分解释了它。


为了提高安全性,限制 scanf 的字符数读取 insertfile_name这样你就可以避免buffer overflows .此外,检查 scanf 的返回值s 和 fopen看看他们是否成功。 scanf s,在你的情况下,如果成功则返回 1。如果不是 1,则发生错误。 fopen , 返回 NULL失败时。如果不是NULL , fopen成功了。


最终程序:

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

int main()        
{        
    char file_name[100];
    char insert[100];

    printf("Enter the file name:");
    if(scanf("%94s", file_name) != 1)  //Scans a maximum of 94 chars,+1 for \0 at the end,+4 for ".txt"
    {
        printf("scanf failed\n");
        exit(-1); //Exit the program with a negative return value
    }

    strcat(file_name, ".txt"); //Improves readability,do one thing in one line 
    FILE *f = fopen(filename, "a");

    if(f == NULL) //if fopen failed to open the filename,
    {
        printf("fopen failed\n");
        exit(-1); 
    }

    printf("Insert:");
    if(scanf (" %99[^\n]", insert) != 1) // Note the space before %[^\n],99 scans a maximum of 99 chars,+1 for the \0 at the end
    {
        printf("scanf failed\n");
        fclose(f); //Free resources before exiting
        exit(-1);
    }
    fprintf(f, "%s", insert);
    fclose(f); //Close the file after use
}


如果你想丢弃第二个 scanf 留下的换行符, 使用

scanf (" %99[^\n]%*c", insert)

代替

scanf (" %99[^\n]", insert)

不过,这对这个程序没有影响。


如果你想知道为什么我要引用“7.21.6.2 fscanf 函数”一章的话,那是因为,fscanfscanf 完全相同当它的第一个参数是 stdin .再次引用 C11 标准,

7.21.6.4 The scanf function

[...]

  1. The scanf function is equivalent to fscanf with the argument stdin interposed before the arguments to scanf.

关于在我的 'writing a file' 程序中找不到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30151909/

相关文章:

c - scanf 读取错误值

json - 更改文件的最后一个字符

java - java/jsp Web 应用程序中的 FileOutputStream 路径

c - 在 strstr 遇到 sigsegv 之前停止它

c - 代码块错误

c - 如何处理 void 函数中的错误条件

c - "producing negative zeroes"在不支持的系统中是什么意思?

c - C 语言中字符串的结构空字符(\0) 问题

python - 写入带有 BOM 的 UTF-16-LE 文本文件

rust - 如何将十六进制数字而不是&[u8]写入文件