在 linux 上使用命令行重定向(wiith <)将文件内容存储在字符串中?
Storing contents of file in string using command line redirection (wiith <) on linux?
如果我以这种方式运行main.cpp:
./main.cpp < file.txt
当我把这个输入存储在字符串的向量中时,同一行的每个字都存储在新行中。如何有效而优雅地将每一整行存储在矢量字符串的一个元素中?例如我希望它是:
Myvector[0]= "this is just a sentence"
不喜欢:
Myvector[0] ="this"
Myvector[1]="is"
Myvector[3]="just" etc
使用std::getline
。
std::string text_line;
while (std::getline(std::cin, text_line))
{
my_vector.push_back(text_line);
}
std::getline
函数读取直到遇到换行符,将数据存储到字符串中。
之所以使用std::cin
,是因为您正在通过管道传输文件中的数据。
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 如何打开并写入一个名称取自C++中字符串的文件
- 如何从二进制文件中读取字符串
- 如何在 c++ 中使用 getline 从文件中读取字符串?
- 将字符串与文件上的数据进行比较
- 用户输入字符串的文件附加问题..C++
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 如何使字符串出现在编译的二进制可执行文件的开头?
- 使用文件内容作为硬编码字符串
- 将文件复制到自定义位置,存在字符串转换问题
- 在 c++ 中连接字符串和整数,以便在 C++ 11 不支持计算机的情况下读取多个文件
- 声明要在 qml 和 cpp 文件中使用的全局字符串
- C 通过char读取文件字符串的文件char;崩溃
- 我不明白如何将 toupper 和 isalpha 与.txt文件字符串输入一起使用
- 在 CPP 文件中使用 make 文件字符串变量
- 编译 PyGAMMA Mac Lion:g++ 找不到标准的 C++ 头文件(字符串/iostream..)
- 如何在c++中使用文件字符串作为命令
- 使用 TinyXML 解析 XML 文件字符串
- 递归下降解析器帮助(不读取文本文件字符串)