JL9
JL9

Reputation: 543

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

Upvotes: 3

Views: 421

Answers (2)

user3629249
user3629249

Reputation: 16550

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

Upvotes: 0

Wintermute
Wintermute

Reputation: 44073

You declare

char line[40];

but later do

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

line cannot hold 90 characters. Either make line larger or read fewer characters.

Upvotes: 4

Related Questions