Thread: Reading multiple data from text files

  1. #1
    Registered User
    Join Date
    Dec 2009

    Reading multiple data from text files

    So I have been working through notes on text files and I have came to a problem with reading multiple data from a textfile. The program has to take the surname and a value from the textfile, so my textfile looks something like this.

    Ogilvie, 1000
    O’Donovan, 1000
    Day, 15000
    Zarrug, 12000
    MacGregor, 5000
    Friel, 6000

    The pseudocode looks like this.

    open salesFile for reading
    while not end of file and index < max
    input from salesFile into names[index]
    input from salesFile into sales[index]
    index=index + 1
    close salesFile

    My actual code looks like this.

     void readInFile()
    	ifstream salesFile ("c:\\sales.txt");
    	index = 0;
    	while ((!salesFile.eof()) && (index < MAX))
    		getline (salesFile, names[index])
    		index = index + 1;
    	count = index;
    Is there anyway I can take the name and store it into an array and take the number and store it into another array by using the comma to seperate the name from the number.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Once you read the entire line in, check each character to see if it is a comma, then skip the space and put the remaining characters in another array. Then convert the numbers to an integer.

    Some useful functions you might want to use.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Quote Originally Posted by scwizzo View Post
    Some useful functions you might want to use.
    It is not C++ way. I prefer stringstream and operator>>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a text file into multiple nodes
    By e2076w in forum C Programming
    Replies: 1
    Last Post: 10-14-2009, 09:21 PM
  2. Reading Multiple Pieces of Data From Text File
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2009, 07:49 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 11:42 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM