Read chars from a text file

I have a text file that contains a list of names seperated by a space and ends with a dash: Like this:
Pete Vince Jam March June October  -


I need to explode this list into an array of char arrays, where each element is one of the names. I know this is easy to do but I am stuck.

after opening to file here is my 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
35
36
37
38
39
40
41
42
43
44
45
...
            fin->seekg(0, ios::beg); //set to begining of file to count id names
            int nNames = 0; //total amount of names listed
            int nChar = 0;
            while(fin->good()) { //reads chars until dash, checks for spaces to tally name count
                char curr = fin->get();
                nChar += 1;
                if(curr == '-') //check if end line
                    break;
                else if(curr == ' ') //if a space then count as new name
                    nNames += 1;
            }

            //fin->seekg(0, ios::beg); //go back to the begining to read names

            char** ids[nNames]; //will store array of names
            int start = 0; //keeps track of starting index of the name

            for(int i = 0; i < nNames; i++) {
                fin->seekg(0, ios::beg); //go back to begining to read name
                fin->ignore(start); //skip to start of name

                int length = 0; //get current position of name char
                while(fin->good()) {
                    char curr = fin->get();
                    if(curr != ' ' && curr != '-') {
                        length += 1; // count size of name
                    } else {
                        break;
                    }
                }
                if (length > 0) {
                    fin->seekg(0, ios::beg); //go back to begining to read name
                    fin->ignore(start); //skip to start of name
                    cout << "s: " << start << "  l: " << length << "   ";
                    start += length + 1; //set start for the next name char index in file

                    char* name = new char[length+1]; //create one larger for termination character
                    fin->read(name,length);
                    name[length] = '\0'; //terminate name for proper display

                    *ids[i] = name; //HOW CAN I DEFINE THIS?
                }
            }
            }


The last line where I try to read the chars from the file causes an issue. I think it is how I have my ids[] array declared?
Last edited on
Line 16 is supposed to be a compile error. It should be:

char** ids = new char *[nNames]; //will store array of names


Line 14 is also supposed to be a compile error. It should be:

ids[i] = name; //Note no * before ids since you want to store pointer and ids is a array of pointer
Ah, I knew it was something incredibly simple and stupid I did. References are a love-hate thing with me. Thank you! Also, I found a much much simpler way of reading the names into a char[]!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
            fin->seekg(0, ios::beg); //set to begining of file to count id names
            nIds = 0; //total amount of names listed
            while(fin->good()) { //reads chars until dash, checks for spaces to tally name count
                char curr = fin->get();
                if(curr == '-') //check if end line
                    break;
                else if(curr == ' ') //if a space then count as new name
                    nIds += 1;
            }

            fin->seekg(0, ios::beg); //go back to the begining to read names

            ids = new char*[nIds]; //will store array of names
            for(int i = 0; i < nIds; i++) {
                char* name;
                *fin >> name;
                if(name[0] != '-') {
                    ids[i] = name;
                    cout << ids[i];
                }
            }
Topic archived. No new replies allowed.