Reading data from a file and storing into seperate variables

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 11
Reputation: Jalwes is an unknown quantity at this point 
Solved Threads: 0
Jalwes Jalwes is offline Offline
Newbie Poster

Reading data from a file and storing into seperate variables

 
0
  #1
30 Days Ago
Hi all, I am trying to write code that will read from a file that is set up as an inventory file. So, on each line you'll have something similar to:

1. Toothpaste
2. Toothbrush
etc...

The problem I'm having is saving the numeric item number into one array index, then the item description into a different array index. The user should be able to add to the file and the program will need to sort this based on item number. I can use getline, but don't know how to delimit it based on this type of file. I've tried using get, but don't know how to assemble the individual characters into one array index. Any help would be greatly appreciated, I'm new to this. Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
30 Days Ago
If the item numbers are consecutive (no missing numbers) then you don't need to keep them in an array. Just use the array index of the text array as the item number. One way to do it is like this:
  1. std::string item;
  2. // get item number, which is ended with a period
  3. getline(fin, item, '.');
  4. // get the reset of the line
  5. getline(fin,item);
Last edited by Ancient Dragon; 30 Days Ago at 10:26 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 11
Reputation: Jalwes is an unknown quantity at this point 
Solved Threads: 0
Jalwes Jalwes is offline Offline
Newbie Poster
 
0
  #3
29 Days Ago
Thanks for taking the time to respond to my question. It really helps us newbies. The only problem is that the program also has to have a search function that would allow the user to search for an item based on the item #. That being said, wouldn't I have to have the item number in a separate array? Also, the user will need to be able to enter new item #'s and descriptions, not necessarily in succession. So after the user enters a new item, via append, it will then be sorted based on item number. The whole purpose of this assignment is to learn sorting and searching. Thanks again for taking the time to offer your expertise.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC