Saving to a new file
Apr 25, 2014 at 9:24pm UTC
I got the desired results from my code but I do not know how to save it to a new file....
The question was:
Write a program that skips leading whitespace characters in each line of the provided data file CPPHumor.txt. Save the results in a new file.
If someone could help me find out how to save into a new file that would be awesome!
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
ifstream file;
ofstream ofile;
string c;
int i=1;
file.open("CPPhumor.txt" );
if (file.fail())
{
cout<< "\nThe file was not opened" <<endl;
system("pause" );
exit(1);
}
cout<<"file was opened" <<endl;
while (file.good())
{
{
while (file.peek()==' ' )
file.get();
}
getline(file,c);
cout << (c) << endl;
}
system("pause" );
return 0;
}
Apr 25, 2014 at 10:58pm UTC
Writing to a file is like writing to std::cout.
Apr 25, 2014 at 11:15pm UTC
The file I'm bringing in as a text is an ifstream and the new file(altered original) is an ofstream correct?
Apr 25, 2014 at 11:35pm UTC
yes. Also why are you doing
1 2 3 4 5 6 7
while (file.good())
{
{
while (file.peek()==' ' )
file.get();
}
getline(file,c);
I would do
1 2 3 4
while (std::getline(file, c))
{
//do something with read information
}
Apr 26, 2014 at 12:12am UTC
I am kind of weary of using "std::", its foreign territory to me.
Topic archived. No new replies allowed.