Hi! I am a computer science student and I'm currently working on files, the subject on which I do not have a massive amount of conceptual understanding. I can quickly understand though, if someone could help me out on this problem.
I'm creating a program for a Restaurant that can take in Customer's information and store them in a binary file. There's a structure for the Customer information from which I have created 10 objects in an array called C.
Now all of that runs perfectly (the entering of information) but I'm getting stuck in this particular section of the code that employs the use of files.
I've created a stream class object fio in int main() like this:
and then I've done this to accept each customer's information into the binary file:
cout<< "\nEnter number of reservations you wish to make: \t";
cin>>n;
while(!fio.eof())
{
if(!fio)
{
cout<< "\n \nFile cannot be opened. \n";
return 1;
}
for(i=0;i<n;i++)
{
cout<< "\nEnter details for reservation "<<i+1<< "\n";
C[i].AddCust();
fio.write((char *)& C[i], sizeof(C[i]));
}
fio.seekg(0);
system ("cls");
cout<< "\n \nReservations made for the day: \n \n";
for(i=0;i<n;i++)
{
fio.read((char *)& C[i], sizeof(C[i]));
C[i].DisplayCust();
}
}
fio.close();
No matter what I've tried it always says displays "File cannot be opened". I have only written this code and since I'm a little confused about files I haven't done ANYTHING else (eg. going to Debug file and doing stuff there).
No matter what I've tried it always says displays "File cannot be opened".
If the file doesn't exist, it cannot be opened for input, that's why it fails. A simple fix, add ios::trunc to the open mode, it will discard any existing file contents, or create a new file if none exists.
The loop while(!fio.eof()) would usually be inadvisable. In this case it seems to serve no purpose at all.
Just for demonstration purposes, I used two separate arrays here. That isn't necessary, but on the other hand, since you have the original array, there is no need for a file either. So, just as a somewhat useless example, this code writes the array to a file, reads it back into a different array, then displays the contents of that second array.
It's better to use perror or strerror(errno) to get a proper error message.
Also forget about !fio.eof(). There are tons of posts about the problem and why it shouldn't be used.
@Chervil Thank you so much! This certainly cleared my problem. I'll figure out the problems by tonight but a crystal clear example like this one is what I needed. Can you suggest a good website to better develop my concepts?
@neha candy I'm not sure of any particular website, there is a tutorial on this site, but it may be too concise to give the fuller understanding you want.
I have finally succeeded. It's been a while but I was busy with my other school projects. There's only one slight problem left. My string outputs contains a bunch of junk values in the beginning and I have tried every method that's possible for me to remove it and have had no luck. Could you try to find the problem, please? Here's the code snippet.
From the AddCust() function in my structure Reservation:
for (i=0;i<n;i++)
{
C[i].AddCust();
}
if(!fio)
{
cout<< "\n \nFile cannot be opened. \n";
return 1;
}
for(i=0;i<n;i++)
{
//cout<< "\nEnter details for reservation "<<i+1<< "\n";
//C[i].AddCust();
//fio.write((char *)& C[i], sizeof(C[i]));
fio.write(reinterpret_cast<char *>(& C[i]), sizeof(C[i]));
}
fio.seekg(0);
system ("cls");
cout<< "\n \nReservations made for the day: \n \n";
Customer cust[10];
int size=0;
while (fio.read(reinterpret_cast<char *>(&cust[size]), sizeof (Customer)))
{
size++;
}
for (int i=0;i<size;++i)
{
cust[i].DisplayCust();
}
And finally here's the output:
Reservations made for the day:
Name: 0x6fefeb24neha
E-mail ID: 0x6fefeb24mehshakjcn
Number: 0x6fefeb2434
Date of next Arrival: 32/32/32 at 32hours.
Name: 0x6fefeb24neha
E-mail ID: 0x6fefeb24eakjcnjac
Number: 0x6fefeb2443
Date of next Arrival: 2/223/2324 at 343hours.
Name: 0x6fefeb24Neha
E-mail ID: 0x6fefeb24nehayay@yipee.com
Number: 0x6fefeb2455602829
Date of next Arrival: 23/11/2017 at 1600hours.
Name: 0x6fefeb24neha
E-mail ID: 0x6fefeb24whcjdka
Number: 0x6fefeb248732632
Date of next Arrival: 32/3/3223 at 2332hours.
Name: 0x6fefeb24neha
E-mail ID: 0x6fefeb24ekjabs
Number: 0x6fefeb2489237
Date of next Arrival: 22/323/3232 at 12hours.
Go back to Start page? (y/n)