久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用逗號分隔值讀寫文本文件

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 ??

我也有整數向量來存儲數據.所以,我想讀取第一行,然后計算該行中元素的總數,這里是 5 (1,2,3,4,5) 并將它們存儲在數組/向量中,然后讀取下一行并將它們存儲在向量中以此類推,直到達到 EOF.

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 ??

謝謝

推薦答案

第一步:不要這樣做:

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

在您嘗試閱讀它之前,EOF 是不正確的.標準模式是:

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

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

另請注意,std::getline() 可以選擇采用第三個參數.這是要突破的角色.默認情況下,這是行終止符,但您可以指定逗號.

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));
}

這篇關于如何使用逗號分隔值讀寫文本文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 国产日韩欧美一区二区在线播放 | 亚洲视频在线免费 | 亚洲3p | 精品一二三区 | 久久久久久久久久久久91 | 91久久综合亚洲鲁鲁五月天 | 久久久久久国产精品 | 免费a网 | 人人射人人 | 亚州激情| 狠狠草视频 | 精品毛片 | 国产激情视频 | 日韩中文欧美 | 综合伊人 | 亚洲a视频 | 色资源在线 | 亚洲综合视频一区 | 中文字幕一区在线观看视频 | 国产精品a久久久久 | 日韩成人在线电影 | 一区二区三区av | 久久精品亚洲欧美日韩久久 | 日韩视频一区二区三区 | 日本h片在线观看 | 国产激情在线 | 亚洲综合久久精品 | 狠狠躁夜夜躁人人爽天天高潮 | h片在线观看网站 | 一区二区三区免费在线观看 | 成人在线视频一区 | 一区二区三区四区在线播放 | 久久久夜夜夜 | 在线观看国产视频 | 日本精品视频 | 国产区视频在线观看 | 一区二区三区四区视频 | 成人羞羞国产免费视频 | 日本三级日产三级国产三级 | 成人久草 | 精品国产99 |