I was trying to use what i have learned about file and resource handling in C++: I would like to write a diff
-like utility.
Here It is my latest version
#include <iostream>
#include <cstdlib>
#include <fstream>
int main(int argc, char* argv[])
{
if(argc!=3)
{
std::cout << "error: 2 arguments required, now exiting ..." << std::endl;
exit (EXIT_FAILURE);
}
std::ifstream file_1(argv[1]);
std::ifstream file_2(argv[2]);
if( file_1.fail() || file_2.fail() )
{
std::cout << "error: can't open files, now exiting ..." << std::endl;
exit (EXIT_FAILURE);
}
std::string dummy_1;
std::string dummy_2;
while(!file_1.eof()) // dummy condition
{
std::getline(file_1,dummy_1);
std::getline(file_2,dummy_2);
std::cout << ((dummy_1==dummy_2) ? "= " : "# ") << dummy_1 << std::endl << " " << dummy_2 << std::endl;
}
return(0);
}
This are my guidelines:
- compare 2 files
- the user must pass the names of this 2 files directly to the executable, only this 2 arguments
- to cover as much error handling as possible in C++
- try to avoid platform specific steps or non-portable code
My actual problem is that i don't know how to improve my dummy condition effectively. For now the while iteration just follows the length of the first passed file and I would like to obiviously go all the way down in both files and solve this without introducing an overkill like an extra cicle to get and compare the length of this 2 files before doing the real comparison.
I also would like to know if my approach can be considered safe.
Eventually I could also accept answers proposing a solution with the boost libraries since they are quite portable and I already know that i will use them for other reasons.
Thanks.