Can't able to read the binary file [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
// STRUCTURE FOR ADD NEW DRIVER'S RECORD
struct driver
{
int long reg;
string name;
string fhname;
string peraddress;
string pesentaddress;
string dob;
string cnic;
string qualification;
string occupation;
string phone;
string mobile;
string licnum;
string email;
string city;
string gender;
string province;
string lictype;
string vitype;
}e[100];
// FUNCTION FOR ADD NEW RECORD OF DRIVER
void add_driver_record()
{
system("CLS");
char option;
fstream driverTextFile, addrecord;
addrecord.open("rcd.data", ios::out | ios::app | ios::binary);
cout << "Do you Want to Enter New Record (Y/N) ";
option = _getch();
system("cls");
if (option == 'Y' || option == 'y')
{
for (int i = 1; i == 1; i++)
{
while (option != 'N')
{
cout << "\t\t Driver Information " << endl;
cout << "\t\t++++++++++++++++++++" << endl;
cout << "\t\tEnter Token No :";
cin >> e[i].reg;
cout << "\t\tEnter License Number :";
cin.ignore();
getline(cin, e[i].licnum);
cout << "\t\tEnter Full Name :";
getline(cin, e[i].name);
cout << "\t\tEnter Father/Husband Name :";
getline(cin, e[i].fhname);
cout << "\t\tEnter Permanent Address :";
getline(cin, e[i].peraddress);
cout << "\t\tEnter Present Address :";
getline(cin, e[i].pesentaddress);
cout << "\t\tEnter Date of Birth :";
getline(cin, e[i].dob);
cout << "\t\tEnter CNI :";
getline(cin, e[i].cnic);
cout << "\t\tEnter city :";
getline(cin, e[i].city);
cout << "\t\tEnter Province :";
getline(cin, e[i].province);
cout << "\t\tEnter Gender :";
cin >> e[i].gender;
cout << "\t\tEnter Occupation :";
cin >> e[i].occupation;
cout << "\t\tEnter Qaulification :";
cin.ignore();
getline(cin, e[i].qualification);
cout << "\t\tEnter E-mail :";
cin.ignore();
getline(cin, e[i].email);
cout << "\t\tEnter Phone No :";
cin >> e[i].phone;
cout << "\t\tEnter Mobile No :";
cin >> e[i].mobile;
cout << "\t\tEnter License Type :";
cin.ignore();
getline(cin, e[i].lictype);
cout << "\t\tEnter Vehicle Type :";
cin.ignore();
getline(cin, e[i].vitype);
addrecord.write((char *)&e, sizeof(driver));
driverTextFile << "========================================================\n";
driverTextFile << "\t\t Driver Information " << endl;
driverTextFile << "\t\t+++++++++++++++++++++++" << endl;
driverTextFile << "\t\t Token No :" << e[i].reg << endl;
driverTextFile << "\t\t License Number :" << e[i].licnum << endl;
driverTextFile << "\t\t Full Name :" << e[i].name << endl;
driverTextFile << "\t\t Father/Husband Name :" << e[i].fhname << endl;
driverTextFile << "\t\t Permanent Address :" << e[i].peraddress << endl;
driverTextFile << "\t\t Present Address :" << e[i].pesentaddress << endl;
driverTextFile << "\t\t Date of Birth :" << e[i].dob << endl;
driverTextFile << "\t\t CNIC :" << e[i].cnic << endl;
driverTextFile << "\t\t City :" << e[i].city << endl;
driverTextFile << "\t\t Province :" << e[i].province << endl;
driverTextFile << "\t\t Gender :" << e[i].gender << endl;
driverTextFile << "\t\t Occupation :" << e[i].occupation << endl;
driverTextFile << "\t\t Qaulification :" << e[i].qualification << endl;
driverTextFile << "\t\t E-mail :" << e[i].email << endl;
driverTextFile << "\t\t Phone No :" << e[i].phone << endl;
driverTextFile << "\t\t Mobile No :" << e[i].mobile << endl;
driverTextFile << "\t\t Enter License Type :" << e[i].lictype << endl;
driverTextFile << "\t\t Enter Vehicle Type :" << e[i].vitype << endl;
driverTextFile << "========================================================\n";
addrecord.close();
cout << "Do you Want to Enter New Record (Y/N) ";
cin >> option;
switch (option) {
case 'Y':
case 'y':
add_driver_record();
break;
case 'N':
case 'n':
exit(1);
break;
default:
printf("Please select suitable option \n");
add_driver_record();
break;
}
} // while Loop ends
}// for loop ends
} // if condition ends
else if (option == 'N' || option == 'n')
{
printf("Please Select Suitable Option \n");
}
}
// FUNCTION FOR DISPLAY ALL RECORDS OF DRIVER'S
void display_all_driver()
{
system("CLS");
system("color f5");
fstream myfile, driverTextFile;
myfile.open("rcd.data", ios::in | ios::out | ios::binary);
int idx = 1;
while (idx == 1)
{
myfile.read((char*)&e[idx].reg, sizeof(e[idx].reg));
myfile.read((char*)&e[idx].licnum, sizeof(e[idx].licnum));
myfile.read((char*)&e[idx].name, sizeof(e[idx].name));
myfile.read((char*)&e[idx].fhname, sizeof(e[idx].fhname));
myfile.read((char*)&e[idx].peraddress, sizeof(e[idx].peraddress));
myfile.read((char*)&e[idx].pesentaddress, sizeof(e[idx].pesentaddress));
myfile.read((char*)&e[idx].dob, sizeof(e[idx].dob));
myfile.read((char*)&e[idx].cnic, sizeof(e[idx].cnic));
myfile.read((char*)&e[idx].city, sizeof(e[idx].city));
myfile.read((char*)&e[idx].province, sizeof(e[idx].province));
myfile.read((char*)&e[idx].gender, sizeof(e[idx].gender));
myfile.read((char*)&e[idx].occupation, sizeof(e[idx].occupation));
myfile.read((char*)&e[idx].qualification, sizeof(e[idx].qualification));
myfile.read((char*)&e[idx].email, sizeof(e[idx].email));
myfile.read((char*)&e[idx].phone, sizeof(e[idx].phone));
myfile.read((char*)&e[idx].mobile, sizeof(e[idx].mobile));
myfile.read((char*)&e[idx].lictype, sizeof(e[idx].lictype));
myfile.read((char*)&e[idx].vitype, sizeof(e[idx].vitype));
cout << "========================================================\n";
cout << "\t\t Driver Information " << endl;
cout << "\t\t+++++++++++++++++++++++" << endl;
cout << "\t\t Token No :" << e[idx].reg << endl;
cout << "\t\t License Number :" << e[idx].licnum << endl;
cout << "\t\t Full Name :" << e[idx].name << endl;
cout << "\t\t Father/Husband Name :" << e[idx].fhname << endl;
cout << "\t\t Permanent Address :" << e[idx].peraddress << endl;
cout << "\t\t Present Address :" << e[idx].pesentaddress << endl;
cout << "\t\t Date of Birth :" << e[idx].dob << endl;
cout << "\t\t CNIC :" << e[idx].cnic << endl;
cout << "\t\t City :" << e[idx].city << endl;
cout << "\t\t Province :" << e[idx].province << endl;
cout << "\t\t Gender :" << e[idx].gender << endl;
cout << "\t\t Occupation :" << e[idx].occupation << endl;
cout << "\t\t Qaulification :" << e[idx].qualification << endl;
cout << "\t\t E-mail :" << e[idx].email << endl;
cout << "\t\t Phone No :" << e[idx].phone << endl;
cout << "\t\t Mobile No :" << e[idx].mobile << endl;
cout << "\t\t Enter License Type :" << e[idx].lictype << endl;
cout << "\t\t Enter Vehicle Type :" << e[idx].vitype << endl;
cout << "========================================================\n";
idx++;
}
myfile.close();
}
I am so sorry if somebody else had already asked this question. But I am unable to rectify the problem that I "can not" read the binary file on console. Let me tell what my code is doing. I took structure with bunch of attribute and taking the input in that and saving that data in Binary file. And i want to read that in second method. But I am completely unable to read the data. This is my code please provide me solution. I have already tried several ways but I am still unable. Your help will save me semester project.

