-
copy two files into one
:confused:
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.
Thanx!!!
-
Code:
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:
Code:
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. :)
-Skipper
-
hi,
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, :)
-
Somthing like this should work.
Code:
std::ifstream fin1("org1.txt")
std::ifstream fin2("org2.txt")
std::ofstream fout("new.txt")
while (true)
{
std::string str1,str2;
if (fin1)
std::getline(fin1,str1);
if (fin2)
std::getline(fin2,str2);
if (!fin1 && !fin2)
break;
fout << str1 << " " << str2 << std::endl
}
-
Thanx guys for the help!!!
Skipper:
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:
<code>
Debug Assertion Failed!
Program: ...\tmp\Debug\concat.exe
File: fopen.c
Line: 53
Expression: file != NULL
</code>
Mickey:
thanx, I'll try to see if i can code something like that :)
Sang-drax:
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 :(
<code>
Compiling...
concat.cpp
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)
</code>