Question reading from an input file

I have an input file with different fileds
example...
1
2
3
4
5
6
a|X12|book Case|12
a|PO1|desk lamp|6
a|I99|chair|23
S
a|XR8
S|RR7|cup|9

How do i go about reading the fields in and putting them into variables?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ifstream infile("items.d1");

        string test;
        if(infile.is_open())
        {
                string test;
                while(!getline(infile, test, '|').eof())
                {
                        string stockNum;
                        string item;
                        string num;
                        getline(infile,stockNum , '|');
                        getline(infile, item, '|');
                        getline(infile, num);
                        cout << stockNum << " " << item
                                << " " << num << endl;
                 }
         infile.close();
         }
         else
             cout << "error";

What i have works until i get to the 4th field then it gets messed up. Also how would i read in the last field, the num, into an int variable instead of making it a string.
Perhaps you should consider reading the whole line and using strtok().
Helios has the right idea. Just use getline() twice: once to read the whole line, and once to split the line. (The getline() function is basically a better strtok().)

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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

typedef string          field;
typedef vector <field>  record;
typedef vector <record> csvdata;

istream& operator >> ( istream& ins, record& r )
  {
  string s;
  r.clear();
  if (getline( ins, s ))
    {
    istringstream iss( s );
    while (getline( iss, s, '|' ))
      r.push_back( s );
    }
  return ins;
  }

istream& operator >> ( istream& ins, csvdata& data )
  {
  record r;
  while (ins >> r) data.push_back( r );
  return ins;
  }

int main()
  {
  ...
  ifstream inf( "items.dl" );
  csvdata items;
  inf >> items;
  if (!inf.eof())
    {
    cerr << "Something went wrong.\n";
    ...
    }
  inf.close();
  ...
  }

Note: I haven't tested this. Stoopid errors and/or typos may have occurred.

Hope this helps.
Last edited on
Thats kind of what i was thinking as far as a second getline() call. I do appreciate your reply but your code is over my head. I thought i had it working but when i run it, it prints every other line. Why would it skip every other line?
1
2
3
4
5
6
a|X12|book Case|12
a|PO1|desk lamp|6
a|I99|chair|23
P
a|XR8|cat food|4
S|RR7|cup|9


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
int main()
{
        Entry person;
        ifstream infile("prog7.d1");

        string test;
        if(infile.is_open())
        {

                string test;

                while(!getline(infile, test).eof())
                {
                        if(test[0] == 'a')
                        {
                                string check;
                                string key;
                                string num;
                                string des;
                                getline(infile, check, ':');
                                getline(infile, key, ':');
                                getline(infile, num, ':');
                                getline(infile, des, ':');

                                cout << key << " " << num << " "
                                        << des << endl;
                        }

                }

        infile.close();
        }
        else
                cout << "File didnt open" << endl;
Topic archived. No new replies allowed.