C++ cin with file instead of user input

leigero

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
const_ref

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ cin with file instead of user input

From Dev

C++ cin input to file

From Dev

cin not accepting user input in C++

From Dev

C++: How to redirect from a file to cin and display as if user typed the input

From Dev

File path as user input in C

From Dev

C Program file-io user input

From Dev

Write user input string to file in C

From Dev

C Program file-io user input

From Dev

Save user input to a text file c++

From Dev

C (Basic stuff) - Write User Input to a File

From Dev

opening a file based on user input c++

From Dev

Pipe an input to C++ cin from Bash

From Dev

getline(cin,input) with multiple lines? (C++)

From Dev

C++ fast cin input streaming

From Dev

C++ std::cin producing extra input

From Dev

Using cin for keyboard input after processing redirected file input with getline

From Dev

C++ File Input/Output Outputting Numbers Instead of Chars

From Dev

C# visual studio user input for file path and file name

From Dev

Interrupt cin while loop without the user entering input

From Dev

Why does std::cin's extractor operator wait for user input?

From Dev

write user input to a file

From Dev

User input into file path

From Dev

How to read a .txt file from user input in C

From Dev

Replace instead of append based on user input?

From Dev

Hardcode file path instead of manual file input

From Dev

Hardcode file path instead of manual file input

From Dev

C++: how do I remove bad input from cin?

From Dev

C++ cin input stored directly to pass by reference variable - dangerous?

From Dev

How do I write to cin, the input stream, in C++