Your struct driver consists of std::strings, which internally represent the actual content trough a pointer to dynamically allocated memory. When you write such a struct (or an array of such structs) as plain bytes to a file, you actually write out "memory addresses" and not the actual content of the strings. So you will loose the actual content, and reading in the file will give you plain garbage.
Try the following program illustrating this:
struct TestStruct {
string testString;
};
int main(){
TestStruct ts;
cout << "sizeof ts with empty string: " << sizeof(ts) << endl;
ts.testString = "Will not get larger, right?";
cout << "sizeof ts with string set to some content: " << sizeof(ts) << endl;
}
Output:
sizeof ts with empty string: 24
sizeof ts with string set to some content: 24

Do you have read permissions for the file? That is my first thought that it is a permissions issue.

Related

Problems reading edited data after modifying data file using fstream in C++

I'm currently making a record system for ticket booking as an assignment and facing problems when I want the data to read after modifying my file. I saw there were 2 options for me: eof or read in fstream.
The code for my modify records:
ifstream inFile;
ofstream outFile;
inFile.open("movieRecord.dat");
outFile.open("temp.dat");
inFile >> item.movieName >> item.movieTime >> item.hallNum >> item.moviePrice;
cout << "\tPlease Enter the Movie to Edit:";
cin.ignore();
cin.getline(editMovie,20);
titleMatch = strcmp (editMovie,item.movieName);
while (!inFile.eof()){
if (editMovie == item.movieName){
break;
}
else{
outFile << item.movieName << " " << item.movieTime << " " << item.hallNum << " " << item.moviePrice << endl;
break;
}
}
cout << "\tEditing " << editMovie << "..." << endl;
cin.clear();
cout << "\tReInsert Movie Title: ";
cin.getline(movieTitle,20);
cin.clear();
cout << flush;
cout << "\tEnter Time(ie:15.00): ";
cin >> movieTime2;
cout << flush;
cout << "\tEnter Hall Number for Movie: Hall ";
cin >> hallNumber;
cout << flush;
cout << "\tEnter Price of Movie Ticket: RM";
cin >> ticketPrice;
outFile << movieTitle << " " << movieTime2 << " " << hallNumber << " " << ticketPrice;
outFile.close();
inFile.close();
remove ("movieRecord.dat");
rename ("temp.dat","movieRecord.dat");
cout << "\tEdit Saved!" << endl;
system("pause");
goto adminMenu;
}
My code for reading the file:
ifstream movieCheck;
movieCheck.open("movieRecord.dat",ios::in);
movieCheck >> item.movieName >> item.movieTime >> item.hallNum >> item.moviePrice;
if (!movieCheck.good()){
cout << "File Open Error!" << endl;
}
while (movieCheck.read((char*)&item,sizeof(item))){
cout << "\tMovie title: " << item.movieName << endl;
cout << fixed;
cout << "\tTime: " << setprecision(2) << item.movieTime << endl;
cout << "\tHall Number: Hall " << item.hallNum << endl;
cout << "\tMovie Ticket Price: RM " << setprecision(2) << item.moviePrice << endl;
cout << "\t==============================================" << endl;
movieCheck >> item.movieName >> item.movieTime >> item.hallNum >> item.moviePrice;
}
movieCheck.close();
cout << endl << "\t The End of the Movie Summaries!" << endl;
When I use eof, the output will only show the first data only, simple example:
Desired Output:
Hello
Help2 \The edited record
Current Output:
Hello \does not show the edited record
When using read function however, my output is either similar to eof or it would not show at all.

