3

I am trying to write a C++ program, but I am not familiar with C++. I have a .txt file, which contains values as follows:

0
0.0146484
0.0292969
0.0439453
0.0585938
0.0732422
0.0878906

What I have done in my C++ code is as follows:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    string line;
    ifstream myReadFile;
    myReadFile.open("Qi.txt");
    if(myReadFile.is_open())
    {
        while(myReadFile.good())
        {
            getline(myReadFile,line);
            cout << line << endl;
        }
        myReadFile.close();
    }
    return 0;
}

I would like to make the output of the program an array, i.e.

line[0] = 0
line[1] = 0.0146484
line[2] = 0.0292969
line[3] = 0.0439453
line[4] = 0.0585938
line[5] = 0.0732422
line[6] = 0.0878906

3 Answers 3

8

Assuming you want your data stored as floating point numbers (not strings) you probably want to do something like this:

#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>

int main() { 
    std::ifstream in("Qi.txt");

    // initialize the vector from the values in the file:
    std::vector<double> lines{ std::istream_iterator<double>(in),
                               std::istream_iterator<double>() };

    // Display the values:
    for (int i=0; i<lines.size(); i++)
         std::cout << "lines[" << i << "] = " << lines[i] << '\n';
}

Just a quick note on style: I prefer to see variables fully initialized right when you create them, so std::ifstream in("Qi.txt"); is preferable to std::ifstream in; in.open("Qi.txt");. Likewise, it's preferable to initialize the vector of lines directly from istream iterators rather than create an empty vector, then fill it in an explicit loop.

Finally, note that if you insist on writing an explicit loop anyway, you never want to use something like while (somestream.good()) or while (!somestream.eof()) to control your loop -- these are mostly broken, so they don't (dependably) read a file correctly. Depending on the type of data involved, they'll frequently appear to read the last item from the file twice. Usually, you want something like while (file >> value) or while (std::getline(file, somestring)). These check the state of the file immediately after reading, so as soon as reading fails they fall out of the loop, avoiding the problems of the while (good()) style.

Oh, as a side note: this is written expecting a compiler that (at lest sort of) conforms with C++11. For an older compiler you'd want to change this:

    // initialize the vector from the values in the file:
    std::vector<double> lines{ std::istream_iterator<double>(in),
                               std::istream_iterator<double>() };

...to something like this:

    // initialize the vector from the values in the file:
    std::vector<double> lines(( std::istream_iterator<double>(in)),
                                std::istream_iterator<double>() );
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for including the style notes. Very helpful.
What will the vector "lines" store when the iterator encounters a blank line?
I think one should #include <fstream> to compile.
@max: Yup. Corrected.
@max: A default-constructed istream_iterator is basically "end of file"--that is, it'll compare equal to an iterator into a stream only when the stream has been read all the way to the end of the file.
|
5

First you'll need a vector:

std::vector<std::string> lines; // requires #include <vector>

Then you'll need to take a string taken from the getline operation, and push it back into the vector. It's very simple:

for (std::string line; std::getline(myReadFile, line);) 
{
    lines.push_back(line);
}

For an output operation, all you need is:

{
    int i = 0;

    for (auto a : lines)
    {
        std::cout << "lines[" << i++ << "] = " << a << std::endl;
    }
}

4 Comments

Hehehe. A vector is essentially a line. So you put the result of getline into the vector.
when I compile, I got the error message "warning: auto will change meaning in c++0x; please remove it"
@IbnuSyuhada Compile with -std=c++11 and see what happens.
@IbnuSyuhada: He's intending the meaning of auto as defined in C++11. It sounds like you might want to update your compiler, though the proper flags may get it to interpret auto correctly.
0
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
string line;
ifstream myReadFile;
myReadFile.open("Qi.txt");
if(myReadFile.is_open())
{
  for(int i=0;i<7;++i)
    if(myReadFile.good())
    {
        getline(myReadFile,line);
        cout<<"line["<<i<<"] = " << line << endl;
    }
    myReadFile.close();
}
return 0;
}

1 Comment

Code only answers are discouraged. Consider adding a bit of explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.