简体   繁体   English

在c中逐行写入另一个文本文件中的文本文件

[英]Writing to a text file from another text file line by line in c

I'm trying to write to a the text file fileout a line at a time from another file, file_in1 , which is defined globally. 我想从另一个文件,file_in1,这是全局定义一个时间来写一个文本文件FILEOUT一行。 I'm getting errors from the code bellow and I don't know why, If someone could figure out why that'd be great. 我从下面的代码中得到错误,我不知道为什么,如果有人能弄明白为什么那么伟大。 Thanks! 谢谢!

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);
    }
}

You declare 你宣布

char line[40];

but later do 但后来呢

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

line cannot hold 90 characters. 行不能容纳90个字符。 Either make line larger or read fewer characters. 要么使line更大,要么读取更少的字符。

the following code 以下代码

  1. compiles cleanly 干净利落地编译
  2. does proper error checking 进行适当的错误检查
  3. properly reads in the output file name 正确读入输出文件名
  4. does not cause undefined behaviour (leading to a seg fault event) IE it limits the number of characters read by fgets() 不会导致未定义的行为(导致seg错误事件)IE它限制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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM