How to read and write to a text file in C++?

后端 未结 4 710
野趣味
野趣味 2020-12-14 02:24

Hey everyone, I have just started to learn C++ and I wanted to know how to read and write to a text file. I have seen many examples but they have all been hard to understand

4条回答
  •  心在旅途
    2020-12-14 02:36

    To read you should create an instance of ifsteam and not ofstream.

    ifstream iusrfile;
    

    You should open the file in read mode.

    iusrfile.open("usrfile.txt", ifstream::in);
    

    Also this statement is not correct.

    cout<

    If you are trying to print the data you read from the file you should do:

    cout<

    You can read more about ifstream and its API here

提交回复
热议问题