ahmed0saber

Storing and using data from a text file in C++

Jun 6th, 2021
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main() {
  5.   string name , msg;
  6.  
  7.   //get data from file
  8.   ifstream ifs("aaa.txt");
  9.   string content( (istreambuf_iterator<char>(ifs) ),
  10.                        (istreambuf_iterator<char>()    ) );
  11.                        
  12.   //display messages
  13.   for(int i=0;i<content.length();i++)
  14.   {
  15.     if(content[i]==' ')
  16.     {
  17.         cout<<endl;
  18.     }
  19.     else
  20.     {
  21.         cout<<content[i];
  22.     }
  23.   }
  24.  
  25.   //enter a new msg
  26.   cout<<"\tName : ";
  27.   cin>>name;
  28.   cout<<"\tDon't use spaces\n\tMessage : ";
  29.   cin>>msg;
  30.    
  31.   //save it to the file
  32.   ofstream MyFile("aaa.txt");
  33.   MyFile<<(content+name+":"+msg+" ");
  34.   MyFile.close();
  35. }
Advertisement
Add Comment
Please, Sign In to add comment