C++ cin with file instead of user input

19,571

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

Share:
19,571
leigero
Author by

leigero

I left a lucrative career in consulting for defense contracting companies to pursue my passion for software development. Making my dream a reality involved years of schooling and living like a pauper, but I finally graduated with a BS in Computer Science. I'm now pursuing my master's in software engineering and working full time as a software engineer. I couldn't be happier. My career path is what I made it. I wanted to be a developer, so I worked to be a developer and achieved the goal which has brought me unending satisfaction. I enjoy hanging out in places like this solving problems and helping others to learn. When I learned such resources were not so readily available and I enjoy helping others.

Updated on June 04, 2022

Comments

  • leigero
    leigero almost 2 years

    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
    
  • leigero
    leigero about 10 years
    There is one expression per line, but they can be rather complex so it's not so simple as just x + y. there can be nested ones as well etc. Previously I just handled this by extracting characters with cin >> charVariable and that would pull out the leftmost character. When it is a string, and not input, it changes everything.
  • const_ref
    const_ref about 10 years
    Please edit your question to show an example of the file layout
  • leigero
    leigero about 10 years
    using cin >> charVariable causes the console to sit there waiting on user input. I want to use this same concept with an existing string. Something like singleExpr >> charVariable but that doesn't work.
  • const_ref
    const_ref about 10 years
    No but you can iterate over the string character by character. Please paste in a copy of your file and I will edit my answer accordingly
  • const_ref
    const_ref about 10 years
    @leigero See my edit for iterating over a string character by character. It sounds like behaviour like this that you desire
  • const_ref
    const_ref about 10 years
    cppreference.com is the preferred site for referencing. The site you mention is rather poor
  • leigero
    leigero about 10 years
    With the edited question's file contents paren = filename.get(); prints out a dollar sign. The file's contents are (test)
  • Abhishek Bagchi
    Abhishek Bagchi about 10 years
    Works for me. I get (, and not $