I have looked up what feels like every resource out there and I can't seem to find a solid answer to this question. Perhaps it's obvious, I am still new to C++.
I had the following functional main method:
int main()
{
char firstChar, secondChar;
cin >> firstChar;
cin >> secondChar;
cout << firstChar << " " << secondChar;
system("pause"); // to wait for user input; allows the user to see what was printed before the window closes
return 0;
}
This will cause the console to wait for input. The user inputs something. In this case (test)
. The output is:
( t
I would like to change this so that the input comes from a file and can execute the same way for each line rather than just once.
I tried many variations of the following:
int main(int argc, char* argv[])
{
ifstream filename(argv[0]);
string line;
char firstChar, secondChar;
while (getline(filename, line))
{
cin >> firstChar; // instead of getting a user input I want firstChar from the first line of the file.
cin >> secondChar; // Same concept here.
cout << firstChar << " " << secondChar;
}
system("pause"); // to wait for user input; allows the user to see what was printed before the window closes
return 0;
}
This merely runs the while loop once for every line in the file, but still requires input into the console and in no way manipulates the data in the file.
File contents:
(test)
(fail)
Desired automatic output (without making the user enter (test) and (fail)
manually:
( t
( f
Final Edit
After seeing the input I would do something like this
int main(int argc, char* argv[])
{
ifstream exprFile(argv[1]); // argv[0] is the exe, not the file ;)
string singleExpr;
while (getline(exprFile, singleExpr)) // Gets a full line from the file
{
// do something with this string now
if(singleExpr == "( test )")
{
}
else if(singleExpr == "( fail )") etc....
}
return 0;
}
You know what the full input is from the file so you can test the whole string at a time instead of character by character. Then just act accordingly once you have this string
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments