File Input with 2D dynamic char array

Dec 5, 2010 at 8:22pm
I need to write a program which compares a string of numbers such as "256432" with an entire list of numbers located on a separate text file.

There's about 1000 different entries on the text file but I'm not sure of the exact amount therefore I want to somehow dynamically allocate an array having the size of it dependant on the amount of entries on the file.

But... Since I want to store it as a char array too it will have to be 2D. So I want to dynamically allocate the size of the array pertaining to the amount of entries but I do know that each field will only be 6 chars long.

How can I possibly allocate the size of the array as I loop through each line of the file until eof()? Also how wll I take in the input from the file and store it into the array? Thanks.
Last edited on Dec 5, 2010 at 8:24pm
Dec 5, 2010 at 9:32pm
why not just make a struct to include both data types, then make a single dimensional array to account for that, or you could define the first dimension then have the second dynamic.
Dec 5, 2010 at 11:50pm
Exactly, I want to have the second dynamic as I don't feel like counting up all the fields myself. I do know the length of each entry which is 6. My main issue is the syntax for inputting the values from the file into the 2D array.

Also, how will I be able to count the amount of entries inside the text file in the first place when I don't have an array to sift through each line? (as I'm dynamically allocating)
Last edited on Dec 6, 2010 at 12:12am
Dec 6, 2010 at 12:46am
try using a <vector> of strings. have a look at this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>  //don't forget these!
#include <string>
#include <fstream>

using namespace std;

vector<string> strvec;
string mystr;
ifstream file;

file.open("name of your text file", ifstream::in);

while(getline(file,mystr))
{
     strvec.push_back(mystr);     // this adds the string mystr which is one line of your file to 
                                                  // the vector of strings
}


Dec 6, 2010 at 2:14am
It was actually specified that you cannot use vectors for this particular assignment. Is there a simpler way of determining the amount of lines in a file without using arrays? (since i'm dynamically allocating) I appreciate the help though.
Dec 6, 2010 at 3:35pm
hmm, well you could read through the file once, get the number of lines and then allocate an array of strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

    ifstream file;
    int n = 0;
    string mystr;

    file.open("name of your text file", ifstream::in);

    while(!getline(file,mystr).eof())
    {
        ++n;
    }

    string strarray[n];
    file.seekg(0, ios_base::beg);
    
    n = 0;
    while (!getline(file,mystr).eof())
    {
        strarray[n] = mystr;
        ++n;
    }
Dec 6, 2010 at 3:54pm
actually i think this might be a better solution, just implement a very simple linked list of strings. here's what i came up with:

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
class strList
{
private:
    string m_str;
    strList * next;
public:
    string Get(){return m_str;}
    strList * Add(string p_str)
    {
        if (next != NULL) return NULL;
        next = new strList(p_str);
        return next;
    }
    strList * Next() {return next;}
    strList(string p_str)
    {
        next = NULL;
        m_str = p_str;
    }
    ~strList()
    {
        if(next) delete next;
    }
};


int main()
{
    ifstream file;
    strList * first, *current;
    string mystr;    
    file.open("filename", ifstream::in);

    getline(file,mystr);
    first = new strList(mystr);
    current = first;
    while(!getline(file,mystr).eof())
    {
        current = current->Add(mystr);  //every time a string is added, the pointer is assigned to the new object
    }
    file.close();
    current = first;
    
    while (current != NULL) //this code loops through the list of strings
    {
        mystr = current->Get();
        //code to check if mystr matches
        current = current->Next();
    }
}
Dec 6, 2010 at 7:57pm
Thanks for that! Gave me some great insight.
Topic archived. No new replies allowed.