私がここで抱えている問題を解決してくれる人がいることを願っていました。私のプログラムは以下のとおりです。私が抱えている問題は、一連の乱数を含む .txt ファイルを取得し、数値を読み取り、正の数値のみを出力する process() 関数の書き方がわからないことです。別ファイルに。私はこれで何日も立ち往生しており、他にどこを向くべきかわかりません。誰かが何らかの助けを提供できれば、私は非常に感謝しています、ありがとう.
/*
10/29/13
Problem: Write a program which reads a stream of numbers from a file, and writes only the positive ones to a second file. The user enters the names of the input and output files. Use a function named process which is passed the two opened streams, and reads the numbers one at a time, writing only the positive ones to the output.
*/
#include <iostream>
#include <fstream>
using namespace std;
void process(ifstream & in, ofstream & out);
int main(){
char inName[200], outName[200];
cout << "Enter name including path for the input file: ";
cin.getline(inName, 200);
cout << "Enter name and path for output file: ";
cin.getline(outName, 200);
ifstream in(inName);
in.open(inName);
if(!in.is_open()){ //if NOT in is open, something went wrong
cout << "ERROR: failed to open " << inName << " for input" << endl;
exit(-1); // stop the program since there is a problem
}
ofstream out(outName);
out.open(outName);
if(!out.is_open()){ // same error checking as above.
cout << "ERROR: failed to open " << outName << " for outpt" << endl;
exit(-1);
}
process(in, out); //call function and send filename
in.close();
out.close();
return 0;
}
void process(ifstream & in, ofstream & out){
char c;
while (in >> noskipws >> c){
if(c > 0){
out << c;
}
}
//This is what the function should be doing:
//check if files are open
// if false , exit
// getline data until end of file
// Find which numbers in the input file are positive
//print to out file
//exit
}