I'm not entirely sure, but it looks like the compiler is having trouble with the way you are managing memory. Also, you are doing stuff twice when you only need to do it once. You won't save any memory by avoiding
vectors or
deques. Use them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
const char * MasterList = "OutputFile.txt";
string FilePath = "Maps\\";
string MapFileExt = ".tif";
int failure( const string& message )
{
cerr << message << endl;
return 1;
}
int main ()
{
// Read the file contents
deque <string> MasterListLines;
ifstream MasterListStream (MasterList);
if (!MasterListStream)
return failure( "Unable to open file." );
string TempString;
while (getline( MasterListStream, TempString ))
{
MasterListLines.push_back( TempString );
}
int NumberOfLines = MasterListLines.size();
if (!MasterListStream.eof())
failure( "WARNING: SOME ERROR OCCURRED!" );
MasterListStream.close();
// Use the contents to modify my filenames
for (int n=0; n<NumberOfLines; n+=4) // loops once for every four lines of input
{
// output old name
cout << (FilePath +MasterListLines[n+1]) << endl;
// output new name
cout << (FilePath +MasterListLines[n] +MapFileExt) << endl;
// separate stuff visually
cout << endl;
}
return 0;
}
|
I'm not sure exactly what you are trying to output, but the example I posted writes first the old filename then the new filename. I also stuck a blank line in there. The important point is that I used the + operator (which uses the .append() function) to stick stuff together.
Also notice how I used the deque to make things simple. It doesn't cost you any significant extra space (maybe 20 bytes or so) and avoids problems with dinking with pointers.
I would also like to draw your attention to line 26. This attempts to get a line (and does so if it can), then returns the ifstream object. The ifstream is then compared as a boolean value (which is the same as using .good()) to check for any stop condition (fail, bad, or eof). I could have written it as:
while (getline( MasterListStream, TempString ).good())
I don't use Dev-C++, but there should (hopefully) be an option in there somewhere to use "smart tabs". That will help clean up all the mis-aligned code.
Phew. Hope this helps.
Oh yeah, before I forget, .reserve() is useful if you know how much memory you will need before you actually use it, so that the string class doesn't have to do any fancy memory swapping whenever the string grows too large. That's not usually a problem, but if you need to optimize heavily for speed and/or size it is wonderful.
[edit] I think the reason he was having trouble is that opening the file in binary then reading to '\n' left him with a string ending in a '\r', which causes the 'print head' as it were, to return to the left side of the display. Hence, the string
appears to be missing the first part, since the first part is shorter than the second. If the second were shorter, the resulting output would look confused.