简体   繁体   中英

Reading a text file using VC++

I need to read a text file which is for example like bottom :

8.563E+002 2.051E+004 4.180E-004 7.596E-001 5.260E-005 6.898E-002 1.710E-001 8.053E-011 2.686E-013 8.650E-012

each of this 10 scientific digits are the specific value of one line it means each line contains 10 value like above, There is one such line for every grid point in each file. The X indices value most rapidly, then Y, then Z; the first line in the file refers to element (0,0,0); it means the first 10 values presents the first line which refers to element (0,0,0) and the second line (second 10 values) to second element (1,0,0); the last to element (599,247,247).

I don't know how can I write the code for this file using visual C++ ,what I know is I have to read this file line by line which can be determined by eliminating 10 values and tokenize it , then I have to create the xyz for each line il end of the line. I know the concept but I don't know How can I code it in visual C++ .. I need to submit it as my homework .. I really welcome every help .. Thanks

core part can look like:

std::ifstream is("test.txt");
std::vector<double> numbers;
for(;;) {
    double number;
    is >> number;
    if (!is)
        break;
    numbers.push_back(number);
}

I do not have here MSVC but GCC 4.3. I hope this code helps:

#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <iterator>

using namespace std;

class customdata
{
friend istream& operator>>(istream& in, customdata& o);
friend ostream& operator<<(ostream& out, const customdata& i);

public:
    customdata()
        : x(0), y(0), z(0)
    {}

    customdata(const customdata& o)
        : x(o.x), y(o.y), z(o.z)
    {}

    customdata& operator=(const customdata& o)
    {
        if (this != &o)
        {
            x = o.x;
            y = o.y;
            z = o.z;
        }
        return *this;
    }

private:
    long double x, y, z;
};

istream& operator>>(istream& in, customdata& o)
{
    in >> o.x >> o.y >> o.z;
    return in;
}

ostream& operator<<(ostream& out, const customdata& i)
{
    out << "x=" << i.x << " y=" << i.y << " z=" << i.z;
    return out;
}

// Usage: yourexec <infile>
int main(int argc, char** argv)
{
    int exitcode=0;
    if(argc > 1)
    {
        ifstream from(argv[1]);
        if (!from)
        {
            cerr << "cannot open input file " << argv[1] << endl;
            exitcode=1;
        }
        else
        {
            list<customdata> mydata;
            copy(istream_iterator<customdata>(from), istream_iterator<customdata>(), back_inserter(mydata));
            if(mydata.empty())
            {
                cerr << "corrupt input data" << endl;
                exitcode=3;
            }
            else
                copy(mydata.begin(), mydata.end(), ostream_iterator<customdata>(cout, "\n"));
        }
    }
    else
    {
        cerr << "insufficient calling parameters" << endl;
        exitcode=2;
    }
    return exitcode;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM