如何使用逗号分隔值对文本文件进行读写 [英] 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\n
6, 7, 8, 9, 10\n
\n
并且在读取文件后,我想将数据写回其他文件,如同上面的格式。
and after reading the file, I want to write the data back into other file as same format above.
,使用
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 ??
我也有ints的向量来存储数据。
所以,我想读取第一行,然后计算该行中的元素总数,这里为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 ??
谢谢
推荐答案
步骤1:
不要这样做:
Step 1: Don't do this:
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 << ")\n";
}
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)?"\n":",");
}
// 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));
}
这篇关于如何使用逗号分隔值对文本文件进行读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!