Hi there. I am trying to use ifstream inFS(inFile) to read in lines of a .txt file. Each line consists of either strings or integers. Ultimately I want to parse the contents of each line and write them to a formatted output, but I cannot even get a test case to work. I don't know why. Please refer to my code below.
Here is declaring the ifstream, opening it, and then attempting to read the first line. The filename comes from the command line and is vehicles.txt. Successfully, I get "vehicles.txt is open" in the terminal, but nothing after that, not even a newline, indicating that the while loop was never entered.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
ifstream inFS(inFile);
inFS.open(inFile);
if (!inFS.is_open())
{
cout << "Could not open file " << inFile << "." << endl;
return 1;
}
else
{
cout << inFile << " is open" << endl;
while (getline(inFS, line))
{
cout << line << endl;
}
}
|
To give you an idea, each line consists of something like the following: "car c111422 2014 Toyota Corolla White 2 cash" where each string or integer represents what will be a variable. The first word, "car", simply indicates that this line contains the information for a car, not a truck (which is represented by a separate class). Then you have the ID of the car, c111422, the year, make, model, color, number of doors, and payment method (not totally relevant for this, but mentioning it in case you need to know how I intend to parse each line's information).
Can someone please offer some input as to why the inFS is not even reading the first word, car?
EDIT: To clarify, if the line did not start with the word "car", the only alternative would be starting with the word "truck", in which case the line would contain slightly different information.