Reading Line

I have a file like:

1 Starting year
2010 Last year
example.100 Site file name
0 Labeling type
-1 Labeling year
-1.00 Microcosm
-1 CO2 Systems
-1 pH Effect
-1 Soil Warming
0 N input scalar option
0 OMAD scalar option
0 Climate scalar option
3 Initial system
KNZ Initial crop
TMDF Initial tree

Year Month Option
1 Block # Temperate_Forest
1896 Last year
1 Repeats # years
1896 Output starting year
1 Output month
0.083 Output interval
F Weather choice
example.wth
1 16 TREE
TMDF
1 16 TFST
1 350 TLST
-999 -999 X

I want my output file without the line "Year Month Option".
I am using the code as below, but I am not getting any output.

1
2
3
4
5
6
7
8
9
10
11
12
  ifstream schedule("schedule_1.txt");
	ofstream out("schedule_out.txt");

	string line;

	while(getline(schedule, line))
	{
		if(line != "Year Month Option")
		{
		out<<line<<endl;
		}
	}

line != "Year Month Option"

It is not the way to compare strings. Here you are trying to compare whether it is physically the same string at the same memory address.

Use "strcmp" function instead.

Also I do not believe you can check the return value of "getline" - according to documentation it returns its first parameter.
Last edited on
You can simply use the string member function compare() to compare it with a constant string:

1
2
if ( line.compare ( "Year Month Option" ) == 0 )
              out << line << endl;


EDIT Note that compare() returns 0 if the compared strings are equal,
and other value depending on the strings compared
Last edited on
thank you
Topic archived. No new replies allowed.