Thread: copy two files into one

  1. #1
    Registered User
    Join Date
    Oct 2002

    Unhappy copy two files into one

    hey guys, just started on C++. Having trouble... I would appreciate if someone could help me with this.

    Program should open two text files for input and one for output (files exist). The program concatenates the corresponding lines of the input lines, using a space as a separator, and writing the results to the output file. If one file is shorter than the other, the remaining lines in the longer file are also copied to the output file.

    if i run my code without including the 3 files, instead of the error messages i get "Debug Assertion Failed!"

    if i run it in a format: <c:\concat file_in_1 file_in_2 file_out>, then all the lines from file_in_1 are missing the first character.

    i'm attaching the concat.cpp file.


  2. #2
    Registered User
    Join Date
    Apr 2002
    while (fin_A.get(ch) || fin_B.get(ch))
    		fin_A.getline(buf_A, 80);	//read a line from the first file
    		fin_B.getline(buf_B, 80);	//read a line from the second file
    		fout_C << buf_A << " " << buf_B << "\n";	//write the lines to the 3rd file
    		cout << "buf_A is: " << buf_A << "\n";	//show the value of buf_A on the screen
    		cout << "buf_B is: " << buf_B << "\n";	//show the values of buf_B on the screen
    You've afforded us a good opportunity to see why C/C++ will "eat your lunch" if you don't understand what is taking place, waldis.

    What does your WHILE test do? It reads a character from the first file - presumably to see if there is a line to be read - and, if not, tests the second file the same way.

    The problem? (You see this coming, don't you? ) 'get()' consumes your first char. That's why all of the first characters from the first lines of your first file are missing.

    Why only the first file? Logical OR ( || ) needs only one of the two conditions to be "true" in order for the loop execution to continue. If the first meets that criteria, the second is never tested.

    Try this instead:
    while ( fin_A || fin_B ){
    // code
    This syntax tests the error bits (flags) including EOF which is set only after the last char in the file is read. I'm betting this will clear up your problem.

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    Registered User
    Join Date
    Apr 2002

    you could also just open them both as binary and transfer both to another file,

    ie, read both files, store them on a buffer, then make a ofstream object, write both of these buffers to it, flush, close,

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Göteborg, Sweden
    Somthing like this should work.
    std::ifstream fin1("org1.txt")
    std::ifstream fin2("org2.txt")
    std::ofstream fout("new.txt")
    while (true)
      std::string str1,str2;
      if (fin1)
      if (fin2)
      if (!fin1 && !fin2)
      fout << str1 << " " << str2 << std::endl
    Last edited by Sang-drax; 10-20-2002 at 07:09 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Oct 2002
    Thanx guys for the help!!!


    now i'm getting all the characters, thanx! But it's still giving me the error of "Debug Assertion Failed!" after i try to run the prog to see my error messages:

    Debug Assertion Failed!

    Program: ...\tmp\Debug\concat.exe
    File: fopen.c
    Line: 53

    Expression: file != NULL


    thanx, I'll try to see if i can code something like that


    thanx for the code, but it's a little over my head, and it is giving me weird compile errors about 'getline', thou when i type std::getline, the intellisense gives me the 'getline' in the list. On this one i don't have a clue what's wrong

    C:\tmp\concat.cpp(65) : error C2039: 'getline' : is not a member of 'std'
    C:\tmp\concat.cpp(65) : error C2065: 'getline' : undeclared identifier
    C:\tmp\concat.cpp(67) : error C2039: 'getline' : is not a member of 'std'
    C:\tmp\concat.cpp(72) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocat
    or<char> >' (or there is no acceptable conversion)
    Error executing cl.exe.

    concat.obj - 4 error(s), 0 warning(s)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. fopen vs. _open (for BIG image files)
    By reversaflex in forum C Programming
    Replies: 3
    Last Post: 04-01-2007, 12:52 AM
  4. Copy files
    By ErikDN in forum C Programming
    Replies: 1
    Last Post: 10-09-2004, 07:50 PM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM