Please help on this program for C++. Thank you! This short assignment is to furt
ID: 3890705 • Letter: P
Question
Please help on this program for C++. Thank you!
This short assignment is to further explore how binary files, and in particular, the read and write operations, work. Before, you defined a class with a int and a double data members, wrote an object of the class to a binary file, and read it back to a new object to see how by using read and write we can copy the whole object to the binary file and back. This time you are going to try the same exercise but see how it works when an object has string and character arrays as data member. We are also going to have 2 programs, one for writing into the file, and one for reading out.
First, define the class charStudent with 2 public data members: ID and name, where ID is an int and name is an array of characters of size 40 (will hold up to 39 characters plus the special ‘’ character signifying the end of the c-type string.) Just like before, declare an object of the class with some values, and write the object into a binary file. The second program should read from the binary file to an object and print out the values to see that the transfer worked correctly. You can also take a look at the file and see what is looks like. If you have not worked with character arrays very much, please read the lecture note “C-strings versus strings” to see how to work with them.
Next, define a class stringStudent with the same 2 public data members,
ID and name, but make name be data type string. Try the same exercise as before, of writing the first object to a binary file in one program, and reading from the file to an object in a second program, and printing out the values. Did the transfer work?
If it worked, try again, but enter a very long name with at least 30 characters. Did it still work? If not, why not? You can try looking at the contents of the file, once when you have a short name, and once when you have a long name.
Explanation / Answer
//****************************Program using Nmae as Character array***********************
#include <iostream>
#include <fstream>
//std.dat is the file used to store and retrive student data
#define FILE_NAME "std.dat"
using namespace std;
//class CharStudent declaration
class CharStudent {
public :
int ID;
char Name[40] ;
public :
//function to get the input data for CharStudent
void getData(){
char c;
cout<<"Please Enter CharStudent ID : " ;
cin>>ID;
cout<<"Please Enter NAME OF THE CharStudent one by one character till: ";
for(int i=0; i<39; i++){
cin>>c;
//Take the input till double enter is pressed
if(c==' ')
break;
Nmae[i]=c;
}
// Add '' at the end of the character array
Nmae[i]='';
}
//function to write CharStudent details
void showData(){
cout<<"CharStudent ID: "<<ID<<endl
//Print the name iterating character by character
for(int i=0; i<40; i++){
if(Name[i]=='')
break;
cout<<"CharStudent NAME: "<<Name[i];
}
}
};
int main(){
//object of CharStudent class
CharStudent std;
//get the CharStudent details from standard input
std.getData();
//write object into the file
fstream file;
file.open(FILE_NAME,ios::out|ios::binary);
if(!file){
cout<<"Error in creating file... ";
return -1;
}
file.write((char*)&std,sizeof(std));
file.close();
cout<<"Record added successfully into file ";
//open file again
file.open(FILE_NAME,ios::in|ios::binary);
if(!file){
cout<<"Error in opening file... ";
return -1;
}
if(file.read((char*)&std,sizeof(std))){
cout<<endl<<endl;
cout<<"Data extracted from file.. ";
//print the object
std.showData();
}
else{
cout<<"Error in reading data from file... ";
return -1;
}
file.close();
return 0;
}
//****************************Program using Nmae as String***********************
#include <iostream>
#include <fstream>
//std.dat is the file used to store and retrive student data
#define FILE_NAME "std.dat"
using namespace std;
//class CharStudent declaration
class CharStudent {
public :
int ID;
char Name[40] ;
public :
//function to get the input data for CharStudent
void getData(){
cout<<"Please Enter CharStudent ID : " ;
cin>>ID;
cout<<"Please Enter NAME OF THE CharStudent : ";
cin.getline(Name,39);
}
//function to write CharStudent details
void showData(){
cout<<"CharStudent ID: "<<ID<<endl
<<"CharStudent NAME: "<<Name<<endl
}
};
int main(){
//object of CharStudent class
CharStudent std;
//get the CharStudent details from standard input
std.getData();
//write object into the file
fstream file;
file.open(FILE_NAME,ios::out|ios::binary);
if(!file){
cout<<"Error in creating file... ";
return -1;
}
file.write((char*)&std,sizeof(std));
file.close();
cout<<"Record added successfully into file ";
//open file again
file.open(FILE_NAME,ios::in|ios::binary);
if(!file){
cout<<"Error in opening file... ";
return -1;
}
if(file.read((char*)&std,sizeof(std))){
cout<<endl<<endl;
cout<<"Data extracted from file.. ";
//print the object
std.showData();
}
else{
cout<<"Error in reading data from file... ";
return -1;
}
file.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.