Reading from file

I have to read the first two lines of a file and discard them,

so the second line in the file contains an integer which is the size of the file.
I need to ignore the first line, and then read in the second line.

However, when I do this, as a test I output what I read on the second line and it's a bunch of garbage.

The text file:
April 20, 2014
50
...
...
...
...
The 50 is what I'm trying to read in

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	float *arrayH;
	float *arrayL;
	int size = 0;
	openFile(size); // to find the size

void openFile(int & s)
{
	string date;
	string size;
	ifstream f;
	f.open(file);
	getline(f, date);
	f >> size;
	f.close();
	cout << size << endl;
	

}


You need to test whether the file could be successfully opened. If not return 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	float *arrayH;
	float *arrayL;
	int size = openFile(); // to find the size

int openFile()
{
int result = 0;

	string date;
	string size;
	ifstream f;
	f.open(file);
if(f.is_open())
{
	getline(f, date);
	f >> result;
	f.close();
	cout << result<< endl;
}
return result;
}


Generally you should check if all went well with the stream before you use the data:
When I use your method, I get 0 as the output

But I should be getting 50

Is it because in the text file, it is a char and since im reading it as an int, there is a loss in conversion?
If you have file "text.txt" at the same place where you .cpp file it should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int result = 0;

	string date;
	
	ifstream f;
	f.open("text.txt");//filename, must be here

	if (f.is_open())
	{
		
		getline(f,date);
		f >> result;
		f.close();
		cout << result << endl;
	}
Topic archived. No new replies allowed.