Reading txt to array

How do I read the elements in columns (item and quantity recorded) and save them to an array ? the txt file is as shown:

Item Price Quantity Rec'd Date
__________________________________________________________________________
Router 100 10 xx

Digital Multimeter 200 400 xx

Network Card 33.50 550 xx
Last edited on
are there any numbers in any of the items in your file, or could there be?
you need a way to know when to stop reading the 'item' column. if there are no digits in it, you can use that. If there are digits, you may have to read it as a line and iterate it backwards looking for spaces to pick off the columns, everything left after the third (from backwards) space is the item...
When you have a specific structure, use a struct.

1
2
3
4
5
6
7
8
9
typedef decltype(std::chrono::system_clock::now()) DateTime;

struct record
{
  std::string item;
  double      price;
  int         quantity;
  DateTime    date;
};

Create a function to read a single record from file. JSYK input is hard, so you need to be careful to split stuff up correctly.

It is especially hard if there are no clear delimiters between items. Is it TAB-delimited? That would make your life about a billion times easier. If not, it can still be done, though. (I did it. It was fun!)

Let us know what separates columns, or if it is just spaces.
Topic archived. No new replies allowed.