Feb 12, 2013 at 7:00pm UTC
I am trying to understand how to save to a file and how to access the file and display to the screen as my cout<<, can someone do a step by step for me so I can practice this?? This is what I started but stuck now:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
main ()
{
//declare variables
ifstream inData;
ofstream outData;
string numString;
//open data file
inData.open("Prog5_1.txt");
outData.open("Prog5_1.txt");
//prompt the user
cout<< "Enter a series of numbers, example: 123456: " << endl;
cin >> numString;
inData >> numString;
if (inData.is_open())
{
while (inData.good())
{
cout << numString << endl;
}
}
return 0;
}
Feb 14, 2013 at 5:51pm UTC
Your close
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
main ()
{
//declare variables
ifstream inData;
ofstream outData;
string outString;
string line;
//open data file
outData.open("in.txt" );
if (outData.is_open())
{
//prompt the user
cout << "Enter a series of numbers, example: 123456: " ;
cin >> outString;
outData << outString;
outData.close();
}
//open data file
inData.open("in.txt" );
if (inData.is_open())
{
while (inData.good())
{
getline(inData,line);
cout << line << endl;
inData.close();
}
}
return 0;
}
Last edited on Feb 14, 2013 at 5:52pm UTC