I have an assignment problem to solve. I need to write a C++ program which takes a text file as input and produces another text file as output. The problem says that I cannot store the input file or output file in RAM. What does this exactly mean?
#include <iostream>
#include <fstream>
#include<string>
usingnamespace std;
int main()
{
string r_content,w_content,test;
fstream outfile;
outfile.open ("data.txt",ios_base::out);
if (!outfile)
{
cout<<"Error writing to file!";
return 0;
}
else
{
cout<<"Please enter a string to be written to the file:\n";
getline(cin,w_content);
cout<<"Writing to file...\n";
//send data to file before closing
outfile<<w_content;
outfile.close();
}
fstream infile;
infile.open("data.txt",ios_base::in);
if (!infile)
{
cout<<"Error reading file!";
return 0;
}
else
{
cout<<"\nReading from file...\n";
//read data in before closing
getline(infile,r_content);
cout<<"The file contents are:\n";
cout<<r_content;
infile.close();
}
}
Changing the order of input/output and making one of them use a different file name may be what you need.
The problem says that I cannot store the input file or output file in RAM. What does this exactly mean?
It means that you'll have to read in, process, and write out to your files one portion at a time. Some I/O methods read in the whole input first and/or generate the whole output before writing, and that is not allowed for your assignment.