How to change the program to enter the data with spaces while using fstream

void Motherboards::add()
{
char x;
int X;
fstream InFileMB("Motherboard_List.txt", ios::in | ios::out | ios::app);
if (!InFileMB)
{
cerr << "Error: Opening File Failed !!";
}
else
{
MotherBoardEntry:
system("cls");
cout << " Enter the name of the Manufacturer :\n";
string MB__Name_Manufacturer;
cin >> MB__Name_Manufacturer;
cout << " Enter Chipset Type :\n";
string MB_Chip;
cin >> MB_Chip;
cout << "Enter Name of the board exactly as it`s written on the box :\n";
string MB_Name;
cin >> MB_Name;
cout << "Enter 3 features of this board :\n";
cout << "1.) ";
string MB_Feature1;
cin >> MB_Feature1;
cout << endl;
cout << "2.) ";
string MB_Feature2;
cin >> MB_Feature2;
cout << endl;
cout << "3.) ";
string MB_Feature3;
cin >> MB_Feature3;
cout << endl;
cout << "Enter Price :\n";
string MB_Price;
cin >> MB_Price;
system("cls");
cout << "Please check the details before conformation :\n";
cout << "Motherboard :\n";
cout << MB__Name_Manufacturer << endl;
cout << MB_Chip << endl;
cout << MB_Name << endl;
cout << "Features :\n";
cout << "1.) " << MB_Feature1 << endl;
cout << "2.) " << MB_Feature2 << endl;
cout << "3.) " << MB_Feature3 << endl;
cout << "Price :\n";
cout << MB_Price;
cout << "\n\n Do You Want To Add The Following Motherboard To Your Stock List ? (y/n)";
cin >> x;
system("cls");
switch (x)
{
case 'y':
InFileMB << "Motherboard :\n" << MB__Name_Manufacturer << endl << MB_Chip << endl << MB_Name << endl << "Price :\n" << MB_Price << endl << "Features :\n" << "1.) " << MB_Feature1 << endl << "2.) " << MB_Feature2 << endl << "3.) " << MB_Feature3 << endl << endl;
break;
case 'Y':
InFileMB << "Motherboard :\n" << MB__Name_Manufacturer << endl << MB_Chip << endl << MB_Name << endl << "Price :\n" << MB_Price << endl << "Features :\n" << "1.) " << MB_Feature1 << endl << "2.) " << MB_Feature2 << endl << "3.) " << MB_Feature3 << endl << endl;
break;
case 'n':
system("cls");
system("pause,2");
cout << " Enter ( 1 ) to try to enter the data again or ( 2 ) to go back to item selection list .. ";
cin >> X;
switch (X)
{
case 1:
goto MotherBoardEntry;
break;
case 2:
break;
}
break;
case 'N':
system("cls");
system("pause,2");
cout << " Enter ( 1 ) to try enter the data again or ( 2 ) to go back to item selection list .. ";
cin >> X;
switch (X)
{
case 1:
goto MotherBoardEntry;
break;
case 2:
break;
}
break;
}
}
}
When I input the data as I am going to show in the snapshots that I provide when I enter the data without any spaces all goes well but when i do enter the data with spaces i cant fill out certain data due to some reason that I don't know as I am a beginner. Can you please tell me exactly where to update and what to update.
[enter image description here][1]
[enter image description here][2]
As you may have noticed in the picture where i have put a space between i7 and 4790k i couldn`t fill out the information for entering the name of the processor and the command window shifted me directly to the next line.

how can I print a char array in c++

I am working on a simple hotel reservsation Project but I couldn't print the customer name and payment type after entering their names'. Here is the piece of code that I typed.
cout << "Please enter the customer name: " << endl;
cin >> customername1;
cin.getline(customername1,100,'\n');
fflush(stdin);
cout << "Please enter the payment type: " << endl;
cin >> paymenttype;
cin.getline(paymenttype,100,'\n');
fflush(stdin);
cout << "How many days would you like to stay: " << endl;
cin >> days;
room_income = days * 30;
count_room++ ;
count_customer1++ ;
customer_name1[single]= new char [strlen(customername1)+1];
strcpy(customer_name1[single],customername1);
payment_type[single]= new char [strlen(paymenttype)+1];
strcpy(payment_type[single],paymenttype);
cout << "Room number : " << single << endl<< endl;
cout << "Customer name : "<< customer_name1 << endl << endl;
cout << "Payment type : "<< payment_type << endl<< endl;
cout << "Number of day for accomodation :"<< days << endl<< endl;
cout << "Income for this room : "<< room_income<< endl<< endl;
Some random numbers and letters are displayed for the customer name and payment type. How can I write them properly?
For the customer name and payment type, you try to print the array, instead of one element. Since the array is basically just a pointer to the first element, you will get a 'random number', which is the memory address.
Try:
cout << "Customer name : "<< customername1 << endl << endl;
cout << "Payment type : "<< paymenttype << endl<< endl;
And when you've got that working, follow the comments and look into std::string and vectors...

C++ GPA Trunc for 10 Students Input

I am able to enter the first student, however, once I request the second student's input it does not accept the: getline(cin, s2) on to getline(cin,s10).
Could you please help me understand where the mistake is made or why the output is not following the same format as s1/GPA1?
Here is my code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Welcome
cout << "Welcome to your personal GPA Calculator! \n" << endl;
float GPA1, GPA2, GPA3, GPA4, GPA5, GPA6, GPA7, GPA8, GPA9, GPA10 = 0;
string s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;
//Requesting User Input
cout << "Student One| Please enter your Last Name, First Name: " << endl;
getline(cin, s1);
cout << "What is your current GPA: " << endl;
cin >> GPA1;
cout << endl;
cout << "Student Two| Please enter your Last Name, First Name: " << endl;
getline(cin, s2);
cout << "What is your current GPA: " << endl;
cin >> GPA2;
cout << endl;
cout << "Student Three| Please enter your Last Name, First Name: " << endl;
getline(cin, s3);
cout << "What is your current GPA: " << endl;
cin >> GPA3;
cout << endl;
cout << "Student Four| Please enter your Last Name, First Name: " << endl;
getline(cin, s4);
cout << "What is your current GPA: " << endl;
cin >> GPA4;
cout << endl;
cout << "Student Five| Please enter your Last Name, First: " << endl;
getline(cin, s5);
cout << "What is your current GPA: " << endl;
cin >> GPA5;
cout << endl;
cout << "Student Six| Please enter your Last Name, First Name: " << endl;
getline(cin, s6);
cout << "What is your current GPA: " << endl;
cin >> GPA6;
cout << endl;
cout << "Student Seven| Please enter your Last Name, First Name: " << endl;
getline(cin, s7);
cout << "What is your current GPA: " << endl;
cin >> GPA7;
cout << endl;
cout << "Student Eight| Please enter your Last Name, First Name: " << endl;
getline(cin, s8);
cout << "What is your current GPA: " << endl;
cin >> GPA8;
cout << endl;
cout << "Student Nine| Please enter your Last Name, First Name: " << endl;
getline(cin, s9);
cout << "What is your current GPA: " << endl;
cin >> GPA9;
cout << endl;
cout << "Student Ten| Please enter your Last Name, First Name: " << endl;
getline(cin, s10);
cout << "What is your current GPA: " << endl;
cin >> GPA10;
cout << endl;
//Store in Tabular Format
cout << ("Student Name| \t\t |Student GPA Value \n");
cout << ("____________________________________________\n");
std::cout << s1 << "\t\t " << std::setprecision(2) << std::fixed << GPA1 << endl;
std::cout << s2 << "\t\t " << std::setprecision(2) << std::fixed << GPA2 << endl;
std::cout << s3 << "\t\t " << std::setprecision(2) << std::fixed << GPA3 << endl;
std::cout << s4 << "\t\t " << std::setprecision(2) << std::fixed << GPA4 << endl;
std::cout << s5 << "\t\t " << std::setprecision(2) << std::fixed << GPA5 << endl;
std::cout << s6 << "\t\t " << std::setprecision(2) << std::fixed << GPA6 << endl;
std::cout << s7 << "\t\t " << std::setprecision(2) << std::fixed << GPA7 << endl;
std::cout << s8 << "\t\t " << std::setprecision(2) << std::fixed << GPA8 << endl;
std::cout << s9 << "\t\t " << std::setprecision(2) << std::fixed << GPA9 << endl;
std::cout << s10 << "\t\t " << std::setprecision(2) << std::fixed << GPA10 << endl;
return (0);
}
You generally don't want to read numbers directly with cin for interactive use. It doesn't play well with newlines -- leaving the newline for the next entry. In this case, you get a blank name for person 2.
Try this:
string temp;
//Requesting User Input
cout << "Student One| Please enter your Last Name, First Name: " << endl;
getline(cin, s1);
cout << "What is your current GPA: " << endl;
getline(cin, temp);
GPA1 = strtof(temp.c_str(), 0);
cout << endl;
You have to be careful when you mix cin and getline(cin, "string"). Cin will ignore any empty spaces or lines while getline will not. What's happening is that after you input the first student's GPA, a newline character is stored in the buffer and is carried over to getline where it's entered automatically. Try using cin.ignore() after every cin >> gpa.

How to print nested struct objects from vector?

I am working on a program that keeps inventory of vehicles, so I created a struct for it. It also needs to keep a list of the drivers, so I created a nested struct for that. Here's the code:
struct Vehicle{
string License;
string Place;
int Capacity;
struct Driver{
string Name;
int Code;
int Id;
}dude;
};
I ask for user input and then put the structs in a vector using this function:
void AddVehicle(vector<Vehicle> &vtnewV){
Vehicle newV;
Vehicle::Driver dude;
cout << "Enter license plate number: " << endl;
cin >> newV.License;
cout << "Enter the vehicle's ubication: " << endl;
cin >> newV.Place;
cout << "Enter the vehicle's capacity: " << endl;
cin >> newV.Capacity;
cout << "Enter the driver's name: " << endl;
cin >> dude.Name;
cout << "Enter the driver's code: " << endl;
cin >> dude.Code;
cout << "Enter the driver's identification number: " << endl;
cin >> dude.Id;
vtnewV.push_back(newV);
};
Now, what I need is to print the structs inside the vector. I made the following function:
void PrintVehicle(vector<Vehicle> vtnewV){
{
vector<Vehicle> ::iterator i;
for (i = vtnewV.begin(); i != vtnewV.end(); i++)
{
cout << "License plate: " << i->License << endl;
cout << "Ubication: " << i->Place << endl;
cout << "Capacity: " << i->Capacity << endl;
cout << "Driver's name: " << i->dude.Name << endl;
cout << "Driver's code: " << i->dude.Code << endl;
cout << "Id: " << i->dude.Id << endl;
cout << " " << endl;
}
}
}
But it only prints out the elements of the first struct, printing out random numbers where the driver's info should be. Can you tell me where's my mistake? Everything else prints fine, except for the nested struct.
Vehicle::Driver dude;
You're declaring another variable here, which has nothing to do with the dude inside the newV (Vehicle).
Change the code to:
void AddVehicle(vector<Vehicle> &vtnewV){
Vehicle newV;
//Vehicle::Driver dude; // delete it here
cout << "Enter license plate number: " << endl;
cin >> newV.License;
cout << "Enter the vehicle's ubication: " << endl;
cin >> newV.Place;
cout << "Enter the vehicle's capacity: " << endl;
cin >> newV.Capacity;
cout << "Enter the driver's name: " << endl;
cin >> newV.dude.Name;
cout << "Enter the driver's code: " << endl;
cin >> newV.dude.Code;
cout << "Enter the driver's identification number: " << endl;
cin >> newV.dude.Id;
vtnewV.push_back(newV);
};