Basic I/O file stream

So, here is my problem. I need to do a ifstream of the variables down below, but I don't know how to do it since the variables are varying in length such 2 to 3 letter longs with spaces in between and there is a comma. Also I cannot change the variables, and it must be kept the same. If everything was standardized I could've done

fileIn >> name >> num >> item >> num >> cost >> totalPrice;

But since the names are varying in length such as 2 to 3 letters long, I thought I needed to do a getline, but I have no idea how to attach the rest of the information after the getline. Since if I did getline(fileIn, name); it would have gotten the entire line, which is wrong since I need to add up the item numbers and total price of the contributing farms.

I have no previous background in C++, so please dumb it down as you would explain it to a highschooler.

variable format is

farm name, item number, item, item cost, total price.

Collins Farm, 43900 tomatoes 0.67 29413

Bart Smith Farms, 34910 cassavas 0.99 34560.9
Last edited on
Actually I would do a getline. Then using string.erase find the first comma, Copy everything on the left to the first value and the rest of the line to the second value. Then look for the space and once again, copy the right to the next value and so forth.
Use the three-parameter form of getline (http://www.cplusplus.com/reference/string/string/getline/), with delimiter a comma, to read the first part of the line into a string.

Use the two-parameter form of getline(same reference) to read the rest of the line into a string. Then use a stringstream (internal stream) to stream this into the other variables which are only separated by spaces.

If there is a possibility that your data lines are followed by a blank line (as here) then you can rid the input buffer of the unwanted white space (including newline) by using ws (http://www.cplusplus.com/reference/istream/ws/)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;


int main()
{
   string farm, name;
   string restOfLine;
   int num;
   double cost, total;

   ifstream inFile( "data.txt" );
   while ( getline( inFile, farm, ',' ) && getline( inFile, restOfLine ) && inFile >> ws )
   {
      stringstream( restOfLine ) >> num >> name >> cost >> total;

      cout << "Farm: "        << farm  << '\n'
           << "Number: "      << num   << '\n'
           << "Name: "        << name  << '\n'
           << "Unit cost: "   << cost  << '\n'
           << "Total price: " << total << '\n'
           << "\n";
   }
   inFile.close();
}


data.txt:
Collins Farm, 43900 tomatoes 0.67 29413

Bart Smith Farms, 34910 cassavas 0.99 34560.9 


Output:
Farm: Collins Farm
Number: 43900
Name: tomatoes
Unit cost: 0.67
Total price: 29413

Farm: Bart Smith Farms
Number: 34910
Name: cassavas
Unit cost: 0.99
Total price: 34560.9




If you wanted to take this further, then, since all your data fields are heavily related, you might want to (a) create a struct to hold them; (b) overload the stream operators << and >>, which makes subsequent coding a little tidier.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

//======================================================================

struct Produce
{
   string farm;
   int num;
   string name;
   double cost;
   double total;
};

istream &operator >> ( istream &strm, Produce &p )
{
   string restOfLine;
   getline( strm, p.farm, ',' );
   getline( strm, restOfLine );
   stringstream( restOfLine ) >> p.num >> p.name >> p.cost >> p.total;
   return strm >> ws;
}

ostream &operator << ( ostream &strm, const Produce &p )
{
   strm << "Farm: "        << p.farm  << '\n'
        << "Number: "      << p.num   << '\n'
        << "Name: "        << p.name  << '\n'
        << "Unit cost: "   << p.cost  << '\n'
        << "Total price: " << p.total << '\n';
   return strm;
}

//======================================================================

int main()
{
   Produce p;
   ifstream inFile( "data.txt" );
   while ( inFile >> p ) cout << p << "\n";
   inFile.close();
}



BTW, it is redundant to specify 'total', since it can be obtained from num and cost.
Last edited on
Topic archived. No new replies allowed.