Skipping blank lines

Hello! I'm having troubles reading an input file that has three columns of numbers. Some of the values of the third columns are blank lines
How can I make the program skip those lines and process the next line without any problem?
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
#include <iostream>
#include <fstream>

using namespace std;

int main() {
	ifstream inputFile("input.txt");
	
	if (!inputFile) {
		cout << "File Doesn't Exist" << endl;
		system("pause");
		return -1;
	}

	ofstream outputFile("output.txt");

	int a, c;
	string b;


	while (inputFile >> a >> b >> c) {
		cout << a << " " << b << " " << c << endl;
		cout << endl;
	
	}
	
	inputFile.close();
	outputFile.close();


	system("pause");
	return 0;

}



input.txt

0 POP
1 PUSH 6
2 PUSH -21
3 POP
4 POP -56
5 POP 37

Try:

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
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main() {
	ifstream inputFile("input.txt");

	if (!inputFile) {
		cout << "File Doesn't Exist\n";
		//system("pause");
		return -1;
	}

	for (string line; getline(inputFile, line); ) {
		istringstream iss(line);

		int a, c;
		string b;

		if (iss >> a >> b >> c)
			cout << a << " " << b << " " << c << '\n';
	}

	//system("pause");
}



1 PUSH 6
2 PUSH -21
4 POP -56
5 POP 37

Last edited on
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main() {
	ifstream in("input.txt");
	if (!in) {
		cerr << "Cannot open input file.\n";
		return 1;
	}

	for (string line; getline(in, line); ) {
		istringstream ss(line);
		string op;
		int n, arg;

		if (ss >> n >> op)
		{
			cout << n << ' ' << op;
			if (ss >> arg)
				cout << ' ' << arg;
			cout << '\n';
		}
	}
}

Thank u! it worked
Topic archived. No new replies allowed.