Ifstream help

Hello all!
When reading files, how is it possible to skip the next character in the file?
Thanks in advance.
Last edited on
ifstream::ignore()
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?
If your goal is to skip the next character in the stream as the original post suggests:

infile.ignore();

what you type in to set the newline character as the delimiting character?


Not surprisingly:

infile.ignore(n_chars_to_ignore, '\n');
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:

1.aem:
5 5 1 0 2 1 0 4 1 1 1 0 0 1 5 0 6 6 1 0 3 8 0 4 1 1 7
11 Test Text.
4
Goblin
4 5 4 6 3 6 6 3 1
Orc
5 6 7 7 2 7 7 4 1
Bat
6 2 3 2 0 3 3 0 1
Spirit
7 7 5 2 0 6 6 2 0




AemRandW.cpp:
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
53
54
55
56
57
58
59
60
61
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 = new int*[rows];
for(int i = 0; i < rows; i++){
		Map[i] = new int[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 = new int*[EnemyQuantity];
for(int i = 0;i<EnemyQuantity;i++){
EnemyStats[i] = new int[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();
}
I would guess you have a trailing space or two in your input file where you don't expect it.
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.
Last edited on
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)?
As long as you don't use ios::bin then yes.
Last edited on
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!
Topic archived. No new replies allowed.