Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

c++ - How to read-write into/from text file with comma separated values

How do I read data from a file if my file is like this with comma separated values

1, 2, 3, 4, 5

6, 7, 8, 9, 10



and after reading the file, I want to write the data back into other file as same format above.

I can get total number of lines, using

string line;
while(!file.eof()){
      getline(file,line);
      numlines++;
    }
    numline--; // remove the last empty line

but how can I know total number of digits in a row/line ??

I also have vector of ints to store the data. So, I want to read the first line and then count total number of elements in that line, here 5 (1,2,3,4,5) and store them in array/vector, and read next line and store them in vector again and so on till I reach EOF.

Then, I want to write the data to file, again, I guess this will do the job of writing data to file,

numOfCols=1;
for(int i = 0; i < vector.size(); i++)
{
    file << vector.at(i);
    if((numOfCols<5) file << ",";//print comma (,)
    if((i+1)%5==0)
    {
                  file << endl;//print newline after 5th value
                  numOfCols=1;//start from column 1 again, for the next line
    }
    numOfCols++;
}
file << endl;// last new line

So, my main problem is how to read the data from file with comma separated values ??

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Step 1: Don't do this:

while(!file.eof())
{
    getline(file,line);
    numlines++;
}
numline--; 

The EOF is not true until you try and read past it. The standard pattern is:

while(getline(file,line))
{
    ++numline;
}

Also note that std::getline() can optionally take a third parameter. This is the character to break on. By default this is the line terminator but you can specify a comma.

while(getline(file,line))
{
    std::stringstream   linestream(line);
    std::string         value;

    while(getline(linestream,value,','))
    {
        std::cout << "Value(" << value << ")
";
    }
    std::cout << "Line Finished" << std::endl;

}

If you store all the values in a single vector then print them out using a fixed width. Then I would do something like this.

struct LineWriter
{
        LineWriter(std::ostream& str,int size)
                :m_str(str)
                ,m_size(size)
                ,m_current(0)
        {}

        // The std::copy() does assignement to an iterator.
        // This looks like this  (*result) = <value>;
        // So overide the operator * and the operator = to
        LineWriter& operator*() {return *this;}   
        void operator=(int val)
        {
                ++m_current;
                m_str << val << (((m_current % m_size) == 0)?"
":",");
        }

        // std::copy() increments the iterator. But this is not usfull here
        // so just implement too empty methods to handle the increment.
        void operator++()       {}
        void operator++(int)    {}

        // Local data.
        std::ostream&           m_str;
        int const               m_size;
        int                     m_current;
};

void printCommaSepFixedSizeLinesFromVector(std::vector const& data,int linesize)
{
    std::copy(data.begin(),data.end(),LineWriter(std::cout,linesize));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...