/* Create a file and print to a file instead of printing to the screen with the help of fstream directive. //stream: Flow of characters or other kind of data. Flow could be into the program (INPUT STREAM) or out of the program (OUTPUT STREAM). Input & output sources: Keyboard/ screen, file. WHAT YOU NEED: create a file input_file.dat and enter three numbers. */ #include #include #include using namespace std ; int main() { ifstream in_stream ; // input is being delivered to the program via a C++ construct known as a stream. ofstream out_stream ; // output via a stream . in_stream.open("input_file.dat");//read from file //open here means open for input out_stream.open("output_file.dat");//write to file//open here means open for output if (in_stream.fail()) { cout << "FAIL!"; exit(1); //causes the program to end! } int _1st, _2nd,_3rd; in_stream >> _1st >> _2nd >> _3rd ; cout<< "the sum of the first 3 \n" << "numbers in the input_file.dat \n" << "is " << (_1st + _2nd + _3rd) << endl; in_stream.close(); //very important to close the files which were opened. out_stream.close(); return 0; } /* Task: 1. Please generate an array of 1000 numbers between 0 and 50 and write the output to a file array.dat. 2. Now use the array.dat file as an input file and group these numbers into even and odd numbers. */