Reading multiple data from text files

This is a discussion on Reading multiple data from text files within the C++ Programming forums, part of the General Programming Boards category; So I have been working through notes on text files and I have came to a problem with reading multiple ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    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.

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

    My actual code looks like this.

    Code:
     void readInFile()
    {
    	ifstream salesFile ("c:\\sales.txt");
    	index = 0;
    	while ((!salesFile.eof()) && (index < MAX))
    	{
    		getline (salesFile, names[index])
    		
    		index = index + 1;
    	}
    	count = index;
    	salesFile.close();
    }
    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
    Posts
    416
    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.
    strncmp()
    strcmp()
    atoi()

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by scwizzo View Post
    Some useful functions you might want to use.
    strncmp()
    strcmp()
    atoi()
    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, 10:21 PM
  2. Reading Multiple Pieces of Data From Text File
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2009, 08: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, 12:42 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM

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