C++ 如何使用逗号分隔值读写文本文件

C++ 如何使用逗号分隔值读写文本文件,c++,csv,C++,Csv,如果我的文件是这样的,并且有逗号分隔的值,那么如何从文件中读取数据 1, 2, 3, 4, 5\n 6, 7, 8, 9, 10\n \n 在读取文件后,我想把数据写回另一个文件,格式与上面相同 我可以使用 string line; while(!file.eof()){ getline(file,line); numlines++; } numline--; // remove the last empty line 但是我怎么知道一行中的总位数呢

如果我的文件是这样的,并且有逗号分隔的值,那么如何从文件中读取数据

1, 2, 3, 4, 5\n
6, 7, 8, 9, 10\n
\n
在读取文件后,我想把数据写回另一个文件,格式与上面相同

我可以使用

string line;
while(!file.eof()){
      getline(file,line);
      numlines++;
    }
    numline--; // remove the last empty line
但是我怎么知道一行中的总位数呢

我还有整数向量来存储数据。 所以,我想读取第一行,然后计算该行中的元素总数,这里是5(1,2,3,4,5),并将它们存储在数组/向量中,然后读取下一行并再次存储在向量中,依此类推,直到达到EOF

然后,我想将数据写入文件,同样,我想这将完成将数据写入文件的工作

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
numofols=1;
对于(int i=0;i
while(!file.eof())
{
    getline(file,line);
    numlines++;
}
numline--; 
只有当你试着把它读过去,EOF才是真的。 标准模式是:

while(getline(file,line))
{
    ++numline;
}
还要注意,
std::getline()
可以选择使用第三个参数。这是要打断的字符。默认情况下,这是行终止符,但可以指定逗号

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

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

}
while(getline(文件,行))
{
std::stringstream linestream(行);
std::字符串值;
while(getline(linestream,value,,'))
{
std::cout尝试以下课程:

#包括

只是,查找Stroustrup的《C++程序设计语言专版》第641页的例子。


它对我来说很有效,我花了很长时间试图弄明白这一点。

我在这里发布了CSV读写代码。我已经检查了它的工作情况

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

void readCSV(istream &input, vector< vector<string> > &output)
{
   string csvLine;
    // read every line from the stream
    while( getline(input, csvLine) )
    {
            istringstream csvStream(csvLine);
           vector<string> csvColumn;
            string csvElement;
            // read every element from the line that is seperated by commas
            // and put it into the vector or strings
            while( getline(csvStream, csvElement, ',') )
            {
                    csvColumn.push_back(csvElement);
            }
            output.push_back(csvColumn);
    }
}

int main()
{
    ofstream myfile;
    string a;
    fstream file("b.csv", ios::in);
    myfile.open ("ab.csv");
    if(!file.is_open())
    {
           cout << "File not found!\n";
            return 1;
    }
    // typedef to save typing for the following object
    typedef vector< vector<string> > csvVector;
    csvVector csvData;

    readCSV(file, csvData);
    // print out read data to prove reading worked
    for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
    {
            for(vector<string>::iterator j = i->begin(); j != i->end(); ++j)
            {
          a=*j;
                  cout << a << " ";
          myfile <<a<<",";

 }
 myfile <<"\n";
 cout << "\n";
 }
 myfile.close();
 system("pause");

}
#包括
#包括
#包括
#包括
使用名称空间std;
void readCSV(istream&input,vector&output)
{
弦线;
//从小溪里读每一行
while(getline(输入,csvLine))
{
istringstream csvStream(csvLine);
向量CSV柱;
串级;
//从以逗号分隔的行中读取每个元素
//并将其放入向量或字符串中
while(getline(csvStream,csvElement,,'))
{
CSV柱。推回(CSVELENT);
}
输出。推回(CSV列);
}
}
int main()
{
流文件;
字符串a;
fstream文件(“b.csv”,ios::in);
myfile.open(“ab.csv”);
如果(!file.is_open())
{
cout-csvVector;
csvVector csvData;
readCSV(文件,csvData);
//打印出读取的数据,以证明读取工作正常
对于(csvVector::迭代器i=csvData.begin();i!=csvData.end();++i)
{
对于(向量::迭代器j=i->begin();j!=i->end();++j)
{
a=*j;

这能做什么?std::stringstream linestream(line);它创建一个字符串流对象。它的行为就像一个流,像std::cin、std::cout或文件流std::fstream,但使用字符串作为数据源。因此实际上它是一个只有一行的流。当我尝试编译时,我得到这个错误,变量“std::stringstream linestream”有初始值设定项,但类型不完整。我已经包含了所有的字符串he头是必需的。定义了std::stringstream
#include <sstream>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

void readCSV(istream &input, vector< vector<string> > &output)
{
   string csvLine;
    // read every line from the stream
    while( getline(input, csvLine) )
    {
            istringstream csvStream(csvLine);
           vector<string> csvColumn;
            string csvElement;
            // read every element from the line that is seperated by commas
            // and put it into the vector or strings
            while( getline(csvStream, csvElement, ',') )
            {
                    csvColumn.push_back(csvElement);
            }
            output.push_back(csvColumn);
    }
}

int main()
{
    ofstream myfile;
    string a;
    fstream file("b.csv", ios::in);
    myfile.open ("ab.csv");
    if(!file.is_open())
    {
           cout << "File not found!\n";
            return 1;
    }
    // typedef to save typing for the following object
    typedef vector< vector<string> > csvVector;
    csvVector csvData;

    readCSV(file, csvData);
    // print out read data to prove reading worked
    for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
    {
            for(vector<string>::iterator j = i->begin(); j != i->end(); ++j)
            {
          a=*j;
                  cout << a << " ";
          myfile <<a<<",";

 }
 myfile <<"\n";
 cout << "\n";
 }
 myfile.close();
 system("pause");

}