Don't like ads? PRO users don't see any ads ;-)

simple file Input/output

By: DarkShadowsX5 on May 7th, 2012  |  syntax: C++  |  size: 0.56 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main(){
  7.     ofstream outputFile("file.txt"); //ofstream will create the file if it doesnt exist, ifstream will not.
  8.     ifstream inputFile;
  9. char myString[50];
  10.     cout << "file.txt \n";
  11.     cout << "Enter a string: ";
  12.         cin.getline( myString, 50, '\n');
  13.     cout << "Writing string to file...\n";
  14. outputFile << "\n world";
  15. outputFile << myString;
  16.     outputFile.close();
  17. cout << "Reading string from file...\n";
  18.     inputFile.open("File.txt");
  19. cout << myString;
  20.     return 0;
  21. }