用C语言逐行从另一个文本文件写入到文本文件

3

我正在尝试从全局定义的文件file_in1中逐行向文本文件fileout写入内容。下面的代码出现了错误,我不知道原因。如果有人能找出问题所在,那就太好了。谢谢!

void output() 
{
    FILE *fileout;
    char line[40];

    file_in1 = fopen(filename1, "r");

    printf("Please enter the name of the output file: ");
    scanf("%s", filename); //Reads filename

    fileout = fopen(filename, "w");

    if (fileout == NULL) {
        printf("(Ensure the file is located in the project \n file folder and has the .txt extension) \n");
        output();
    }

    while (fgets(line, 90, file_in1) != NULL) //Looks through characters until end of the file
    {
        fputs(line, fileout);
    }
}

2
完整的错误信息是什么? - William Price
这是一个类型为 fgets(line, 90, file_in1) 的吗?因为你声明了 char line[40] - Iharob Al Asimi
3
程序似乎陷入了一个循环中,它似乎没有注意到“!=NULL”的条件。 - JL9
1
file_in1在此处已经打开,因此不能在不同的函数中进行检查。另外,在递归输出(output())之前printf中的消息表明文件应该被定位在某个地方,但这不应该是fileout而是file_in1吧?不管怎样,file_in1仍在泄露。(并且在output()返回后一切都会崩溃)。 - user3125367
1
即使 file_in1 没有问题,当递归返回时,它将尝试在一个为 NULLfileout 上进行写入。 - chrk
显示剩余9条评论
2个回答

4
你需要声明 <\p>
char line[40];

但稍后进行

               //   v--- 90?
while (fgets(line, 90, file_in1) != NULL)

行不能容纳90个字符。要么将line变大,要么读取更少的字符。


1
这可能是错误,但如果在“40”个字符之前的文本中有换行符,则不是。 - Iharob Al Asimi
1
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - jim mcnamara
1
@JL9,别忘了接受这个答案,这样我们就不用再回来尝试回答它了 :) - Iharob Al Asimi

0

以下代码

  1. 编译干净
  2. 进行适当的错误检查
  3. 正确读取输出文件名
  4. 不会导致未定义行为(导致段错误事件),即它限制了fgets()读取的字符数

-

void output() 
{
    FILE *fileout;
    char line[40];

    //file_in1 = fopen(filename1, "r"); // <-- always check results
    if( NULL == (file_in1 = fopen(filename1, "r") ) )
    { // fopen failed
        perror( "fopen failed for input file" ); // writes failure details to stderr
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful for input file

    printf("Please enter the name of the output file: ");
    //scanf("%s", filename); //Reads filename // <-- always check return code
                                              //     and allow for leaidng white space skipping
    if( 1 != scanf( " %s", filename) )
    { // scanf failed
        perror( "scanf failed for output file name" ); // writes failure details to stderr
    }

    // implied else, scanf for output file name successful

    //fileout = fopen(filename, "w");
    //if (fileout == NULL) // <-- always place the literal on the left to enable compiler to catch certain syntax errors
    if( NULL == (fileout = fopen(filename, "w")))
    { // then, fopen failed
        perror( "fopen failed for output file" ); // writes failure details to stderr
        printf("(Ensure the file is located in the project \n file folder and has the .txt extension) \n");
        // output(); <-- the program failed, do not continue  and certainly not recursively
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful for output file

    //while (fgets(line, 90, file_in1) != NULL) //Looks through characters until end of the file
                                                // <-- line is only defined as 40 characters
    while( NULL != fgets(line, sizeof(line), file_in1))
    {
        fputs(line, fileout);
    }
} // end function: output

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接