Help with program.

I am having issue with my program below. When I run my program it only produces zeros. It doesn't give me the information I need.

I think my problem lies with the Dynamic Memory Allocation? I am fairly new to C++. I am currently taking my 2nd computer science course.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

#define die(msg) {cerr << msg << endl; exit(1);}

using namespace std;

struct Eip
{
    string col1;
    string col2;
    string col3;
    string col4;
};

int showSize(fstream &);
void inputData(fstream &, Eip [], int);
void outputData(fstream &, Eip [], int);

int main()
{
    fstream fs("Book1.txt", ios::in);
    fstream fsout("test.txt", ios::out);

    if(!fs.is_open()) die("Can not open Book1.txt!");
    if(!fsout.is_open()) die("Can not open test.txt!");

    int size = showSize(fs);
    Eip *name;
    name = new Eip[size];

    inputData(fs, name, size);
    outputData(fsout, name, size);

    fs.close();
    fsout.close();
    return(0);
}

int showSize(fstream &fs)
{
    string line;
    int count = 0;
    while(getline(fs,line)) {
        count++;
    }
    return count;
}

void inputData(fstream &fs, Eip name[], int size)
{
    for(int i = 0; i < size; i++) {
        getline(fs, name[i].col1, '\t');
        getline(fs, name[i].col2, '\t');
        getline(fs, name[i].col3, '\t');
        getline(fs, name[i].col4, '\n');
    }
}

void outputData(fstream &fsout, Eip name[], int size)
{
    for(int i = 1; i < size; i++) {
        fsout << setw(4) << setfill('0');
        fsout << name[i].col1;

        if(name[i].col2 == "")
            fsout << " ";
        else
            fsout << name[i].col2;

        fsout << setw(3) << setfill('0');
        fsout << name[i].col3;

        if(name[i].col4 == "")
            fsout << "  " << endl;
        else
            fsout << name[i].col4 << " " << endl;
    }
}



I think my problem lies within

1
2
3
    int size = showSize(fs);
    Eip *name;
    name = new Eip[size];


If I change the above to:

1
2
3
4
5
6
    int size;
    cout << "How many lines? ":
    cin >> size;

    Eip *name;
    name = new Eip[size];


That will display the information I need. I don't want to have the user input the number of lines within the text file.

After calling showSize() function file pointer will be at the end of the file. What do you think inputData() is going to read now?

Instead of manually allocating memory (which is easy to get wrong: your code has a memory leak) use one of the safe C++ containers which can grow in size to accomodate new values automatically. For example std::vector.
Thanks MiiniPaa for the suggestion. I will use vector instead.

I have another question for you. Why would:

1
2
3
4
5
6
    int size;
    cout << "How many lines? ":
    cin >> size;

    Eip *name;
    name = new Eip[size];


work with my program and not:

1
2
3
    int size = showSize(fs);
    Eip *name;
    name = new Eip[size];


Am I maunally allocating memory with both? Is there a memory leak if I use the first method as well? I am just a little confused with memory allocation.
Last edited on
I said that before:
MiiNiPaa wrote:
After calling showSize() function file pointer will be at the end of the file. What do you think inputData() is going to read now?
After you call showSize(), it reads everything in file. There is nothing left to read. You need to rewind file back to the beginning (or not call this function in the first place and use something which does not need to know size beforehand like vector)

Is there a memory leak if I use the first method as well?
Yes. Each new should have corresponding delete and new[]delete[].
Thanks MiinIPaa!

I tried using vector and it worked!!!

Your explanation below helped me understand why it was reading zeros.

"After you call showSize(), it reads everything in file. There is nothing left to read. You need to rewind file back to the beginning (or not call this function in the first place and use something which does not need to know size beforehand like vector)"

I really appreciate you taking the time to help with my issue.

Thanks again!
Topic archived. No new replies allowed.