Read Strings in from text file and store into array

This is a discussion on Read Strings in from text file and store into array within the C++ Programming forums, part of the General Programming Boards category; I'm having difficulty with this. I'm working on a project for a class that is an address book. The user ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19

    Read Strings in from text file and store into array

    I'm having difficulty with this. I'm working on a project for a class that is an address book. The user will input some contact information which is stored into an object of a class, that object is then stored into an array. Once the user has finished inputting all their contacts into the program, it saves those contacts to a text file and exits the session. I am trying to read the saved data in from the text file, and store it back into the array. Here is how I save the data:

    This is a member function of a class in the project, this class is used to first store data into the array, then sort it into alphabetical order, then search the contacts upon the user's request.

    Code:
    // This function will contact list to disk
    void List::saveList()
    {
    	ofstream contactDatabase;
    	contactDatabase.open("ContactDatabase.txt");
    	for (int x=0; x<nextEmptySpot; x++)
    	{
    		contactList[x].savePerson(contactDatabase);
    	}
    	contactDatabase.close();
    }
    That function calls a member function of the second class in my project. The second class asks the user for a contact's information, then stores it into private variables.

    Code:
    void Person::savePerson(ofstream& contactDatabase)
    {
    	contactDatabase<<firstName<<endl<<lastName<<endl<<address<<endl<<zip<<endl<<phone<<endl<<email<<endl<<notes<<endl;
    }
    Now is where I am having trouble, trying to read in the saved data so that it can be searched and sorted with new data that the user might input. This is a global function in the main file.
    Code:
    void readFile()
    {
    	List inputData;
    	ifstream contactDatabase("ContactDatabase.txt");
    	if (contactDatabase.is_open())
    	{
    		while (contactDatabase.good())
    		{
    			inputData.inputDataFromFile(contactDatabase);
    		}
    		contactDatabase.close();
    	}
    	else
    		cout<<"No existing contacts stored on disk\n\n";
    }
    That function calls a function intended to input data from the file back into the array. I can't seem to get this function correct. This is in our first class, since that is where my array is a private variable.

    Code:
    void List::inputDataFromFile(ifstream& contactDatabase)
    {	 
    	string firstName;
    	contactDatabase.getline(contactDatabase, firstName);
    	contactList[nextEmptySpot] = firstName;
    	nextEmptySpot += 1;
    }
    I'm not sure if you will need to see other parts of the code or not, but this is what i have so far. I appreciate your feedback.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    I am getting errors relating to the last function i posted.

    error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_El em *,std::streamsize)' : cannot convert parameter 1 from 'std::ifstream' to 'char *'
    and
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

    I am unsure of how to fix these.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    See the example here

    There's not enough info for the second one, but you're trying to assign a string to something that is not a string.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    I looked at that page quite a few times before and after posting this. In my case, the string firstName already has data that needs to be associated with it in the text file, and I need to associate that data with the string. I'm not sure of the syntax on how to do this.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    You just need
    Code:
    getline(contactDatabase, firstname);
    Not knowing what type of variable contactList is, can't help on the other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file, scan and store data into variables?
    By wisdom30 in forum C Programming
    Replies: 8
    Last Post: 04-18-2011, 11:23 PM
  2. Read text file and store it in an array
    By look2hook in forum C Programming
    Replies: 2
    Last Post: 12-03-2010, 10:47 PM
  3. read and store text file as an array
    By abotaha in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 08:57 PM
  4. Read max value from strings of numbers in text file
    By james890 in forum C++ Programming
    Replies: 14
    Last Post: 04-15-2010, 03:26 PM
  5. Read text from a file into an array
    By ccwash in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2005, 03:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21