MORE file I/O troubles
fonzie (11)Jul 19, 2008 at 11:58pm UTC
This program runs fine, but when I go in my files to find "database_file.txt" I can't find it anywhere. Anybody spot where I went wrong?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct customerDataBase{ //basic customer information struct
string lastName;
string firstName;
string addr1;
string addr2;
string eMail;
} Customer;
int k;
void TheScreen(customerDataBase customerInfo){
//this SHOULD print out all the information in the array
cout<<"Last name: " <<customerInfo.lastName<<endl;
cout<<"First name: " <<customerInfo.firstName<<endl;
cout<<"Addr1: " <<customerInfo.addr1<<endl;
cout<<"Addr2: " <<customerInfo.addr2<<endl;
cout<<"E-Mail: " <<customerInfo.eMail<<endl;
}
void addNewCustomer(){
fstream CustomerFile;
CustomerFile.open("database_file.txt" );
int maxCustomerNumber;
maxCustomerNumber = 1;
customerDataBase customerArray[maxCustomerNumber];
TheScreen(customerArray[k]);
cout<<endl<<"please enter last name: " ;
cin>>customerArray[k].lastName;
CustomerFile<<customerArray[k].lastName<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
cout<<endl<<"please enter first name: " ;
cin>>customerArray[k].firstName;
CustomerFile<<customerArray[k].firstName<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
cout<<endl<<"please address name: " ;
cin>>customerArray[k].addr1;
CustomerFile<<customerArray[k].addr1<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
cout<<endl<<"please other address information name: " ;
cin>>customerArray[k].addr2;
CustomerFile<<customerArray[k].addr2<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
cout<<endl<<"please enter e-mail name: " ;
cin>>customerArray[k].eMail;
CustomerFile<<customerArray[k].eMail<<"," ;
system ("cls" );
TheScreen(customerArray[k]);
CustomerFile.close();
}
int main(int argc, char *argv[])
{
addNewCustomer();
system("PAUSE" );
return EXIT_SUCCESS;
}
Duoas (801)Jul 19, 2008 at 11:58pm UTC
You are cout int the data instead of CustomerFile ing it, if that makes sense. Since no output to the customer file ever happens, C++ doesn't bother to actually make the file. (If you wanted a zero-length file you'd have to flush () it before close ()ing it.) If you change your TheScreen function to take an ostream object as argument, then you can print both to file and to the screen:1 2 3 4
void TheScreen( customerDataBase customerInfo, std::ostream &outs )
{
// you make the remaining fixes here
}
and1 2
TheScreen( customerArray[k], CustomerFile );
TheScreen( customerArray[k], cout );
Hope this helps.
fonzie (11)Jul 19, 2008 at 11:58pm UTC
Well, however much I appreciate your help, I don't quite follow your doing. Is there another way to explain it? For example, why when I put
CustomerFile<<customerArray[k].lastName<<"," ; is that not writing to the file?
Duoas (801)Jul 19, 2008 at 11:58pm UTC
You actually understand it just right. I was just interested in making it so you can output both to the file and to the screen.1 2 3 4 5 6 7 8
void TheScreen( customerDataBase customerInfo, std::ostream &outs )
{
outs<<"Last name: " <<customerInfo.lastName<<endl;
outs<<"First name: " <<customerInfo.firstName<<endl;
outs<<"Addr1: " <<customerInfo.addr1<<endl;
outs<<"Addr2: " <<customerInfo.addr2<<endl;
outs<<"E-Mail: " <<customerInfo.eMail<<endl;
}
The function now takes an additional argument: the thing you want to write to. So inside addNewCustomer when you want to write a customer's data you can write to both the file and the stream:1 2
TheScreen( customerArray[k], CustomerFile );
TheScreen( customerArray[k], cout );
Hope this helps.
DiptenduDas (83)Jul 19, 2008 at 11:58pm UTC
Hi fonzie, U r trying to write the data to a file after creating the same. But U have created the file "database_file.txt " using the fstream and that to without any mode.1 2
fstream CustomerFile;
CustomerFile.open("database_file.txt" );
By Default mode for fstream is ios:in , which means the file should be present else it will not create the file. So to overcome the same U either have to give the mode to ios:out like
CustomerFile.open("database_file.txt" ,ios::out); or use the ofstream for the file type.1 2
ofstream CustomerFile;
CustomerFile.open("database_file.txt" );
Hope This helps U out.
Last edited on Jul 19, 2008 at 11:58pm UTC
This topic is archived - New replies not allowed.