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:
int main()
{
constint 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
}
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 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.
constint 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;
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.
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;
}
}
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.
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.
int main()
{
constint 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.
int main()
{
int x;
int count = 0;
ifstream fin;
constint 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;
}