Can we do both in a function, while using the elements of input file, can we write the results in output file same time?
我们可以在一个函数中同时使用输入文件的元素,我们可以同时在输出文件中写入结果吗?
inside of while statement is true?
在while语句里面是真的吗?
void solve(string inputFileName, string outputFileName)
{
//declaring variables
string filename = inputFileName;
//Open a stream for the input file
ifstream inputFile;
inputFile.open( filename.c_str(), ios_base::in );
//open a stream for output file
outputfile = outputFileName;
ofstream outputFile;
outputFile.open(outputfile.c_str(), ios_base::out);
while(!inputFile.eof())
{
inputFile >> number; //Read an integer from the file stream
outputFile << number*100 << "\n"
// do something
}
//close the input file stream
inputFile.close();
//close output file stream
outputFile.close();
}
2 个解决方案
#1
2
while(!inputFile.eof())
doesn't work very well, because it tests if the previous operation failed, not if the next one will be successful.
不能很好地工作,因为它测试前一个操作是否失败,而不是下一个操作是否成功。
Instead try
相反,试试
while(inputFile >> number)
{
outputFile << number*100 << "\n"
// do something
}
where you test each input operation for success and terminate the loop when the read fails.
在哪里测试每个输入操作是否成功,并在读取失败时终止循环。
#2
1
You can, the input and output stream are independent from each other, so mixing them together in statements has no combined effect.
您可以,输入和输出流彼此独立,因此在语句中将它们混合在一起没有综合效果。
#1
2
while(!inputFile.eof())
doesn't work very well, because it tests if the previous operation failed, not if the next one will be successful.
不能很好地工作,因为它测试前一个操作是否失败,而不是下一个操作是否成功。
Instead try
相反,试试
while(inputFile >> number)
{
outputFile << number*100 << "\n"
// do something
}
where you test each input operation for success and terminate the loop when the read fails.
在哪里测试每个输入操作是否成功,并在读取失败时终止循环。
#2
1
You can, the input and output stream are independent from each other, so mixing them together in statements has no combined effect.
您可以,输入和输出流彼此独立,因此在语句中将它们混合在一起没有综合效果。