I have a .txt file and I'm expecting to get a certain line format from it that contains data that I need to keep.
For instance:
input.txt:
Number: 654
Name: Alon
-----
I need to:
1. extract the 654 and "Alon" into their appropriate variables.
2. throw an error if the format is not exact.
If this was C I would probably use:
if (fscanf(inputFile, "Number: %d", &num) == 0)
{
// raise an error
}
Assuming using C's functions are not a good idea, I'm left with std::cin that might give me access to the data that I need to extract, but no control over the exact format of the string wrapping the data.
I have already opened and read the file as an "ifstream" using . I've also retrieved the first line using std::getline(...). This is what I've got:
std::ifstream inputFile;
string lineToParse;
inputFile.open("input.txt", std::fstream::in);
if (inputFile.fail())
{
// throw exception
}
else
{
std::getline(inputFile, lineToParse);
int data;
inputFile >> data;
}
Assuming input.txt is the file above, I expect lineToParse to be "Number: 654" and data to be 654. But as I said, I gain no control over the format of the line this way.
Any ideas?