[英]How to read data from a specific column in a text file
I'm having a text file with some columns 我有一个带有一些列的文本文件
name,age,address,gender
I know I can access a line with getline();
我知道我可以用
getline();
访问一行getline();
, but when I get the string with the data that getline();
,但是当我得到带有
getline();
数据的字符串时getline();
returned I want to read some specific column. 返回我想阅读一些特定的专栏。 How do I do that ?
我怎么做 ?
I saw a solution to this problem but it was in java which I don't know yet, so I decided to post this for a c++ answer 我看到了解决此问题的方法,但它尚不知道是在Java中,所以我决定将其发布为C ++答案
The approach is the same: read the file line by line and tokenize, while taking the second elem of the array if I want to see the second column. 方法是相同的:逐行读取文件并标记化,如果我想查看第二列,则获取数组的第二个元素。 It can be improved because it does lots of copies but the idea is here.
由于可以进行很多复制,因此可以进行改进,但是这里有个主意。
#include <sstream>
#include <string>
#include <boost/algorithm/string.hpp>
std::string line;
vector<string> strs;
std::ifstream infile("thefile.txt");
while (std::getline(infile, line))
{
strs.clear();
boost::split(strs,line,boost::is_any_of("\t"));
cout<<"age ="<<strs[1]<<endl;
}
Be it you are asking in Java or C++, the approach will be similar. 无论您是用Java还是C ++进行询问,方法都是相似的。
After you have read the entire line. 阅读完整行之后。 Split the line by its delimiter.
用定界符分隔行。
In Java, it is just 在Java中,
line.split(",");
In C++, you could use str.find(",")
to get the position of the comma. 在C ++中,您可以使用
str.find(",")
来获取逗号的位置。 Then substring the delimited tokens out. 然后将定界标记子字符串化。
Set a count
variable to count number of tokens you have delimited so far to get to your desired column. 设置一个
count
变量,以计算到目前为止已定界的令牌数量,以到达所需的列。
You may also parse the C string (if you use STL strings do copy the C string to a char array!) using strtok. 您也可以使用strtok解析C字符串(如果使用STL字符串,则将C字符串复制到char数组!)。 It will work pretty well, although the need for a separate copy is due to the fact that strtok does modify the string as well as some static memory (or, perhaps, thread-local storage; I wouldn't rely on it)
尽管需要单独的副本是因为strtok确实会修改字符串以及一些静态内存(或者也许是线程本地存储;我不会依赖它),但它会很好地工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.