Jump to content
  • Advertisement

mckoo

Member
  • Content Count

    62
  • Joined

  • Last visited

Community Reputation

122 Neutral

About mckoo

  • Rank
    Member
  1. can anyone offer me suggestions ?? many thanks
  2. thanks for the replies.... I finally got the serializing code written .... void Record::InFile() { Read_Str(Name.getFirst()); Read_Str(Name.getLast()); Read_Str(Address.getCity()); Read_Str(Address.getID()); Read_Str(Address.getState()); Read_Str(Address.getStreet()); Read_Str(Address.getZip()); Read_Str(Phone.getArea()); Read_Str(Phone.getNumber()); Read_Str(Phone.getPrefix()); } void Read_Str(string str) { ofstream fout("info.dat", ios::binary|ios::app); string data; data = str; int tmp = data.size(); fout.write(reinterpret_cast(&data), sizeof(tmp) ); fout.write(data.c_str(), tmp+1); fout.close(); } I checked info.dat, i found all data entered is shown in the file However I got stuck when trying to deserialize the code [source lang = "cpp"] string Write_Str() { ifstream fin("info.dat",ios::binary); string data; int len=0; char *p=0; fin.read(reinterpret_cast(&len), sizeof(len) ); p=new char [len+1]; fin.read(p, len+1); data = p; cout<< data; fin.close(); return data; } the first character of the file is shown...but the rest are the junk :/ plz help >.<
  3. Main Code #include <iostream> #include <iomanip> #include <string> #include <cstdlib> #include <fstream> #include <vector> #include "shortphone.cpp" #include "shortname.cpp" #include "shortaddress.cpp" using namespace std; class Record{ shortName Name; shortAddress Address; shortPhone Phone; public: void AddNewRecord(); void ViewRecord(); int FindRecord(); }; int menu() { int choice; cout <<" Record List Processing System "<<endl; cout <<"1. Add New Record "<<endl; cout <<"2. Remove A Record Object from the list"<<endl; cout <<"3. View all record object"<<endl; cout <<"4. Find the object"<<endl; cout <<"5. Exit"<<endl; cin >> choice; return choice; } /*void Record::InFile(ofstream fout) { string output = Name.getFirst(); fout.write((char*) &output, sizeof(Name.getSizeFirst())); fout.write(output.c_str(), Name.getSizeFirst()); output = Name.getLast(); fout.write((char*) &output, sizeof(Name.getSizeLast())); fout.write(output.c_str(), Name.getSizeLast()); }*/ void Record::AddNewRecord() { cin.ignore(); string input; cout << "First Name :"; getline(cin,input,'\n'); Name.setFirst(input); cout << "Last Name :"; getline(cin,input,'\n'); Name.setLast(input); cout << "ID: :"; //Address getline(cin,input,'\n'); Address.setID(input); cout << "Street :"; getline(cin,input,'\n'); Address.setStreet(input); cout << "City :"; getline(cin,input,'\n'); Address.setCity(input); cout << "State :"; getline(cin,input,'\n'); Address.setState(input); cout << "Zip :"; getline(cin,input,'\n'); Address.setZip(input); cout << "Area Code :"; getline(cin,input,'\n'); Phone.setArea(input); cout << "Telephone - Prefix :"; getline(cin,input,'\n'); Phone.setPrefix(input); cout << "Telephone - Numbers:"; getline(cin,input,'\n'); Phone.setNumber(input); input.erase(); } void Record::ViewRecord() { cout << Name.getFirst() <<" " << Name.getLast() <<" "; cout << Address.getID() <<" " << Address.getCity() <<" " << Address.getStreet() <<" " << Address.getState() <<" " << Address.getZip() <<setw(4) ; cout << Phone.getArea() <<" " <<Phone.getPrefix() <<"-" << Phone.getNumber() <<endl; } int Record::FindRecord() { cin.ignore(); string input,last_name; int i = 1; last_name = Name.getLast(); cout <<"Enter the last name :"; getline(cin, input); i=last_name.compare(input); return i; } typedef vector<Record> SetRecord; int main() { SetRecord GrowingRecord; ofstream fout("info.dat", ios::binary|ios::app); ifstream fin("info.dat", ios::binary); for(;;){ int choice = menu(); switch(choice) { case(1): { Record Employee; Employee.AddNewRecord(); GrowingRecord.push_back (Employee); // Employee.InFile(fout); break; } case(2): { int i,j; cout <<"Index " << " Name " << setw(2) << " ID " << setw(2) << " Street " << setw(5) << " City " << setw(5) <<"State "<<setw(2) << " Telephone " <<endl; for(i=0; i<GrowingRecord.size();i++) { Record Employee; Employee = GrowingRecord; cout << i << setw(2); Employee.ViewRecord(); } if(i==0){ cout <<"No Records Exist! "<<endl; break; } cout << "Which record do you wish to delete (-1 to exit) ? (0-" << i-1 <<")" ; cin >> j; GrowingRecord.erase(GrowingRecord.begin()+j); } break; case(3): { cout <<"Index " << " Name " << setw(2) << " ID " << setw(2) << " Street " << setw(5) << " City " << setw(5) <<"State "<<setw(2) << " Telephone " <<endl; for(int i=0; i<GrowingRecord.size();i++) { Record Employee; Employee = GrowingRecord; cout << i << " "; Employee.ViewRecord(); } break; } case(4): { int j; for(int i=0; i<GrowingRecord.size();i++) { Record Employee; Employee = GrowingRecord; if((j=Employee.FindRecord()==0)){ cout <<"Found !"<< "The data is located at " << i << "th data" <<endl; break;} else cout <<"Not Found !"; } break; } case(5): exit(1); break; default: cout <<"Error" << endl; }; } return 0; } ShortPhone Definition #include <iostream> using namespace std; const string null = ""; //Define a constant null value... class shortPhone { string AreaCode; string Prefix; string Number; public: //Declare the constructors... shortPhone(); shortPhone(string, string, string); //Define the mutators... void setArea(string Area) { AreaCode = Area; } void setPrefix(string Pref) { Prefix = Pref; } void setNumber(string Num) { Number = Num; } //...and the accesors... string getArea() { return AreaCode; } string getPrefix() { return Prefix; } string getNumber() { return Number; } }; //Define the default constructor... shortPhone::shortPhone() { AreaCode = null; Prefix = null; Number = null; } //Define the initializing one... shortPhone::shortPhone(string Area, string Pre, string Num) { AreaCode = Area; Prefix = Pre; Number = Num; } ShortName Definiton #include <iostream> #include <string> #include <vector> using namespace std; //const string null; //Define a constant null value... class shortName { string firstName; string lastName; public: //Declare the constructors... shortName(); shortName(string, string); //Define the mutator member functions... void setFirst(string First) { firstName = First;} void setLast(string Last) { lastName = Last;} //...and some accessor functions string getFirst() { return firstName;} string getLast() { return lastName;} int getSizeFirst() { return sizeof(firstName);} int getSizeLast() { return sizeof(lastName);} }; //Define the default constructor... shortName::shortName() { firstName = "New First Name"; lastName = "New Last Name"; } //...and the initializing one... shortName::shortName(string First, string Last) { firstName = First; lastName = Last; } ShortAddress Definition #include <iostream> #include <string> #include <vector> using namespace std; //const string =0; //Define a constant null value... class shortAddress { string StreetID; string StreetName; string CityName; string StateName; string ZipCode; public: //Declare the constructors... shortAddress(); shortAddress(string, string, string, string, string); //Define the mutators... void setID(string ID) { StreetID = ID; } void setStreet(string Street) {StreetName = Street; } void setCity(string City) { CityName = City; } void setState(string State) { StateName = State; } void setZip(string Zip) { ZipCode = Zip; } //...and the accesors... string getID() { return StreetID; } string getStreet() { return StreetName; } string getCity() { return CityName; } string getState() { return StateName; } string getZip() { return ZipCode; } }; //Define the default constructor... shortAddress::shortAddress() { StreetID = "New"; StreetName = "New"; CityName = "New"; StateName ="New"; ZipCode = "New"; } //Define the initializing constructor... shortAddress::shortAddress(string ID, string Street, string City, string State, string Zip) { StreetID = ID; StreetName = Street; CityName = City; StateName = State; ZipCode = Zip; } ShortPhone Definition #include <iostream> using namespace std; const string null = ""; //Define a constant null value... class shortPhone { string AreaCode; string Prefix; string Number; public: //Declare the constructors... shortPhone(); shortPhone(string, string, string); //Define the mutators... void setArea(string Area) { AreaCode = Area; } void setPrefix(string Pref) { Prefix = Pref; } void setNumber(string Num) { Number = Num; } //...and the accesors... string getArea() { return AreaCode; } string getPrefix() { return Prefix; } string getNumber() { return Number; } }; //Define the default constructor... shortPhone::shortPhone() { AreaCode = null; Prefix = null; Number = null; } //Define the initializing one... shortPhone::shortPhone(string Area, string Pre, string Num) { AreaCode = Area; Prefix = Pre; Number = Num; } I tried to export it to a binary file, but i'm not able to read it back. the biggest problem i've found is that the class contains other classes which makes it difficult to be written out to a file. What can I need to do to make the program reading the binary file correctly? excuse my poor english
  4. mckoo

    data validation!?

    thx~~ but i do really want a single keypress..... so may i use itoa?
  5. mckoo

    data validation!?

    int main() { int exit_flag = 0; int ch; while(exit_flag == 0){ printf("XXX Secondary School Students' Union\n"); printf("1. Add new member information\n"); printf("2. Print member list\n"); printf("3. Exit\n"); printf("Please enter the choice: "); scanf("%d", &ch); switch(ch){ case 1: input_screen(); break; case 2: read_data(); break; case 3: exit_flag = 1; break; default: printf("Fal"); break; } } exit(1); return 0; } it works fine with digits....however when i type in a character, it look infinitely.....why this occurs? i tried with ch = getchar()...still doesn't work...... any replies are appreciated
  6. mckoo

    strange output

    struct record{ char name[20]; char student_id[7]; char class_id[10]; char position[25]; }; int main() { FILE *output_file; output_file = fopen("results.txt", "a+"); struct record *student; char ch; char buffer[BUFFERMAXSIZE]; fflush (stdin); //data ==> output_file printf("Name: "); gets(student.name); printf("Class: "); gets(student.class_id); printf("Student ID: "); gets(student.student_id); printf("Position: "); gets(student.position); printf("Are you sure to add new member/change member information(Y/N)? "); ch = getchar(); ch = toupper(ch); if( ch == 'Y' ) { fprintf(output_file,"%s\n", student.name); fprintf(output_file,"%s ", student.class_id); fprintf(output_file,"%s ", student.student_id ); fprintf(output_file,"%s ", student.position); fclose(output_file); input_screen(); } else if( ch == 'N'){ return 0; fclose(output_file); } else{ printf(" Invalid Choice, Please try again"); fclose(output_file); } } }; is it the problem of buffer overflow? when i input the record:::: Name:MiCold Class: F3A Student ID: 1234567 Position: Test i got sth like this in the results.txt MiCold (blank!?) 1234567 Test
  7. get it~ thx in advance to those helped me~
  8. so it returns a reference of num1 and assign the value of the memory address where the reference points to as i?
  9. #include <iostream> using namespace std; int num1 = 0; int &func() { return num1; } int main() { int i; i = 199; func() = i; cout << num1 << endl; i = 213; func() = i; cout << num1 << endl; return 0; } is that mean pass i to the func, then the function has returned a reference of this variable to obtain the value??? any replies are appreciated [evolutional] Added source tags
  10. why the function can be made as a l-value()??? like: int source = 15; //is it change the value of the variable directly? int &ret(int h){ return h; ///don't understand what does it for } int main() { int j = 9; ret() = j; //what's the point of doing this???? return 0; } this is really confusing.....
  11. mckoo

    why this works?

    sorry, i may have made a cross-post: but i finally got the program work with this code: #define BUFFERMAXSIZE 80 struct record{ char name[20]; char student_id[7]; char class_id[4]; char position[25]; }; int input_data() { FILE *output_file; output_file = fopen("results.txt", "a+"); struct record student; char ch; char buffer[BUFFERMAXSIZE]; fflush (stdin); //data ==> output_file printf("Name: "); gets(student.name); strcat(student.name, "\n"); printf("Class: "); gets(student.class_id); strcat(student.class_id, "\n"); printf("Student ID: "); gets(student.student_id); strcat(student.student_id, "\n"); printf("Position: "); gets(student.position); strcat(student.position, "\n"); printf("Are you sure to add new member/change member information(Y/N)? "); ch = getchar(); ch = toupper(ch); if( ch == 'Y' ) { fputs(student.name,output_file); fputs(student.class_id, output_file); fputs(student.student_id, output_file); fputs(student.position, output_file); fclose(output_file); input_screen(); } else if( ch == 'N'){ return 0; fclose(output_file); } else{ printf("\nInvalid Choice, Please try again"); fclose(output_file); } printf("\n"); } but why it doesn't work with this thingy:::::: int input_data() { FILE *output_file; output_file = fopen("results.txt", "a+"); struct record student; char ch; char buffer[BUFFERMAXSIZE]; fflush (stdin); //data ==> output_file printf("Name: "); gets(buffer); strcpy(student.name, buffer); strcat(student.name, "\n"); fflush (stdin); printf("Class: "); gets(buffer); strcpy(student.class_id, buffer); strcat(student.class_id, "\n"); printf("Student ID: "); gets(buffer); strcpy(student.student_id, buffer); strcat(student.student_id, "\n"); printf("Position: "); gets(buffer); strcpy(student.position, buffer); strcat(student.position, "\n"); printf("Are you sure to add new member/change member information(Y/N)? "); ch = getchar(); ch = toupper(ch); if( ch == 'Y' ) { fputs(student.name,output_file); fputs(student.class_id, output_file); fputs(student.student_id, output_file); fputs(student.position, output_file); fclose(output_file); input_screen(); } else if( ch == 'N'){ return 0; fclose(output_file); } else{ printf("\nInvalid Choice, Please try again"); fclose(output_file); } printf("\n"); } is that one more null character exist inside the buffer????pls reply my question
  12. mckoo

    file i/o output ===hurts!!!

    don't give me the code...pls.... just need advice and what kind of string function should i used.... thx in advance~
  13. excuse me,what's wrong with this function: void input_data() { FILE *output_file; output_file = fopen("results.txt", "a+"); struct record student; char ch; int loop_count; char buffer[BUFFERMAXSIZE]; fflush (stdin); //data ==> output_file printf("Name: "); gets(buffer); printf("Class: "); gets(buffer); printf("Student ID: "); gets(buffer); printf("Position: "); gets(buffer); printf("Are you sure to add new member/change member information(Y/N)? "); ch = getchar(); ch = toupper(ch); if( ch == 'Y' ) { fputs(buffer,output_file); } printf("\n"); } sorry...i forgot to attach my struct. struct record{ int student_id; char *student_name; char *class_id; char *position; }; the result is that none of the inputted data has been outputted...so what should i do? [Edited by - mckoo on September 6, 2004 7:47:16 AM]
  14. mckoo

    function ptr====hurts

    o....merci bien~ i'm pretty careless....~ thx in advance~
  15. #include <iostream> using namespace std; int org_func(int, int); int (*ptr_org)(int, int); int main() { int i,j = 9; int value; ptr_org = org_func; cout << org_func(3,4); return 0; } int org_func(int i, int j) { return i+j; } well...it prints out 7 as the result when cout << org_func(i,j), it prints out garbage...so can u guys tell me what is going on with it? any replies are appreciated
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!