Thanks for this. Would anyone be able to show me an example of how to use this, and for the function's second paramater, is "EOF" what you type in to set the newline character as the delimiting character?
Thanks, it really does take a genius to see the obvious!
One more question: In this source code, why do i have to set n_chars_to_ignore to 3 and not 1 to get it to ignore the newline char? File to be read and code below:
void MapFunctions::MapRead(){
//Tells the game that this new map requires completion.
mapCompletion = 0;
//Constructing the file name.
stringstream ss;
ss << level << ".aem";
//Opening the file to be read.
ifstream LoadMap(ss.str().c_str());
//Loading the map dimensions.
LoadMap >> rows >> cols;
//Allocation of dynamically allocated array (map grid).
Map = newint*[rows];
for(int i = 0; i < rows; i++){
Map[i] = newint[cols];}
//Loading in each individual terrain type, enemy e.t.c. into map.
for(int j = 0; j < rows; j++){
for(int i = 0; i < cols; i++){
LoadMap >> Map[j][i];
}
}
//Loads text to be printed at start (and the size of the string containing it).
LoadMap >> StartTextSize;
StartText.resize(StartTextSize);
getline(LoadMap,StartText);
//Loads in the number of enemies and the number of stats that each enemy has.
LoadMap >> EnemyQuantity;
EnemyNames = new string[EnemyQuantity];
EnemyStats = newint*[EnemyQuantity];
for(int i = 0;i<EnemyQuantity;i++){
EnemyStats[i] = newint[NoOfEnemyStats];
}
//Load in each enemy name, followed by their stats.
for(int i = 0;i<EnemyQuantity;i++){
LoadMap.ignore(3, '/n');
getline(LoadMap,EnemyNames[i]);
for(int j = 0;j<NoOfEnemyStats;j++){
LoadMap >> EnemyStats[i][j];
}
}
//Closes the file.
LoadMap.close();
}
Checked the hidden characters in Notepad++ and a long story short, after some research there is two extra characters in a Windows newline. One extra in a Linux newline (I usually use Linux, but am using Windows right now). I am going to set the number of characters to skip equal to a variable, the value of which is determined by the OS on which the program is being compiled. How would I use a pre-processor directive to determine which OS is being used (I know of "#ifdef", but do not know how to tell it Windows or Linux afterwards).
The \r\n <-> \n conversion is handled for you unless you specify the ios::binary flag upon opening the file. Reading in converts \r\n to \n, and writing out converts \n to \r\n.
The \r\n <-> \n conversion is handled for you unless you specify the ios::binary flag upon opening the file. Reading in converts \r\n to \n, and writing out converts \n to \r\n.
Does this mean that while I'm using ios::in it should deal with that OS specific newline rubbish for me while reading in and writing to the file (I only need it to be read)?
It still wont automatically format it, but I have managed to use a pre-processor directive to tell the compiler how many characters to skip based on the OS. Thanks all for answers!