Thread: merging files into one

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    31

    merging files into one

    Hi,

    I am trying to copy several rtf files into one rtf file. this is basically what i have done

    Code:
    char in_filename[MAXPATHLEN];
    char out_filename[MAXPATHLEN];
    FILE *in_fp, *out_fp;
    char line_buffer[80];
    
    //....get the required file name through user input 
    // assumed they lead to correct files so the program does not
    // crash on incorrect file path
    
    
    if (out_fp = fopen(out_filename, "w")  == '\0')
    {
        printf("error opening file");
        break;
    }
    in_fp = fopen(in_filename, "r");
    while (fgets(line_buffer, 80, in_fp) != NULL)
        fputs(line_buffer, out_fp);
    fclose(in_fp);
    fclose(out_fp);
    but i get a segmentation fault after reading the first line of the file

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    you have to check the return value of fopen each and every time...never assume that the file can be opened!

    >> if (out_fp = fopen(out_filename, "w") == '\0')

    '\0' is used for representing the ASCII value zero and should only be used with text-based processing - for pointers use 0 or NULL.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    31
    That doesnt solve the segmenation fault.

    I can open a new file and open the file to read it.

    it crashes somewhere between the while loop statment.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    so you are checking the return value of 'in_fp'? also if the file can't be opened be sure to bypass any calls to fclose on the handle.
    Last edited by Sebastiani; 02-16-2006 at 07:26 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    31
    yep... doing the same thing as when i check out_fp

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    31
    I found out what i was doing wrong.

    When i check to see if i can open or read the file i didnt put braces around the openning of the file first.
    Code:
    if (out_fp = fopen(out_filename, "w") == NULL)
    
    /*** should be ***/
    
    if ((out_fp = fopen(out_filename, "w")) == NULL)
    For anyone who is interested.

    Thanks for the help about the '\0' character

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Edit: too late, this post was written before I saw the previous post.

    See my added stuff...

    Quote Originally Posted by mbooka
    Hi,

    I am trying to copy several rtf files into one rtf file. this is basically what i have done

    Code:
    char in_filename[MAXPATHLEN];
    char out_filename[MAXPATHLEN];
    FILE *in_fp, *out_fp;
    char line_buffer[80];
    
    //....get the required file name through user input 
    // assumed they lead to correct files so the program does not
    // crash on incorrect file path
    
    /* Without the added (blue) parentheses, you have a precedence issue, 
        you are assigning out_fp to the comparison result of fopen()'s return and NULL */
    if ((out_fp = fopen(out_filename, "w"))  == /* '\0' WRONG */NULL)
    /* fopen returns NULL on error, this is not the same as '\0' */
    {
        printf("error opening file");
        break;
    }
    in_fp = fopen(in_filename, "r");
    /* check in_fp is not NULL here.  There is no excuse even if you're sure the 
    file exists */
    while (fgets(line_buffer, 80, in_fp) != NULL)
        fputs(line_buffer, out_fp);
    fclose(in_fp);
    fclose(out_fp);
    but i get a segmentation fault after reading the first line of the file
    Last edited by cwr; 02-16-2006 at 07:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM