Problem with Vector

Hi,

I'm having a problem returning values from a vector. My program is simple, it reads lines of text from a file and stores it a vector. Everything appears to work fine, however, when I try to return a value I get empty text. Here is the
code:

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 <vector>

using namespace std;

const int ROW_LEN = 80;

int main(void) {

    vector <char *>rs;
    char row[ROW_LEN+1];
    
    ifstream dbf;

    dbf.open("D:\\departments.dbf");

    while (! dbf.eof()) {
        dbf.getline(row,ROW_LEN);
	rs.push_back(row);
    }

    for (int index=0; index<=rs.size()-1; index++) {
        cout << "[" << rs[rs.size()-1] << "]" << endl;
    }

    dbf.close();

    return 0;

}


The file "departments.dbf" is a flat ASCII file that contains the following:

1
2
3
4
1,MECHANICAL HANDLING
2,GEAR CUTTING
3,ENGINEERING
4,WELDING


The output for the program is:

1
2
3
4
[]
[]
[]
[]


Now if I change this line:

 
rs.push_back(row);


to this:

 
rs.push_back("a line of text");


I get this output:

1
2
3
4
[a line of text]
[a line of text]
[a line of text]
[a line of text]


I'm sure its something really simple, any help would be appreciated. Many Thanks

This says that rs is a vector of "address of a char"
 
vector <char *>rs;


This says row is an array of chars of length ROW_LEN + 1
 
char row[ROW_LEN+1];


This says read a line from stream dbf into char array row, using upto ROW_LEN entries, overwrite whatever's there
 
dbf.getline(row,ROW_LEN);


This says push the address of the first element of row to the end of rs, grow it rs if necessary
 
rs.push_back(row);

You should note here that the address of row doesn't change, but the content of row changes each time through the loop. Do you understand what's happening?

A char* is the address of a char, it is not a string. If you want a string, C++ provides a string class, use that.
on line 22 you always push_back the pointer to 'row' which points always to the same memory. The last line is empty (when you reach eof) and so you have always an empty string. The problem is that you just copy the pointer and not the content.

Many Thanks for your advice. I've changed the program to use a string:

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 <vector>

using namespace std;

const int ROW_LEN = 80;


int main(void) {

    vector <string>rs;
    string row;
    ifstream dbf;

    dbf.open("D:\\departments.dbf");

    while (! dbf.eof()) {
        dbf.getline(row,ROW_LEN);
	rs.push_back(row);
    }

    for (int index=0; index<=rs.size()-1; index++) {
        cout << "[" << rs[rs.size()-1] << "]" << endl;
    }

    dbf.close();

    return 0;

}


However, when I compile it, I get the following error message:

1
2
3
4
5
6
7
8
9
Compiling...
vector.cpp
D:\dev\c++\vector.cpp(22) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int)' : cannot convert parameter 1 from 'class std::basic_str
ing<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

vector.obj - 1 error(s), 0 warning(s)



Is this because getline() expects "char" not a string?
Replace
 
dbf.getline(row,ROW_LEN);
with
 
getline(dbf, row);


Why does your loop at the end print the last element only?

I hadn't changed it from when I was trying various things to debug it earlier. Many Thanks

Finally, don't write:
1
2
3
4
5
6
7
    ifstream dbf;

    dbf.open("D:\\departments.dbf");

    while (! dbf.eof())

    dbf.close();


Use (without calling close):
1
2
3
    ifstream dbf("D:\\departments.dbf");

    while (dbf)

Topic archived. No new replies allowed.