COSC1315 Fundamentals of Programming
Instructor: Prof. Richard G. Baldwin
Assignment Number: 22
Object-Based program that illustrates file I/O and arrays.
Making certain that you comply with the General Requirements, write a C++ program that meets the following specifications:
BEGIN WRITTEN SPECIFICATIONS
This program must use Object-Based Programming Syntax.
Use your text editor to create a file named Asg22.txt that contains the following values each on a separate line:
0 1 2 3 4
Without making any changes to the function named doSomething below, other than to enter your name where indicated, write all of the additional code necessary to create an Object-Based program that will produce the screen output shown below. This program reads and writes the data from the file created above several times, reversing the order of the values each time the file is written.
//Begin program code
void doSomething(){
int data[5];
int fileSize = 5;
cout << "Put your name here" << endl;
//Read and display the data values from a file named
// Asg22.txt containing five integer values, each on
// a new line.
readFile(data,fileSize);
for(int cnt = 0;cnt < fileSize;cnt = cnt + 1){
cout << cnt << " " << data[cnt] << endl;
}//end for loop
//Write the same five integers into the same file
// in reverse order.
writeFile(data,fileSize);
//Read and display the file data again.
readFile(data,fileSize);
for(cnt = 0;cnt < fileSize;cnt = cnt + 1){
cout << cnt << " " << data[cnt] << endl;
}//end for loop
//Write the values into the same file again.
writeFile(data,fileSize);
//Read and display the five values one more time.
// They should be back in their original order now.
readFile(data,fileSize);
for(cnt = 0;cnt < fileSize;cnt = cnt + 1){
cout << cnt << " " << data[cnt] << endl;
}//end for loop
}//end doSomething function
//End program code
Program Output:
Put your name here Read data from file Asg22.txt 0 0 1 1 2 2 3 3 4 4 Write data into file Asg22.txt Write in reverse order Read data from file Asg22.txt 0 4 1 3 2 2 3 1 4 0 Write data into file Asg22.txt Write in reverse order Read data from file Asg22.txt 0 0 1 1 2 2 3 3 4 4
END WRITTEN SPECIFICATIONS
As specified in the General Requirements, copy the above specifications, including the two lines of upper-case characters and paste the specifications into a comment block at the beginning of your source code file.
Note: Sometimes when copying C++ source code into HTML documents (like this one) the code will become corrupted. This is particularly true for code that involves left and right angle brackets such as in:
#include <iostream>
if(x <= y){
Although Prof. Baldwin has made an effort avoid such problems, it is possible that some corruption of source code may have occurred during the creation of these HTML documents. If you see anything that you believe might be corrupted source code, please notify Prof. Baldwin so that he can look into the problem, correct the code if necessary, and make all of the students aware of the problem.
-end-