Hello, I have this C++ code for it: #include<iostream> #include<string> #include
ID: 3902131 • Letter: H
Question
Hello, I have this C++ code for it:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string inputfile, outputfile, strings[1000];
ifstream in;
ofstream out;
cout << "Enter input file name : ";
cin >> inputfile;
cout << "Enter output file name : ";
cin >> outputfile;
in.open(inputfile);
out.open(outputfile);
int count=0;
while (getline(in,strings[count]))
{
count++;
}
for (int i = count-1; i>=0; i--)
{
cout << strings[i] << endl;
}
in.close();
out.close();
return 0;
}
But my teacher said this is wrong. She said, "May not read whole file into memory - please read Assignment Instructions.???????????" Do you guys know of another way to do it? Thank you.
Write a program that reads each line in a file, reverses its lines, and writes them to another file. Suppose the user specifies input.txt and output.txt when prompted for the file names, and input.txt contains the lines Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go After the program is finished, output.txt should contain The lamb was sure to go And everywhere that Mary went Its fleece was white as snow Mary had a little lambExplanation / Answer
The while loop can be chnaged to, for better file read
while(!in.eof()) //While will continue untill the end of file using eof(), function
{
getline(in, strings[count]); //read from file into strings array
count++; //Increment the count to represent the rrows of strings
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.