Input file into array until eof. Help!

I am having an incredibly difficult time wrapping my mind around this concept. Any help is greatly appreciated.

I am asking for help in learning how to read an input file INTO an array that is not specified on how many values there are, So until EOF(end of file). What I have is this and it doesn't seem to display the file:

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
  int main()
{
    const int MAX = 100;
    string masterList[MAX];
    int count = 0;
    ifstream fin;

    //Open the text file, if fails to open, return.
    fin.open("CWC_Master.txt");
    if(!fin){
        cout << "The file failed to open.\n";
        return -1;
    }

    //reading

    while(fin.eof() =!false){
        fin >> masterList[count];
        count++;

    }

    cout << "The data is: ";
    for (count = 0; count < MAX; count++)
        cout << masterList[count] << " " << endl;


I only have MAX set to 100 because thats the maximum amount of entries for the file, but at any given time, the file could have anything below 100. So I need to learn how to read the input file into an array until eof.

Thanks so much for reading and any help you can contribute!
Arrays must have a size known at compile time. Don't use an array.

Use one of the the Standard Library containers, like std::vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream>
#include <vector>

int main()
{
    std::vector numbers;
    if(std::ifstream in ("CWC_master.txt"))
    {
        int x;
        while(in >> x) //loop on the input operation, not eof
        {
            numbers.push_back(x);
        }
    }
    //done, all your numbers are in the vector
}
http://www.cplusplus.com/reference/vector/
http://en.cppreference.com/w/cpp/container/vector
Last edited on
Thanks for the input.

This is for a project and unfortunately were told we cannot use vector :/
Since your professor doesn't want you to learn C++, you will have to do it the C way and dynamically allocate the array. I recommend reading the file twice - the first time to fine out how many values there are and the second time to store them into the array that you dynamically allocate.
I think he wants us to completely understand concepts before moving into other things. Like vector(I have no clue what that is yet)

I changed it to this. And although I am getting the data read from the file, it continuously reads repeated data until it hits the 100 value mark I believe.

Still having trouble reading until 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
 const int MAX = 100;
    string masterList[MAX];
    int count = 0;
    ifstream fin;
    string master;

    //Open the text file, if fails to open, return.
    fin.open("CWC_Master.txt");
    if(!fin){
        cout << "The file failed to open.\n";
        return -1;
    }

    //reading

    while(count <  MAX && fin >> masterList[count]){
            count++;
    }

    cout << "The data is: ";
    for (count = 0; count < MAX; count++)
        cout << masterList[count] << " " << endl;


    return 0;
while(fin >> masterList[count]) count++; would probably be fine. I wouldn't bother with MAX unless you are reading exactly that number.

Similarly, I think count = 0; while(!masterList[count].empty()) { cout << masterList[count] << " " << endl; count++; } would be better than looping through potentially empty strings.
Last edited on
Thecal wrote:
I think he wants us to completely understand concepts before moving into other things. Like vector(I have no clue what that is yet)
The C++ community widely agrees that std::vector should be taught before arrays. Your professor obviously doesn't agree (or doesn't even know he is in the minority).
Thecal wrote:
I changed it to this. And although I am getting the data read from the file, it continuously reads repeated data until it hits the 100 value mark I believe.

Still having trouble reading until EOF.
Don't think about EOF. Pretend it doesn't exist. Erase it from your mind. It is not important.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int x;
std::size_t count = 0;
if(std::ifstream in ("CWC_Master.txt"))
{
    while(in >> x)
    {
        ++count;
    }
}


//...dynamically allocate `arr` to hold `count` elements


std::szie_t i = 0;
if(std::ifstream in ("CWC_Master.txt"))
{
    while((in >> x) && i < count) //just in case the file was changed from last time
    {
        arr[i] = x;
        ++i;
    }
}
Last edited on
It didnt seem to like that. Compiled, but did not display the input file. Heres what I have:

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

int main()
{
    const int MAX = 100;
    string masterList[MAX];
    int count = 0;
    ifstream fin;
    string master;
    string x;

    //Open the text file, if fails to open, return.
    fin.open("CWC_Master.txt");
    if(!fin){
        cout << "The file failed to open.\n";
        return -1;
    }
    else
    {
        while(fin >> x)
        {
            count++;
        }
    }
    int i=0;
    while(fin >> x && i < count)
    {
        masterList[i] = x;
        ++i;
    }



    cout << masterList[i];
    

//    cout << "The data is: ";
//    for (count = 0; count < MAX; count++)
//        cout << masterList[count] << " " << endl;


    return 0;
}
ignore the comments, I commented out something I was trying.
Thecal wrote:
It didnt seem to like that. Compiled, but did not display the input file.
The code I showed to you does not print anything out. I expected you to write that part of the code yourself; it is simple and you have shown the ability to do it in the past.

The file has to be opened twice, that's why the if statement was duplicated.
I couted the array on the code I pasted.
No, on line 33 you access an invalid index and print its value. You are unlucky that your program continues instead of crashing.

Printing out the entire array involves using a loop.
Ah. Which is what I commented out. I apologize. let me try that.
WIth your above code, you only open the file once. After line 22, you are at the end of the file stream and it is in an invalid state. You then try to use the stream in this invalid state on line 25.

Also, you need to look up how to dynamically allocate memory using new[] and delete[] - you should not have any MAX variable.
Last edited on
I took a different approach. Tried using the book to walk me through it. Did exactly what was said. The code is:
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

int main()
{
    const int MAX = 100;
    int totalCount[MAX];
    int count = 0;

    ifstream fin;

    fin.open("CWC_Master.txt");
    if(!fin){
        cout << "The file failed to open.\n";
        return -1;
    }
    while (count < MAX && fin >> totalCount[count])
        count++;

    cout << "The data in the input file is: ";
    for(count = 0; count < MAX; count ++)
        cout << totalCount[count] << " ";
    cout << endl;

    fin.close();

    return 0;

Howerver I am getting some crazy long numbers, where as I should get the data from the file.

Somehow just not grasping this.
The correct solution does not involve lines 4 or 5. Try starting with this as a template:
http://www.cplusplus.com/forum/beginner/162125/#msg824317
Legitimately tried it. Not getting anything.
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
int main()
{
int x;
int count = 0;
ifstream fin;
const int SIZE = 100;
int ARRAY[SIZE];


fin.open("CWC_Master.txt");
if(fin)
{
    while(fin >> x)
    {
        ++count;
    }
}


//...dynamically allocate `arr` to hold `count` elements


int i = 0;
if(fin)
{
    while((fin >> x) && i < count) //just in case the file was changed from last time
    {
        ARRAY[i] = x;
        ++i;
    }
     cout << "The data in the input file is: ";
    int i=0;
    for(i = 0; i < SIZE; i ++)
        cout << ARRAY[i] << " ";
    cout << endl;

}
Said I needed to define array, so I did that, then said that the array had no value, so I set size to 100. finally compiled. Spitting out nothing.
Do not change the content of the if statements, you are changing the logic of the program.

Lines 6 and 7 should not exist.
Last edited on
Topic archived. No new replies allowed.