Alex Teo
hi need help with C++ programming!
1 Answer(s)      3 years and 3 months ago
Posted in : Java Beginners

how do i :

b. Store numbers / characters into the file (output)

c. Retrieve numbers / characters from the file (input

thanks!

Advertisement
View Answers

February 13, 2012 at 1:11 PM


b)

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}

c)

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}









Related Tutorials/Questions & Answers: