Well we're on I/O streams dealing with reading and writing to a data file in my C++ class and im completely lost. I just got over having swine flu and I'm pretty far behind. My book doesn't give any great examples on writing to a data file so I figured I'd ask here.
The assignment is:
<i>Write a C++ program that accepts lines of text from the keyboard and writes each line to a file name text.dat until an empty line is entered. An empty line is a line with no text that is created by pressing the Enter (or Return) key.</i>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
usingnamespace std;
int main()
{
string filename = "text.dat";
string line;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail())
{
cout << "\nThe file was not found or is non-existant. \nPlease check to see if the file exists." << endl;
exit(1);
}
if (inFile.is_open())
{
if (line != "")
{
cout << "Enter a a line of text:" << endl;
cin >> line;
}
}
inFile.close();
return 0;
}
But when I run it, it doesn't actually do anything is just exits right away. Any help would be appreciated. Thank you =)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
usingnamespace std;
int main()
{
string filename = "text.dat";
string line;
ofstream outFile;
outFile.open(filename.c_str());
if (outFile.fail())
{
cout << "\nThe file was not found or is non-existant. \nPlease check to see if the file exists." << endl;
exit(1);
}
if (outFile.is_open())
{
do
{
cout << "Enter a a line of text:" << endl;
getline(cin,line);
}
while (line != "");
}
outFile.close();
return 0;
}