13 Replies - 1168 Views - Last Post: 09 December 2012 - 11:42 AM Rate Topic: -----

#1 dcwl2020  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 08-December 12

after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 09:53 AM

#include <iostream>
 #include <fstream> 
 #include <string> 
 #include <vector> 
 using namespace std; 
 struct Date 
 { 
            int month, day, year; 
 };
 struct Item 
 { 
                string ItemDescription; 
                int quantityCost; 
                float wholesale; 
                float retailCost; 
                Date dateAdded; 
 }; 
 Date String_To_MDY(char * date_ptr); 
 char * Strchcpy(char * target, char * source, int ch); 
 int Validate_Date(Date date); 
 Item inputItem(); 
 void outputItem(Item); 
 void readInventory(string,vector<Item>&); 
 void writeInventory(string,vector<Item>); 
 int displayMenu(); 
 void displayRecords(vector<Item>); 
 //Start main
 int main( ) 
 { 
            vector<Item> inventory; 
            int menuOption; 
            string fileName; 
            readInventory("inventory.txt",inventory); 
             do{ 
                         menuOption=displayMenu( ); 
                         switch(menuOption){ 
                         case 1:{ 
                                        inventory.push_back(inputItem()); 
                                    } 
                                     break; 
                         case 2:{ 
                                     int menuIndex; 
                                     do{ 
                                           displayRecords(inventory); 
                                          cout<<"Enter number of record to view: "; 
                                           cin>>menuIndex; 
                                          if(menuIndex<0 || menuIndex>=inventory.size()) 
                                            cout<<"Invalid Input!"<<endl; 
                                  }while(menuIndex<0 || menuIndex>=inventory.size()); 
                               outputItem(inventory[menuIndex]); 
                            } 
                              break; 
                    case 3:{ 
                                  int menuIndex; 
                                                do{ 
                                                      displayRecords(inventory); 
                                                     cout<<"Enter number of record to change: "; 
                                                             cin>>menuIndex; 
                                                             if(menuIndex<0 || menuIndex>=inventory.size()) 
                                                                         cout<<"Invalid Input!"<<endl; 
                                                 }while(menuIndex<0 || menuIndex>=inventory.size()); 
                                                 inventory[menuIndex]=inputItem(); 
                                     } 
                                     break; 
                                     case 4:{ 
                                                 writeInventory("inventory.txt",inventory); 
                                                  } 
                                     break; 
                        } 
             }while(menuOption!=4); 
             system "pause";
             return 0; 
 } 
 
 
 Date String_To_MDY(char * date_ptr) 
 { 
     Date mdy_date; 
     char month[4], 
     day[4], 
     year[4]; 
     char * ch_ptr; 
     ch_ptr = date_ptr; 
     ch_ptr = Strchcpy(month, ch_ptr, '/'); 
     ++ch_ptr; 
     ch_ptr = Strchcpy(day, ch_ptr, '/'); 
     ++ch_ptr; 
     Strchcpy(year, ch_ptr, '\0'); 
     mdy_date.month = atoi(month); 
     mdy_date.day   = atoi(day); 
     mdy_date.year = atoi(year); 
     return mdy_date; 
 } 
 
 
 char * Strchcpy(char * target, char * source, int ch) 
 { 
  while (*source != ch && *source != '\0') 
     { 
       *target = *source; 
       ++target; 
       ++source; 
     } 
  *target = '\0'; 
  return source; 
 }
 
   
 int Validate_Date(Date date) 
 { 
    int num_days [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
    int last_days [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  
    if (date.month < 1 || date.month > 12)
     return 0; 
     if (date.day < 1) 
     return 0; 
     if (date.year % 4 == 0) 
     { 
       if (date.day > last_days[date.month]) 
             return 0; 
     } 
  else 
     if (date.day > num_days[date.month])
       return 0; 
  if (date.year > 100) 
     return 0; 
  return 1; 
  
 } /* End of Validate_Date() */ 
 
 
 void outputItem(Item output) 
 { 
             cout<<"\nItemDescription :\t"<<output.ItemDescription; 
             cout<<"\nDate            :\t"<<output.dateAdded.month<<"/"<<output.dateAdded.day
                                                  <<"/"<<output.dateAdded.year; 
             cout<<"\nQuantity on Hand:\t"<<output.quantityCost; 
             cout<<"\nWholesale Cost :\t"<<output.wholesale; 
             cout<<"\nRetail Cost     :\t"<<output.retailCost<<endl<<endl; 
 } 
 
 
 Item inputItem() 
 { 
             Item temp; 
             char date[9]; 
             cout<<"ItemDescription:\t"; 
             cin>>temp.ItemDescription; 
             do{ 
                         cout<<"Date in mm/dd/yy form:\t"; 
                         cin>>date; 
                         temp.dateAdded=String_To_MDY(date); 
                         if(!Validate_Date(temp.dateAdded)) 
                                     cout<<"Invalid Entry :: Invalid Date!\n"; 
             }while(!Validate_Date(temp.dateAdded)); 
             do{ 
                         cout<<"Quantity Cost:\t"; 
                         cin>>temp.quantityCost; 
                         if(temp.quantityCost<0) 
                                     cout<<"Invalid value\n"; 
             }while(temp.quantityCost<0); 
             do{ 
                         cout<<"Wholesale Cost:\t"; 
                         cin>>temp.wholesale; 
                         if(temp.wholesale<0) 
                                     cout<<"Invalid Value\n"; 
             }while(temp.wholesale<0); 
             do{ 
                         cout<<"Retail Cost:\t"; 
                         cin>>temp.retailCost; 
                         if(temp.retailCost<0) 
                                     cout<<"Invalid value\n"; 
             }while(temp.retailCost<0); 
             return temp; 
 }
 
 
 void readInventory(string fileName,vector<Item>&inventory) 
 { 
           cout<<"\nFile name: "<<fileName<<endl; 
             fstream file(fileName.c_str(),fstream::in); 
             if(file.good()){ 
                         cout<<"\t\t :: Success!\n"; 
                         while(!file.eof()){ 
                                     Item temp; 
                                     int readsize=0; 
                                     file>>readsize; 
                                     if(readsize){
                                                 file.read((char*)&temp.ItemDescription,readsize); 
                                                 file>>temp.dateAdded.day; 
                                                 file>>temp.dateAdded.month; 
                                                 file>>temp.dateAdded.year; 
                                                 file>>temp.quantityCost; 
                                                 file>>temp.retailCost; 
                                                 file>>temp.wholesale; 
                                                 inventory.push_back(temp); 
                                                 cout<<"\t\t :: read item: "<<temp.ItemDescription<<endl; 
                                     } 
                         } 
             } 
             else 
                         cout<<"\nFailed,If you Save & Exit then a new file will be created."; 
             file.close(); 
             cout<<"\nClosed "<<fileName<<endl; 
 } 
 
 
 void writeInventory(string fileName,vector<Item> inventory) 
 { 
             cout<<"\t :: Opening "<<fileName<<endl; 
             fstream file(fileName.c_str(), fstream::out); 
             if(file.good()){ 
                         cout<<"\t\t :: Success!\n"; 
                         for(int i = 0; i < inventory.size(); i++){ 
                                     file<<sizeof(inventory[i].ItemDescription); 
                                     file.write((char*)&inventory[i].ItemDescription,sizeof(inventory[i].ItemDescription)); 
                                     file<<inventory[i].dateAdded.day<<endl; 
                                     file<<inventory[i].dateAdded.month<<endl; 
                                     file<<inventory[i].dateAdded.year<<endl; 
                                     file<<inventory[i].quantityCost<<endl; 
                                     file<<inventory[i].retailCost<<endl; 
                                     file<<inventory[i].wholesale<<endl; 
                                     cout<<"\t\t :: wrote item: "<<inventory[i].ItemDescription<<endl; 
                         } 
             } 
             else 
                         cout<<"\t\t :: Failure!\n"; 
             file.close(); 
             cout<<"\t :: Closed "<<fileName<<endl; 
 }
 
  
 int displayMenu() 
 { 
             int ch; 
             do{ 
                         
                         cout<<"--------------------------------"<<endl; 
                         cout<<"1: Add new records to the file"<<endl; 
                         cout<<"2: Display any record in the file"<<endl; 
                         cout<<"3: Change any record in the file "<<endl; 
                         cout<<"4: Save & Exit"<<endl; 
                         cout<<"--------------------------------\n>"; 
                         cin>>ch; 
                         if(ch<1 || ch>4) 
                                     cout<<"Invalid Input!"<<endl; 
             }while(ch<1 || ch>4); 
             return ch; 
 } 
 
 
 void displayRecords(vector<Item> inventory) 
 { 
             cout<<"Records:"<<endl; 
             for(int x=0;x<inventory.size();x++) 
                         cout<<x<<": "<<inventory[x].ItemDescription<<endl; 
 }





OKAY,here is the code, after i create it, add a record and then i save it. After that i re-run the program again, but it crash.

Is This A Good Question/Topic? 0
  • +

Replies To: after create a.txt file and then reopen it, it crash, HELP!

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,341
  • Joined: 31-December 10

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 10:06 AM

A stream doesn't return EOF until you try to read from it, so you need to check for EOF after each input attempt. I would recommend reading a line from the file and then splitting the line up into the input you need.
To read a file, line by line:
string line;
fstream file(/* ... */);

while(getline(file, line))
{
    // Do something with the line
}


The getline() function returns the stream so after the function attempts to read a line, if it reaches EOF, the while loop will exit because the condition will fail.
Was This Post Helpful? 0
  • +
  • -

#3 dcwl2020  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 08-December 12

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 10:09 AM

i have no problem reading a record, but the problem is after i save my file, i reopen it, its NOT RESPONDING

This post has been edited by GunnerInc: 08 December 2012 - 10:16 PM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,744
  • Joined: 19-February 09

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 08:27 PM

ItemDescription is a string.

string ItemDescription;



The class string more than likely uses a pointer to point to the actual data/string.
Basically, you are not writing the actual data to the file.

Here this is the size of a string class object and not the string.

sizeof(inventory[i].ItemDescription); 




You can use the c_str function and there is also a size function.

string


In the file you may want a space between the string size and the string data.
Was This Post Helpful? 0
  • +
  • -

#5 dcwl2020  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 08-December 12

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 09:14 PM

can u actually fixed that for me? pls

This post has been edited by GunnerInc: 08 December 2012 - 10:15 PM
Reason for edit:: Removed quote

Was This Post Helpful? -1
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,744
  • Joined: 19-February 09

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 09:26 PM

Here is one part in the write function we wish to change.

file << sizeof(inventory[i].ItemDescription); 




The string is :

string ItemDescription;




How would you return the size of the C++ string?
Was This Post Helpful? 0
  • +
  • -

#7 dcwl2020  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 08-December 12

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 09:31 PM

so i have to return the string itemdescription?

This post has been edited by GunnerInc: 08 December 2012 - 10:15 PM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,744
  • Joined: 19-February 09

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 10:00 PM

No.

Do you want to write the data as binary?

When you use the << operator you are not writing binary data.

This post has been edited by #define: 08 December 2012 - 10:14 PM

Was This Post Helpful? 0
  • +
  • -

#9 dcwl2020  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 08-December 12

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 10:21 PM

No, its nt binary file. But i cant read the character in the txt file, it looks like binary number in the file
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,744
  • Joined: 19-February 09

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 08 December 2012 - 10:54 PM

Basically, files can be written as binary or text. It is not exactly the same meaning as in a binary number 010111.

When data is written to a binary file it is usually saved as it is written in memory. So numbers won't be written the same way as text numbers. String data is usually the same or similar.

You use write and read which is used for binary files, and << which is used for text files.


You can use a text file.

To write a string to a text file, you could add an end line, then to read the data use getline. With numbers you can leave a space between the data, but maybe use an endline/newline at the end of the record.


You can have functions in the structs.

struct Item
{
  string description;
  int    quantity;
  float  wholesale;
  float  retailCost;
  Date   added;

  void write_text(ofstream &os);
};



void Item::write_text(ofstream &os)
{
  os << description << endl;

}


Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 09 December 2012 - 01:15 AM

All files are binary files, text files are just a specific representation :) The issue with writing string objects as binary is that you don't know how much memory the string occupies. Binary data is usually used with POD types, if you are going to try and save string data, I recommend just using character arrays of a fixed size.
Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,341
  • Joined: 31-December 10

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 09 December 2012 - 09:34 AM

View Postjjl, on 09 December 2012 - 03:15 AM, said:

The issue with writing string objects as binary is that you don't know how much memory the string occupies.

Um....std::string doesn't have a size() or length() member function? Whatever is returned from either of those functions is the amount of memory that is required (not including any terminating NULL character).

I recently discovered this thing on google about serializing data structures and it can be used for a few different languages: Protocol Buffers.

This post has been edited by vividexstance: 09 December 2012 - 09:35 AM

Was This Post Helpful? -1
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,895
  • Joined: 25-December 09

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 09 December 2012 - 10:36 AM

Quote

Um....std::string doesn't have a size() or length() member function? Whatever is returned from either of those functions is the amount of memory that is required (not including any terminating NULL character).

Yes, but that won't help you when reading a binary file unless you have saved this information in the file before the string. When you want to retrieve a string from a binary file you must know how many characters were written to the file for that particular string.

Jim
Was This Post Helpful? 0
  • +
  • -

#14 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,341
  • Joined: 31-December 10

Re: after create a.txt file and then reopen it, it crash, HELP!

Posted 09 December 2012 - 11:42 AM

The OP isn't dealing with binary files, so why is everyone talking about that?
Was This Post Helpful? -1
  • +
  • -

Page 1 of 1