Thread: Write a Vector to a File

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    6

    Write a Vector to a File

    Hello,
    I have the Fill, Show, Remove, and Find functions working properly, but I cannot get my WriteToFile function to work. I know that there is a problem involving WriteToFile("Success"); but I don't know how to fix it so that my void WriteToFile function will work. Any help would be appreciated! Thanks in advance

    Here is my code so far:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class InventoryInfo  
    {
            string InventoryID;
            string InventoryDesc;
            string InventoryCost;
    
    public:
    		InventoryInfo() {InventoryID=""; InventoryDesc=""; InventoryCost="";}
            InventoryInfo(string ID, string Desc, string Cost)
    		{
    			InventoryID=ID;
    			InventoryDesc=Desc;
    			InventoryCost=Cost;
    		}
     
    		void SetID(string ID) {InventoryID=ID;} 
    		void SetDesc(string Desc) {InventoryDesc=Desc;}
    		void SetCost(string Cost) {InventoryCost=Cost;}
    
    		string GetID() {return InventoryID;}
    		string GetDesc() {return InventoryDesc;} 
    		string GetCost() {return InventoryCost;}
    };
    
    void Fill(vector<InventoryInfo>& iInfo);
    void Show(vector<InventoryInfo> iInfo);
    void Find(vector<InventoryInfo> iInfo);
    void Remove(vector<InventoryInfo>& iInfo);
    void WriteToFile(vector<InventoryInfo> iInfo);
    
    int main()
    {
            vector<InventoryInfo> iInfo;  //Creates an object...vector of strings...
    
    		Fill(iInfo);
            Show(iInfo);
    		Remove(iInfo);
    		Show(iInfo);
    		Find(iInfo);
    		iInfo.WriteToFile("Success");
    }
    
    void Fill(vector<InventoryInfo>& iInfo)
    {
    		cout<<"*** Add item ***"<<endl;
    
    		string ID, Desc;
    		string Cost;
    
    		for(;;)
    		{
    			cout<<"\n=================================================================="<<endl;
    			cout<<"Please enter an item ID, type 'stop' to cancel"<<endl;
    			cout<<"> ";
    			getline(cin, ID);
    			if(ID=="stop")
    				break;
    
    			cout<<"\nPlease give a description of the item"<<endl;
    			cout<<"> ";
    			getline(cin, Desc);
    
    			cout<<"\nWhat is the cost of the item?"<<endl;
    			cout<<"> $";
    			getline(cin, Cost);
    			cout<<"=================================================================="<<endl;
    
    			InventoryInfo Value0;
    			Value0.SetID(ID);
    			Value0.SetDesc(Desc);
    			Value0.SetCost(Cost);
    
    			iInfo.push_back(Value0);
    		}
    }
    
    void Show(vector<InventoryInfo> iInfo) // reference!
    {
    		cout<<"\n*** Item Inventory ***"<<endl;
    
    		vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
    
    		for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{
    			cout<<"\n=================================================================="<<endl;
    			cout<<"> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
    			cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
    			cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
    			cout<<"=================================================================="<<endl;
    		}
    }
    
    void Find(vector<InventoryInfo> iInfo)
    {
    		string Search;
    
    		cout<<"\nPlease enter item ID to search: "<<endl;
    		cout<<"> ";
    		getline(cin, Search);
    
    		vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
    
    		for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{
    			if(AccessInventory->GetID()==Search)
    			{
    				cout<<"\n=================================================================="<<endl;
    				cout<<"> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
    				cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
    				cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
    				cout<<"=================================================================="<<endl;
    				return;
    			}
    		}
    		cout<<"\nItem ID not found, please try again..."<<endl;
    		return;
    }
    
    void Remove(vector<InventoryInfo>& iInfo)
    {
    		string Remove;
    
    		cout<<"\nPlease enter item ID you wish to delete: "<<endl;
    		cout<<"> ";
    		getline(cin, Remove);
    		cout<<"\nItem inventory updated!"<<endl;
    	
    		vector<InventoryInfo>::iterator AccessInventory;
     
            for(AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{       
    			if(AccessInventory->GetID() == Remove)  
    			{
    				iInfo.erase(AccessInventory);
    				return;
                }
    		}
            cout<<"\nItem ID not found, please try again..."<<endl;
    		return;
    }
    
    void WriteToFile(vector<InventoryInfo> iInfo)
    {
    		fstream OutFile(iInfo.c_str(), ios::out);
    	
    		vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
    
    		for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{
    			OutFile<<AccessInventory->GetID()<<endl;
    			OutFile<<AccessInventory->GetDesc()<<endl;
    			OutFile<<AccessInventory->GetCost()<<endl;
    		}
    		OutFile.close();
    }

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    std::vector has no c_str method.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    6
    Thanks for replying! How would I go about fixing this?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Uh, well the problem is this:
    You probably want the function (maybe) to take the contents to write to file (which would be a vector) and a string telling the filename of the file to write to (that would be a string).
    A vector is a collection of one or more types (ie it is an array), so obviously it has no c_str() method!
    I think you need to think this over a little bit more on what you actually want to do.

    I also notice to try to call a member WriteToFile on the vector! Vector has no such method!
    I suspect you want to write it to a file, but it seems to me like you really need to study a bit more how objects work.
    You can't just declare a global method and expect a member function in an object to pop up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    6
    Thanks for replying! Okay, so I've made the following changes, but still no dice:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class InventoryInfo  
    {
            string InventoryID;
            string InventoryDesc;
            string InventoryCost;
    		string FileName;
    
    public:
    		InventoryInfo() {InventoryID=""; InventoryDesc=""; InventoryCost="";}
            InventoryInfo(string ID, string Desc, string Cost)
    		{
    			InventoryID=ID;
    			InventoryDesc=Desc;
    			InventoryCost=Cost;
    		}
     
    		void SetID(string ID) {InventoryID=ID;} 
    		void SetDesc(string Desc) {InventoryDesc=Desc;}
    		void SetCost(string Cost) {InventoryCost=Cost;}
    
    		string GetID() {return InventoryID;}
    		string GetDesc() {return InventoryDesc;} 
    		string GetCost() {return InventoryCost;}
    };
    
    class FileAccess  
    {
            string FileName;
    		int Size;
    
    public:
            FileAccess();
            FileAccess(string);
    
    		void WriteToFile();
    }
    
    FileAccess::FileAccess()
    {
            FileName = "Temp File";
            Size = 0;
    }
    
    FileAccess::FileAccess(string FName)
    {
            FileName = FName;
            Size = 0;
    }
    
    void Fill(vector<InventoryInfo>& iInfo);
    void Show(vector<InventoryInfo> iInfo);
    void Find(vector<InventoryInfo> iInfo);
    void Remove(vector<InventoryInfo>& iInfo);
    
    int main()
    {
            vector<InventoryInfo> iInfo;  //Creates an object...vector of strings...
    
    		Fill(iInfo);
            Show(iInfo);
    		Remove(iInfo);
    		Show(iInfo);
    		Find(iInfo);
    		WriteToFile(iInfo);
    }
    
    void Fill(vector<InventoryInfo>& iInfo)
    {
    		cout<<"*** Add item ***"<<endl;
    
    		string ID, Desc;
    		string Cost;
    
    		for(;;)
    		{
    			cout<<"\n=================================================================="<<endl;
    			cout<<"Please enter an item ID, type 'stop' to cancel"<<endl;
    			cout<<"> ";
    			getline(cin, ID);
    			if(ID=="stop")
    				break;
    
    			cout<<"\nPlease give a description of the item"<<endl;
    			cout<<"> ";
    			getline(cin, Desc);
    
    			cout<<"\nWhat is the cost of the item?"<<endl;
    			cout<<"> $";
    			getline(cin, Cost);
    			cout<<"=================================================================="<<endl;
    
    			InventoryInfo Value0;
    			Value0.SetID(ID);
    			Value0.SetDesc(Desc);
    			Value0.SetCost(Cost);
    
    			iInfo.push_back(Value0);
    		}
    }
    
    void Show(vector<InventoryInfo> iInfo) // reference!
    {
    		cout<<"\n*** Item Inventory ***"<<endl;
    
    		vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
    
    		for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{
    			cout<<"\n=================================================================="<<endl;
    			cout<<"> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
    			cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
    			cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
    			cout<<"=================================================================="<<endl;
    		}
    }
    
    void Find(vector<InventoryInfo> iInfo)
    {
    		string Search;
    
    		cout<<"\nPlease enter item ID to search: "<<endl;
    		cout<<"> ";
    		getline(cin, Search);
    
    		vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
    
    		for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{
    			if(AccessInventory->GetID()==Search)
    			{
    				cout<<"\n=================================================================="<<endl;
    				cout<<"> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
    				cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
    				cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
    				cout<<"=================================================================="<<endl;
    				return;
    			}
    		}
    		cout<<"\nItem ID not found, please try again..."<<endl;
    		return;
    }
    
    void Remove(vector<InventoryInfo>& iInfo)
    {
    		string Remove;
    
    		cout<<"\nPlease enter item ID you wish to delete: "<<endl;
    		cout<<"> ";
    		getline(cin, Remove);
    		cout<<"\nItem inventory updated!"<<endl;
    	
    		vector<InventoryInfo>::iterator AccessInventory;
     
            for(AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{       
    			if(AccessInventory->GetID() == Remove)  
    			{
    				iInfo.erase(AccessInventory);
    				return;
                }
    		}
            cout<<"\nItem ID not found, please try again..."<<endl;
    		return;
    }
    
    void FileAccess::WriteToFile()
    {
    		fstream OutFile(FileName.c_str(), ios::out);
    	
    		vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
    
    		for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{
    			OutFile<<AccessInventory->GetID()<<endl;
    			OutFile<<AccessInventory->GetDesc()<<endl;
    			OutFile<<AccessInventory->GetCost()<<endl;
    		}
    		OutFile.close();
    }

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Now you should create an instance FileAccess in main and call its WriteToFile method. That method also needs to take the vector as an argument.

    Incidentally, if your function doesn't modify the vector (or any other potentially large object) it is best to pass them by const reference to avoid unnecessary copying.

    E.g:
    Code:
    void Find(const vector<InventoryInfo>& iInfo);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    6
    Thank you anon for replying! What do you mean by "create an instance FileAccess in main and call its WriteToFile method"? Sorry if I may seem slow, but I am new to programming.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems that you have made WriteToFile a member function of a class called FileAccess. So you need a FileAccess instance to call it with:

    Code:
    int main()
    {
        vector<InventoryInfo> iInfo;
        FileAccess file("somewhere");
        ...
        file.WriteToFile(iInfo);  
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    6
    Thanks again anon! I worked long and hard on this program and it finally runs ^^. I've added a menu() and 2 more strings in the class. Everything runs fine, but when I display or search, the values are all weird. They were fine before. Do you know what's wrong with it?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class InventoryInfo  
    {
            string InventoryID;
            string InventoryDesc;
            string InventoryCost;
    		string ItemLocation;
    		string ItemsInStock;
    
    public:
    		InventoryInfo() {InventoryID=""; InventoryDesc=""; InventoryCost=""; ItemLocation=""; ItemsInStock="";}
            InventoryInfo(string ID, string Desc, string Cost, string Loc, string Stock)
    		{
    			InventoryID=ID;
    			InventoryDesc=Desc;
    			InventoryCost=Cost;
    			ItemLocation=Loc;
    			ItemsInStock=Stock;
    		}
     
    		void SetID(string ID) {InventoryID=ID;} 
    		void SetDesc(string Desc) {InventoryDesc=Desc;}
    		void SetCost(string Cost) {InventoryCost=Cost;}
    		void SetLocation(string Loc) {ItemLocation=Loc;}
    		void SetInStock(string Stock) {ItemsInStock=Stock;}
    
    		string GetID() {return InventoryID;}
    		string GetDesc() {return InventoryDesc;} 
    		string GetCost() {return InventoryCost;}
    		string GetLocation() {return ItemLocation;}
    		string GetInStock() {return ItemsInStock;}
    };
    
    void Fill(vector<InventoryInfo>& iInfo);
    void Show(vector<InventoryInfo> iInfo);
    void Find(vector<InventoryInfo> iInfo);
    void Remove(vector<InventoryInfo>& iInfo);
    void WriteToFile(vector<InventoryInfo> iInfo);
    void Menu();
    
    int main()
    {
            string Command;
    
    		vector<InventoryInfo> iInfo;  //Creates an object...vector of strings...
    		
    		while(true){
          Menu();
    		getline(cin, Command);
    		if (Command == "exit")
    		{
    			break;
    		}
    		else if (Command == "add")
    		{
    			Fill(iInfo);
    			cout << "\n" << endl;
    		}
    		else if (Command == "delete")
    		{
    			cout<<"\n"<<endl;
    			Remove(iInfo);
    			cout<<"\n"<<endl;
    		}
    		else if (Command == "display")
    		{
    			cout << "\n" << endl;
    			Show(iInfo);
    			cout << "\n" << endl;  
    		}
    		else if (Command == "search")
    		{
    			cout<<"\n"<<endl;
    			Find(iInfo);
    			cout<<"\n"<<endl;
    		}
    		else if (Command == "save")
    		{
    			cout<<"\n"<<endl;
    			WriteToFile(iInfo);
    			cout<<"\n"<<endl;
    		}
    		else
    		{
    			cout << "Please try another command\n" << endl;
    		}
    
       }
    }
    
    void Menu()		//This is displayed on the menu
    {
    	cout << "#################################################################\n" << endl;
    	cout << "Please choose one of the following commands:\n" << endl;
    	cout << "add" << endl;
    	cout << "delete" << endl;
    	cout << "display" << endl;
    	cout << "search" << endl;
    	cout << "save" <<endl;
    	cout << "exit"<< endl;
    	cout << "\n#################################################################\n" << endl;
    }
    
    void Fill(vector<InventoryInfo>& iInfo)
    {
    		cout<<"\n*** Add item ***"<<endl;
    
    		string ID, Desc, Cost, Loc, Stock;
    
    		for(;;)
    		{
    			cout<<"\n=================================================================="<<endl;
    			cout<<"Please enter an item ID, type 'stop' to cancel"<<endl;
    			cout<<"> ";
    			getline(cin, ID);
    			if(ID=="stop")
    				break;
    
    			cout<<"\nPlease give a description of the item"<<endl;
    			cout<<"> ";
    			getline(cin, Desc);
    
    			cout<<"\nWhat is the cost of the item?"<<endl;
    			cout<<"> $";
    			getline(cin, Cost);
    
    			cout<<"\nPlease describe the location of the item"<<endl;
    			cout<<"> ";
    			getline(cin, Loc);
    
    			cout<<"\nHow many items are in stock?"<<endl;
    			cout<<"> ";
    			getline(cin, Stock);
    
    			cout<<"=================================================================="<<endl;
    
    			InventoryInfo Value0;
    			Value0.SetID(ID);
    			Value0.SetDesc(Desc);
    			Value0.SetCost(Cost);
    			Value0.SetCost(Loc);
    			Value0.SetCost(Stock);
    
    			iInfo.push_back(Value0);
    		}
    }
    
    void Show(vector<InventoryInfo> iInfo) // reference!
    {
    		cout<<"\n*** Item Inventory ***"<<endl;
    
    		vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
    
    		for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{
    			cout<<"\n=================================================================="<<endl;
    			cout<<"> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
    			cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
    			cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
    			cout<<"> Item location: $"<<AccessInventory->GetLocation() << endl;
    			cout<<"> Items in stock: $"<<AccessInventory->GetInStock() << endl;
    			cout<<"=================================================================="<<endl;
    		}
    }
    
    void Find(vector<InventoryInfo> iInfo)
    {
    		cout<<"\n*** Find Item ***"<<endl;
    	
    		string Search;
    
    		cout<<"\nPlease enter item ID to search: "<<endl;
    		cout<<"> ";
    		getline(cin, Search);
    
    		vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
    
    		for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{
    			if(AccessInventory->GetID()==Search)
    			{
    				cout<<"\n=================================================================="<<endl;
    				cout<<"> Item ID: "<<AccessInventory->GetID() << endl; // use iterator...
    				cout<<"> Item description: "<<AccessInventory->GetDesc() << endl;
    				cout<<"> Item cost: $"<<AccessInventory->GetCost() << endl;
    				cout<<"> Item location: $"<<AccessInventory->GetLocation() << endl;
    				cout<<"> Items in stock: $"<<AccessInventory->GetInStock() << endl;
    				cout<<"=================================================================="<<endl;
    				return;
    			}
    		}
    		cout<<"\nItem ID not found, please try again..."<<endl;
    		return;
    }
    
    void Remove(vector<InventoryInfo>& iInfo)
    {
    		cout<<"\n*** Remove Item ***"<<endl;
    	
    		string Remove;
    
    		cout<<"\nPlease enter item ID you wish to delete: "<<endl;
    		cout<<"> ";
    		getline(cin, Remove);
    		cout<<"\nItem inventory updated!"<<endl;
    	
    		vector<InventoryInfo>::iterator AccessInventory;
     
            for(AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{       
    			if(AccessInventory->GetID() == Remove)  
    			{
    				iInfo.erase(AccessInventory);
    				return;
                }
    		}
            cout<<"\nItem ID not found, please try again..."<<endl;
    		return;
    }
    
    void WriteToFile(vector<InventoryInfo> iInfo)
    {
    		fstream OutFile("FINAL.dat", ios::out);
    	
    		vector<InventoryInfo>::iterator AccessInventory; //Sets up an iterator...
    
    		for( AccessInventory = iInfo.begin() ; AccessInventory != iInfo.end() ; ++AccessInventory)
    		{
    			OutFile<<AccessInventory->GetID()<<endl;
    			OutFile<<AccessInventory->GetDesc()<<endl;
    			OutFile<<AccessInventory->GetCost()<<endl;
    		}
    		OutFile.close();
    }

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    6
    Oh! Never mind, I caught my mistake:

    Value0.SetCost(Cost);
    Value0.SetCost(Loc);
    Value0.SetCost(Stock);

    They were all SetCost -_-

    Thanks again for your help, appreciated it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM