Thanks for the replies!
I'll be linking the CRT statically for this simple program and I've taken a look at vectors and what you proposed. This way there are less I/O operations .
I've changed the main to one of the standard ones (and make use of argv[] to make the program reusable, the one before was the default one when you select a console app in VS 2010 (Beta 1). I've looked at it too, and for the records I'll post what I've founded (by default the standard syntax didn't produce an executable but a linker error):
In release configurations:
Code:
LIBCMT.lib(wcrt0.obj) : error LNK2001: unresolved external symbol _wmain
In debug configurations:
Code:
MSVCRTD.lib(wcrtexe.obj) : error LNK2019: unresolved external symbol _wmain referenced in function ___tmainCRTStartup
Cause (somebody wrote this as a response for an already open "bug" ticket for VS 2010):
Code:
This is NOT an issue with Visual Studio. In Visual C++,
the "main" method is "main" in ANSI and "wmain" in Unicode.
By default, Visual Studio sets your project to Unicode, so your
main function should be renamed "_tmain" (the macro _tmain
automatically adjusts to "wmain" or "main" depending on
whether the project is set to use Unicode).
So in the end I changed the character set under General in the project properties to "Use Multi-Byte Character Set". This way I can use the standard main syntax instead of _tmain that may not be pressent in other compilers.
Thanks for all your help.
The code I have right now:
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char** argv) {
if (argc < 2) {
// There are no file specified:
std::cerr << "n Please, specify the file.n";
return 1;
}
// Open the file:
std::ifstream in (argv[1]);
if (!in.is_open()) {
// If the file is not open show an error through stderr:
std::cerr << "n Failed to open the input file.n";
return 1;
}
std::string line;
std::vector<std::string> contents;
// Discard first line:
getline(in, line);
// Fill the vector with the resting lines:
while (getline(in, line)) {
contents.push_back(line);
}
// Close the file:
in.close();
// Open the file in write mode:
std::ofstream out (argv[1], std::ios::trunc);
if (!out.is_open()) {
// Something bad happened:
std::cerr << "n Failed to open the input file.n";
return 1;
}
// An iterator for the vector:
std::vector<std::string>::iterator current = contents.begin();
// Iterate in the vector to fill the output file:
for (current; current != contents.end(); current++) {
out << *current << std::endl;
}
// Finally close the output file and flush to disk:
out.close();
return 0;
}