ifstream

I am trying to read some data from a text file, and place it into a structure. The text file consists of an integer number followed by two strings. Written in "columns", for example:

12 qwert yuiop
13 asdfg hjkl

I wanted to create a new structure for each line of data. But I have been getting a core dump error unless I hard code numbers into the new object. What am I doing wrong?.. and is there also a way to keep the structures after they fall out of scope of the while(!data.eof())


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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


struct Information
{
    Information() {}
    string strInfo1;
    string strInfo2;
    int intInfo;
};

int main()
{
    ifstream data("input.txt");
    int i(1);

    while(!data.eof())
    {

        Information* objInfo = new Information[i];

        data>>objInfo[i].intInfo;
        data>>objInfo[i].strInfo1;
        data>>objInfo[i].strInfo2;

        cout<<objInfo[i].intInfo<<" ";
        cout<<objInfo[i].strInfo1<<" ";
        cout<<objInfo[i].strInfo2<<endl;
        i++; // so it can create the next object
    }

    return 0;
}
Line 24: memory leak here: You are creating completely new array (losing all old data in process) and do not delete old (leaking memory)
Line 26-32: Out of bound access — Array of size i holds values from 0 to i-1.
Line 21: Use of eof usually leads to errors.

I suggest using automatic containers like vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    ifstream data("input.txt");
    std::vector<Information> objInfo;
    {
        Information temp;
        while(data >> temp.intInfo >> temp.strInfo1 >> temp.strInfo2) {
            objInfo.push_back(temp);
            std::cout << temp.intInfo  << " " <<
                         temp.strInfo1 << " " <<
                         temp.strInfo2 << std::endl;
        }
    }
    return 0;
}
I thought I'd repost before you deleted it as you did previously. Please do not delete your posts.


@coal
I am trying to read some data from a text file, and place it into a structure. The text file consists of an integer number followed by two strings. Written in "columns", for example:

12 qwert yuiop
13 asdfg hjkl

I wanted to create a new structure for each line of data. But I have been getting a core dump error unless I hard code numbers into the new object. What am I doing wrong?.. and is there also a way to keep the structures after they fall out of scope of the while(!data.eof())
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


struct Information
{
    Information() {}
    string strInfo1;
    string strInfo2;
    int intInfo;
};

int main()
{
    ifstream data("input.txt");
    int i(1);

    while(!data.eof())
    {

        Information* objInfo = new Information[i];

        data>>objInfo[i].intInfo;
        data>>objInfo[i].strInfo1;
        data>>objInfo[i].strInfo2;

        cout<<objInfo[i].intInfo<<" ";
        cout<<objInfo[i].strInfo1<<" ";
        cout<<objInfo[i].strInfo2<<endl;
        i++; // so it can create the next object
    }

    return 0;
}
Topic archived. No new replies allowed.