Reading in only every other line from a file

Hi, I've been trying to figure out how to cout the even lines from a file into one array and the odd lines from the file into another array. I've tried a few things but nothing seems to be working.
Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  void readResp(ifstream &infile, string resp[], string cat[], int &size) {
    string temp;
    int i = 0;
    size == 0;
    temp = i;
    int const maxsize = 25;
    infile.open("pa.txt");
    if (infile.fail()) {
        cout << "Your file did not open" << endl;
    } else {
        for (i = 0; i < maxsize; i++) {
            if (i % 2 == 0) {
                temp = i;
                resp [i] = temp;
            }
                while (getline(infile, temp)) {
                        cout << temp << endl;
                    }
        }
    }   }


Thanks in advance
I can't really follow your code, but I might do it something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void readResp(string resp[], string cat[], int &size) {
    int const maxsize = 25;
    ifstream infile("pa.txt");   // don't pass infile in just to use it as a local variable!
    if (!infile) {
        cout << "Cannot open pa.txt\n";
        return;
    }
    string line;
    int i = 0;
    while (i < maxsize && getline(infile, line)) {
        resp[i] = line;
        if (!getline(infile, line)) {
            // error: missing 2nd line
        }
        cat[i] = line;
        ++i;
    }
    size = i;
}

Last edited on
tpb,

Thanks for replying quickly, I'll try what you did. Can I ask you what specific line makes it so that the array resp[] only reads in the even lines from the file?

For the infile thing, I actually have a few more functions where I'll be using it which is why I had passed it in the parameter.
Last edited on
The idea is that there are two getlines. The first one reads the odd lines, the second one reads the even lines. The way it is, it's actually putting odd lines in resp, but you can switch that around either way.

Actually, it looks like those are "parallel arrays", which are usually better implemented as a single array of structs (or classes). Also, it's always best to use a vector unless there's a good reason not to. So I think you want something like this (still reading odd lines into resp) :

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

struct Record {
    string resp;
    string cat;

    Record(const string& r, const string& c) : resp(r), cat(c) { }
};

vector<Record> readResp(const char *filename) {
    vector<Record> records;
    ifstream infile(filename);
    if (!infile)
        cerr << "Cannot open " << filename << '\n';
    else {
        string line1, line2;
        while (getline(infile, line1) && getline(infile, line2))
            records.push_back(Record(line1, line2));
    }
    return records;
}

int main() {
    vector<Record> records = readResp("pa.txt");

    for (size_t i = 0; i < records.size(); i++)
        cout << records[i].resp << '\n'
             << records[i].cat << '\n';
}

Last edited on
Unfortunately, for this assignment, we can't use vectors, structs, or classes, we have to use arrays. I tried what you said and even tried switching some things around but now my output is that this file did not open.
In that case, more like this:

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

const int MaxRecords = 100;

int readResp(const char *filename, string resp[], string cat[]) {
    ifstream infile(filename);
    int i = 0;
    if (!infile)
        cerr << "Cannot open " << filename << '\n';
    else {
        string line;
        while (i < MaxRecords && getline(infile, line)) {
            resp[i] = line;
            if (getline(infile, line)) {
                cat[i] = line;
                ++i;
            }
        }
    }
    return i;
}

int main() {
    string resp[MaxRecords], cat[MaxRecords];
    int size = readResp("pa.txt", resp, cat);
    for (int i = 0; i < size; i++)
        cout << resp[i] << '\n'
             << cat[i] << '\n';
}

As for the file not opening, that's a problem with the filename or location or permissions....
I'd rather:
1
2
3
4
5
6
7
8
9
string lineR, lineC;
while ( i < MaxRecords &&
        getline(infile, lineR) &&
        getline(infile, lineC) )
{
  resp[i] = lineR;
  cat[i]  = lineC;
  ++i;
}



PS. This seems to be continuation of http://www.cplusplus.com/forum/beginner/245366/
Last edited on
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
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;

const int SIZE = 100;


int readResp ( ifstream &in, string odd[], string even[] )
{
   string line;
   for ( int i = 1; i <= 2 * SIZE; i++ )
   {
      if ( !getline( in, line) ) return i - 1;
      if ( i%2 ) odd [ i   /2] = line;
      else       even[(i-1)/2] = line;
   }
   return 2 * SIZE;
}


int main()
{
   string odd[SIZE], even[SIZE];

   ifstream in( __FILE__ );   assert(in);

   int totalLines = readResp( in, odd, even );
   cout << "Odd lines:\n";
   for ( int i = 1; i <= ( totalLines + 1 ) / 2; i++ ) cout << i << ":\t" << odd[i-1] << '\n';
   cout << "Even lines:\n";
   for ( int i = 1; i <=   totalLines       / 2; i++ ) cout << i << ":\t" << even[i-1] << '\n';
}


Odd lines:
1:	#include <iostream>
2:	#include <string>
3:	using namespace std;
4:	const int SIZE = 100;
5:	
6:	{
7:	   for ( int i = 1; i <= 2 * SIZE; i++ )
8:	      if ( !getline( in, line) ) return i - 1;
9:	      else       even[(i-1)/2] = line;
10:	   return 2 * SIZE;
11:	
12:	int main()
13:	   string odd[SIZE], even[SIZE];
14:	   ifstream in( __FILE__ );   assert(in);
15:	   int totalLines = readResp( in, odd, even );
16:	   for ( int i = 1; i <= ( totalLines + 1 ) / 2; i++ ) cout << i << ":\t" << odd[i-1] << '\n';
17:	   for ( int i = 1; i <=   totalLines       / 2; i++ ) cout << i << ":\t" << even[i-1] << '\n';
Even lines:
1:	#include <fstream>
2:	#include <cassert>
3:	
4:	
5:	int readResp ( ifstream &in, string odd[], string even[] )
6:	   string line;
7:	   {
8:	      if ( i%2 ) odd [ i   /2] = line;
9:	   }
10:	}
11:	
12:	{
13:	
14:	
15:	   cout << "Odd lines:\n";
16:	   cout << "Even lines:\n";
17:	}
Last edited on
Topic archived. No new replies allowed.