Getting input from external files?

后端 未结 4 2047
我在风中等你
我在风中等你 2021-01-28 11:36

I need to get very basic input from an external file in C++. I tried searching the internet a few times but nothing really applied to what I need. This would be a .txt file that

4条回答
  •  耶瑟儿~
    2021-01-28 11:54

    You can open a file for input with std::ifstream from the header , then read from it as you would from std::cin.

    int main()
    {
        std::ifstream input("somefile.txt");
        int a;
        input >> a;  // reads a number from somefile.txt
    }
    

    Obviously, you can use >> in a loop to read multiple numbers.

提交回复
热议问题