[英]Create a vector of strings from file c++
I am trying to input three pieces of information from a.txt file.我正在尝试从 a.txt 文件中输入三条信息。
First column is the course mark.第一列是课程标记。 Second column is the course code.第二列是课程代码。 Third column(s) is the course name.第三列是课程名称。
I would like to store these as 3 vectors of strings.我想将这些存储为 3 个字符串向量。
Would using stringstream be a good option here?在这里使用 stringstream 会是一个不错的选择吗? and maybe iterators?也许是迭代器?
The.txt file is like .txt 文件就像
65.6 10071 Mathematics 1
66.7 10101 Dynamics
60.0 10121 Quantum Physics and Relativity
66.9 10191 Introduction to Astrophysics and Cosmology
... ... ...
and my code so far is到目前为止我的代码是
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<string>
#include<iterator>
#include<sstream>
#include<vector>
//Main Function
int main()
{
//Define variables
std::string course_mark, course_code, course_name;
std::vector<std::string> course_mark_vector;
std::vector<std::string> course_code_vector;
std::vector<std::string> course_name_vector;
std::string data_file[100];
// Ask user to enter filename
std::cout<<"Enter data filename: ";
std::cin>>data_file;
int i{0};
// Open file and check if successful
std::fstream course_stream(data_file);
if(course_stream.is_open()) {
while (!course_stream.eof()) //while the end of file is NOT reached
{
//I have 2
getline(course_stream, course_mark, ' ');
course_mark_vector.push_back(course_mark);
getline(course_stream, course_code, ' ');
course_code_vector.push_back(course_code);
getline(course_stream, course_name, '\n');
course_name_vector.push_back(course_name);
i += 1; //increment number of lines
}
course_stream.close(); //closing the file
std::cout << "Number of entries: " << i-1 << std::endl;
}
else{
std::cout << "Unable to open file. Please run again" << std::endl;
return 1;
}
Any help would be greatly appreciated任何帮助将不胜感激
Would using stringstream be a good option here?在这里使用 stringstream 会是一个不错的选择吗?
Yes.是的。
and maybe iterators?也许是迭代器?
There is no need for iterators in this case.在这种情况下不需要迭代器。
Try this:尝试这个:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iomanip>
//Main Function
int main()
{
//Define variables
std::string course_mark, course_code, course_name, data_file, line;
std::vector<std::string> course_mark_vector, course_code_vector, course_name_vector;
int i = 0;
// Ask user to enter filename
std::cout << "Enter data filename: ";
std::cin >> data_file;
// Open file and check if successful
std::ifstream course_stream(data_file);
if (!course_stream.is_open())
{
std::cout << "Unable to open file. Please run again" << std::endl;
return 1;
}
while (std::getline(course_stream, line)) //while the end of file is NOT reached
{
std::istringstream iss(line);
iss >> course_mark;
course_mark_vector.push_back(course_mark);
iss >> course_code;
course_code_vector.push_back(course_code);
std::getline(iss >> std::ws, course_name);
course_name_vector.push_back(course_name);
++i; //increment number of lines
}
course_stream.close(); //closing the file
std::cout << "Number of entries: " << i << std::endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.