在不使用外部库的情况下从文件中标记行的好方法?
我正在尝试标记以逗号分隔的数据库转储。我只需要读取第一个单词,它会告诉我这是否是我需要的行,然后标记该行并将每个分隔的字符串保存在向量中。
我很难保持所有数据类型的顺序。我使用 getline 方法:
string line;
vector<string> tokens;
// Iterate through each line of the file
while( getline( file, line ) )
{
// Here is where i want to tokenize. strtok however uses a character array and not a string.
}
问题是,如果第一个单词是我想要的,我只想继续阅读并标记一行。以下是文件中一行的示例:
example,1,200,200,220,10,550,550,550,0,100,0,-84,255
因此,如果我在字符串示例之后,它会继续标记该行的其余部分以供我使用,然后停止从文件中读取。
我应该使用 strtok、stringstream 还是其他东西?
谢谢你!
I am trying to tokenize a database dump separated by commas. I only need to read the first word, which will tell me if this is the line I need and then tokenize the line and save each separated string in a vector.
I have had trouble keeping all of the datatypes in order. I use a method of getline:
string line;
vector<string> tokens;
// Iterate through each line of the file
while( getline( file, line ) )
{
// Here is where i want to tokenize. strtok however uses a character array and not a string.
}
The thing is, I only want to continue reading and tokenize a line if the first word is what I am after. Here is a sample of a line from the file:
example,1,200,200,220,10,550,550,550,0,100,0,-84,255
So, if I am after the string example, it goes ahead and tokenizes the rest of the line for my use and then stops reading from the file.
Should I be using strtok, stringstream or something else?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何在 C++ 中标记字符串?
http://www.daniweb.com/software-development/cpp/threads/27905
希望这会有所帮助,尽管我不是熟练的 C/C++ 程序员。作为记录,如果您可以在标签或您正在使用的帖子语言中指定,那就太好了。
How do I tokenize a string in C++?
http://www.daniweb.com/software-development/cpp/threads/27905
Hope this helps, though I am not proficient C/C++ programmer. For the record it would be nice if you could specify in the tags or in post language you are using.
Tokenizer.h
Tokenizer.cpp
对字符串进行标记
Tokenizer.h
Tokenizer.cpp
To tokenize a string