Writing to a text file from another text file line by line in c

I am trying to write to a text file a line at a time from another file_in1 that is defined globally. I am getting errors from the code below and I don’t know why. If anyone could understand why this is great. Thank!

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

      

+3


source to share


2 answers


You declare

char line[40];

      

but later



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

      

Line

cannot contain 90 characters. Either do line

more or read less characters.

+4


source


following code

  • compiles cleanly
  • does correct error checking
  • reads correctly in the output file name
  • does not cause undefined behavior (resulting in a seg crash event) I.E. it limits the number of characters read by 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

      

0


source







All Articles