Zasito
Zasito

Reputation: 279

Read input from .txt in C++

I've got a .cpp which is prompted as follows:

$ ./program file < file.txt

Then I want to use the text on the file.txt for some functions inside my program. How can I access the input on the .txt on my .cpp?

stdin? cin? could you put some examples?

Upvotes: 0

Views: 129

Answers (2)

Ahmed KRAIEM
Ahmed KRAIEM

Reputation: 10427

You must use std::cin

#include <iostream>
#include <string>

int main() {
    for (std::string line; std::getline(std::cin, line);) {
        std::cout << line << std::endl;
    }
    return 0;
}

Upvotes: 3

Nagasaki
Nagasaki

Reputation: 58

you can use ifstream to open your file and getline function to read it line by line. You don't need to use < to pass param to your program. The param can be get in the argv array of your main function

Upvotes: 0

Related Questions