user3738495
user3738495

Reputation: 1

reading in from file to an array C++

I've been trying to perform what should be the relatively simple task of reading a line of text from a file and saving it into an array. Unfortunately this has just not been working and instead the program simple does not read anything in at all. This is the sort of code I am trying to use:

ifstream in_stream;
int x=0;
string array[150]
in_stream.open("file.txt");

while(!in_stream.eof()){
    in_stream>>array[x];
    x++;
}

I have also tried to use getline as below:

ifstream in_stream;
int x=0;
string array[150]
in_stream.open("file.txt");

while(!in_stream.eof()){
    getline(in_stream, array[x]);
    x++;
}

Neither mode works and will not read anything into the array but rather leaves it blank... I am just not sure what is wrong so if someone can help that'd be grand!

Upvotes: 0

Views: 97

Answers (2)

user3416290
user3416290

Reputation:

Look, that's my file.txt's content:

ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi

10 lines exactly

and that's the data stored in an array of size == 20:

ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi
ifojgoidfjjfdogdifjgdjgdoigjdoijdofi

10 lines too!

If you use getline, each line will be stored in a position into the array. If the size of your file is not large enough and/or the size of your array is too large instead, you will only see blank spaces being printed out. First, make sure your array is short enough or your file's size is large enough. Anyway, getline's solution can work. I didn't test ifstream's solution, but the case would be similar to the first one.

Hope it helps!

Upvotes: 0

stix
stix

Reputation: 1146

It's important to check that the file actually opened successfully before attempting to read from it, as in your case if the file failed to open you would not begin populating the array.

As christianm said, it's better to use a std::vector, as it will allow you to handle a file of any size. As such, my recommendation would be to refactor your code as follows:

std::ifstream in_stream("file.txt");

if(!in_stream.good())
{
    std::cerr << "Error opening file!" << std::endl;
    //More error handling code goes here
}

std::vector<std::string> lines;
while(!in_stream.eof())
{
    std::string line;
    std::getline(in_stream, line);
    lines.push_back(line);
}

std::cout << "Read: " << lines.size() << " lines." << std::endl;

One thing to keep in mind about ifstream is that it will open the string you pass to it as a relative path in your test case. This means that you have to start the program from the same directory where file.txt is contained, change the working directory after the program starts, or feed a fully qualified path.

Upvotes: 1

Related Questions