C++ 从文本文件中收集数据
我正在尝试从文本文件中读取数据。如何从C++中实现这一点。我必须使用输入模式吗C++ 从文本文件中收集数据,c++,C++,我正在尝试从文本文件中读取数据。如何从C++中实现这一点。我必须使用输入模式吗 open(fileName,ios::in) 如果是这样,那么如何以字符串形式读入数据 谢谢这是最简单的方法: #include <fstream> #include <string> #include <vector> int main () { std::string str; std::vector<std::string> vec;
open(fileName,ios::in)
如果是这样,那么如何以字符串形式读入数据
谢谢这是最简单的方法:
#include <fstream>
#include <string>
#include <vector>
int main () {
std::string str;
std::vector<std::string> vec;
std::ifstream myfile("example.txt"); //name of your txt file
while (std::getline(myfile, str)) { // read txt file line by line into str
vec.push_back(str); //you have to store the data of str
}
myfile.close(); // optional, streams are RAII types
// and thus close automatically on leaving scope
}
#包括
#包括
#包括
int main(){
std::字符串str;
std::vec;
std::ifstream myfile(“example.txt”);//txt文件的名称
而(std::getline(myfile,str)){//逐行将txt文件读入str
vec.push_back(str);//必须存储str的数据
}
myfile.close();//可选,流是RAII类型
//从而在离开范围时自动关闭
}
您可能应该使用,因为这是学校作业,我想,您的教授是否提供了任何形式的支持?我希望应该有。办公时间?为该课程的学生提供一个虚拟的会议场所?我担心学习这一点的最好方法是买本书或看在线教程,但很多教程材料都是“盲人引导盲人”类型的,新手很难分辨哪个是哪个:(这可能是一个可行的起点: