I wrote some code to grab input from a text file. I know specifically the format of the text file and the program was made tailored to only that specific format. I know this is pretty bad practice and if anyone can point me in the right direction, that would be great. But first I'd like to address a bug I'm getting.
My program basically takes in a .txt file and echoes back the file in exactly the same format. The bug is that I get a last line of zeroes. Can anyone explain to me why?
Code:
//Copies everything in an input file to cout
//usage: executable file1
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main(int argc, char** argv)
{
ifstream fin(argv[1]);
string line;
if (fin)
{
int nodes = 0;
fin >> nodes;
cout << nodes << endl;
}
//Grabs edge1, edge2, and cost
while (fin)
{
//I need these to be int. I'm going to be doing something later with it.
int x = 0, y = 0, z = 0;
fin >> x >> y >> z;
cout << x << " " << y << " " << z << endl;
}
}
Here is the input for the .txt file.
Code:
50
0 1 14
1 2 13
3 9 11
9 11 5
1 2 3
Here is the output of the program.
Code:
50
0 1 14
1 2 13
3 9 11
9 11 5
1 2 3
0 0 0 //last line of zeroes