How to write to variables str1 , str2 , etc. of type string strings from a file, separated by a newline character (in this case there may be other separating characters, for example, spaces)?

    1 answer 1

     #include <string> #include <fstream> #include <iostream> using namespace std; void main() { string str = "aa aa\nbb bb\n"; ofstream out("C:\\1.txt"); out << str; out.close(); string str1; string str2; ifstream in("C:\\1.txt"); getline(in, str1); getline(in, str2); in.close(); cout << str1 << endl; cout << str2 << endl; } 

    or

     vector<string> ArrStr; string temp; ifstream in("C:\\1.txt"); while(getline(in, temp)) ArrStr.push_back(temp); in.close(); for(int i = 0; i < (int)ArrStr.size(); ++i) cout << ArrStr[i] << endl;