C++ 使用分隔符逐行读取文件并将数据存储到变量中
C++ Reading a file line by line with delimiter and store data into variable
我对C++很陌生,我有一个带有数据的txt文件,看起来像这样:
测试:123:锁定
QWERTY:4321:解锁
ASDF:12:锁定
我是否可以使用 ":"
作为分隔符将数据逐行读取到变量/数组中?
我尝试做这样的事情:
while(!myfile.eof()) {
for (int i = 0; i < 3; i++) {
getline(myfile,UserN[i],':');
}
}
我想要实现的是将第一行的数据存储到UserN[0]
、UserN[1]
和UserN[2]
中。当它开始读取第二行时,第二行的数据将替换 UserN[0]
、 UserN[1]
和 UserN[2]
中的值。提前感谢!
首先阅读该行,然后用std::stringstream
标记它:
#include <sstream>
...
std::string line;
while(std::getline(myfile, line)) { // cache the line
std::istringstream tokenizer(line);
std::getline(tokenizer, UserN[0], ':'); // then get the tokens from it
std::getline(tokenizer, UserN[1], ':');
std::getline(tokenizer, UserN[2]); // last token: get the remainder
// of the line.
if(tokenizer) {
// success!
} else {
// There were fewer than two colons in the line
}
}
从本质上讲,std::istringstream
将字符串包装在流接口中 - 生成的流的行为(大致)类似于具有与构建它的字符串相同的内容的文件。然后可以使用>>
或getline
或任何其他可以在文件或std::cin
或其他输入流上使用的东西,在这里我们使用它来将字符串分解到您需要的令牌中。
你可以简单地做到这一点
ifstream myfile( "aFile.txt" );
// .. check whether the file is open: if( !myfile.is_oppen() ) error
for( string userN[3]
; getline( getline( getline( myfile >> ws, userN[0], ':' ), userN[1], ':' ), userN[2] ); )
{
// userN[0..2] is read correctly
}
或者以更优雅的方式,也许更适合您的要求。我假设第二个文本总是一个数字,第三个文本是"锁定"或"解锁"或其他类似枚举的东西。
enum class LockState
{
lock, unlock
};
// -- reading a LockState
// please consider, that behind the text must follow a white space character (Space, LF, ..)
std::istream& operator>>(std::istream& in, LockState& s)
{
std::string word;
if( in >> word )
{
if( word == "lock" )
s = LockState::lock;
else if( word == "unlock" )
s = LockState::unlock;
else
in.setstate( std::ios_base::failbit );
}
return in;
}
struct Entry // change the name 'Entry' of the struct suitable for Your requirements
{
std::string someText;
int aNr;
LockState lockState;
};
// -- function to read an 'Entry'-object
std::istream& operator>>(std::istream& in, Entry& e)
{
char colon;
if( getline( in >> std::ws, e.someText, ':' ) >> e.aNr >> colon
&& colon != ':' )
in.setstate( std::ios_base::failbit );
else
in >> e.lockState;
return in;
}
以及稍后在您的主程序中
ifstream myfile( "aFile.txt" );
// .. check whether the file is open: if( !myfile.is_oppen() ) error
for( Entry e; myfile >> e; )
{
// use here the Entry-object 'e'
}
if( myfile.eof() )
cout << "Ok - You read the file till the end" << endl;
避免这里的麻烦,并使用 Boost 中的拆分函数:
#include <fstream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
// ...
// Read file and throw exception on error.
std::ifstream infile;
infile.open(file_name);
std::string line;
while (std::getline(infile, line))
{
// Strip of the comments.
std::vector<std::string> strings;
boost::split(strings, line, boost::is_any_of(":"));
// You have now a vector of strings, which you can process...
}
相关文章:
- 将从格式化文本文件读取的文本数据存储到链表
- 将线程中的数据存储到全局容器的最佳方法?
- 将数据存储在内存中以供以后访问
- 将一个阵列中的数据存储到另外两个阵列中不起作用
- 如何将函数的输出数据存储在文本文件中
- 将结构数据存储在循环缓冲区中
- 安卓中的Qt和应用程序数据存储
- C++ 将数据存储在对齐数组的中间
- 如何删除字符串中的所有空格并将数据存储在两者之间
- 实现双链接列表,其中数据存储在堆上
- 是否有一种方法可以从文本文件中读取并将单个数据存储为不同的变量
- 了解字符数组初始化和数据存储
- 如何在C++中将字符串数据存储在字符串 2D 数组中
- 将数据存储在具有可互换 x/y 坐标的 2D 矩阵中
- 将文件中的数据存储到数组中
- 将数据存储到地图
- 保存和加载类数据存储在C 中的二进制文件中
- 将由结构构建的数组传递到将数据存储到文件中的函数
- 在这种情况下,将数据存储在诸如SQLite之类的数据库中,还是存储在平面文本文件中更好
- 将数据存储在char数组中导致变量周围损坏