>
Code:
int main () { ifstream inStream; ofstream outStream; getInputOutputStreams(inStream, outStream); numberFile(inStream, outStream); return EXIT_SUCCESS; }
I have been stuck with an issue where the code I wrote compiles, but when running it has to be terminated because it just lags. I believe my issue may have to deal with not correctly returning the names of the input and output files, but I cannot figure out where I'm going wrong. I'm only a beginner in C++ (we are just now learning about arrays), so if you think this is a dumb question I'm sorry!
Here is the code I have written:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;
string getInputOutputStreams(ifstream &inStream, ofstream &outStream);
void numberFile(ifstream &inStream, ofstream &outStream);
int main ()
{
ifstream inStream;
ofstream outStream;
getInputOutputStreams(inStream, outStream);
numberFile(inStream, outStream);
return EXIT_SUCCESS;
}
string getInputOutputStreams(ifstream &inStream, ofstream &outStream)
{
string inputFile;
string outputFile;
cout << "Enter the name of the input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
while (inStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
}
inStream.close();
cout << "Enter the name of the output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
while (outStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
}
outStream.close();
return inputFile;
return outputFile;
}
void numberFile(ifstream &inStream, ofstream &outStream)
{
string inputFile;
string outputFile;
inStream.open(inputFile);
outStream.open(outputFile);
int lineNumber = 0;
string line;
while(!inStream.eof()) {
if(line == " ") {
}
else {
lineNumber++;
outStream << lineNumber << ": " << line << endl;
}
getline(inStream, line);
}
cout << lineNumber << " lines processed" << endl;
inStream.close();
outStream.close();
}
I see a number of mistakes:
getInputOutputStreams()
is closing the streams that it opens.
incorrect return
statements in getInputOutputStreams()
.
numberFile()
is re-opening the streams, using filename strings that have no values.
not ignoring blank lines, as the instructions ask for.
Try this instead
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void getInputOutputStreams(ifstream &inStream, ofstream &outStream);
void numberFile(ifstream &inStream, ofstream &outStream);
int main ()
{
ifstream inStream;
ofstream outStream;
getInputOutputStreams(inStream, outStream);
numberFile(inStream, outStream);
return EXIT_SUCCESS;
}
void getInputOutputStreams(ifstream &inStream, ofstream &outStream)
{
string inputFile;
string outputFile;
cout << "Enter the name of the input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
while (inStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an input file:" << endl;
cin >> inputFile;
inStream.open(inputFile);
}
cout << "Enter the name of the output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
while (outStream.fail()) {
cout << "Invalid file name." << endl;
cout << "Enter the name of an output file:" << endl;
cin >> outputFile;
outStream.open(outputFile);
}
}
void numberFile(ifstream &inStream, ofstream &outStream)
{
int lineNumber = 0;
string line;
while (getline(inStream, line)) {
if (line != "") {
++lineNumber;
outStream << lineNumber << ": " << line << endl;
}
}
cout << lineNumber << " lines processed" << endl;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.