如何一次从一个文件读取输入,类似于在C ++中使用cin/scanf从控制台读取输入? [英] How to read an input from a file one at a time similar to reading input from console using cin/scanf in c++?
问题描述
我正在尝试从文件中逐个读取输入,而不是从标准输入中给出输入.
I am trying to read input from a file one by one instead of giving it from standard input.
我所拥有的,目前正在使用!
What I have, which is currently working!
for(int i = 0; i <CLASS_SIZE; i++)
{
for(int j = 0; j <10 ; j++)
{
scanf("%d", &grade);
studentsInClass[i].setGrade(j,grade);
}
}
来自控制台的当前输入是:
current input from console is this:
91 92 85 58 87 75 89 97 79 65
88 72 81 94 90 61 72 75 68 77
75 49 87 79 65 64 62 51 44 70
我希望直接从文件中读取此输入!因此,我尝试逐行将其读取为文件流,然后尝试将该行保存到字符串向量中,以便可以在内部循环中提取每行的单个数字.
I want this input to be read directly from a file! So, I tried reading it as a file stream, line by line and then tried to save that line into a vector of strings, so that I can extract the single number per line in an inner loop.
std::ifstream inputfile("input.txt");
std::string line;
std::vector<std::string> v;
for(int i = 0; i <CLASS_SIZE; i++)
{
for(int j = 0; j <10 ; j++)
{
if(inputfile.is_open()){
while( std::getline(inputfile, line) ){
std::cout << line << '\n';
std::istringstream iss(line);
iss>>v;
for(std::vector<std::string>::iterator it = v.begin(); it != v.end(); ++it) {
studentsInClass[i].setGrade(j, std::stoi(it, nullptr, 2));
}
}
inputfile.close();
}
}
}
但是出现以下错误!因为我不知道正确的方法!
but getting following error! Because I don't know the right way to do it!
error: cannot bind ‘std::basic_istream<char>’ lvalue to ‘std::basic_istream<char>&&’
iss>>v;
及以下错误,因为我正在尝试将字符串转换为整数,因为我的程序需要将字符串消耗为整数,所以我再次在语法上做错了事.
and below error, because I am trying to convert the string to an integer as my program needs to consume as integer and again I am doing something wrong with the syntax.
error: no matching function for call to ‘stoi(std::vector<std::__cxx11::basic_string<char> >::iterator&, std::nullptr_t
, int)’
studentsInClass[i].setGrade(j, std::stoi(it, nullptr, 2));
任何人都可以帮助解决此问题吗?
can anyone help to fix this issue?
推荐答案
首先,我将对使您满意的代码进行必要的最小更改:
As a first approximation, I'd make the most minimal changes necessary to the code that makes you happy:
std::ifstream infile("filename");
for(int i = 0; i <CLASS_SIZE; i++)
{
for(int j = 0; j <10 ; j++)
{
// scanf("%d", &grade);
infile >> grade;
studentsInClass[i].setGrade(j,grade);
}
}
鉴于您知道每个学生的确切成绩,使用 getline
几乎不会(如果有).如果输入是面向行的,那将很有用.例如,考虑这样的输入文件:
Given that you know the exact number of grades for each student, you gain little (if anything) from using getline
. That would be useful primarily if the input was line-oriented. For example, consider an input file like this:
91 92 85 58 87 75 89 97 79 65 88
72 81 94 90 61 72 75 68 77
75 49 87 79 65 64 62 51 44 70
按现状,您显然仍希望这是3位学生的成绩,每位10年级,所以第一行的最后一个成绩( 88
)属于第二个学生,而不是第一位.
As it stands, you still apparently want this read as 3 students with 10 grades apiece, so the last grade on the first line (the 88
) belongs to the second student, not the first.
使用 getline
,然后分别解析每行,如果您希望将此输入数据解释为第一位学生具有11年级,第二位学生具有9年级(第三位是10年级),则是有意义的.
Using getline
and then parsing each line individually would make sense if you wanted this input data interpreted as the first student having 11 grades, and the second student 9 grades (and the third 10 grades).
这涵盖了您提出的问题,但是我对代码仍然不太满意.就个人而言,我更喜欢编写一个学生类,该类知道如何从流中读取自己的数据:
That covers the question you asked, but I'm still not particularly happy about the code. Personally, I'd prefer to write a student class that knew how to read its own data from a stream:
class student {
std::vector<int> grades;
public:
// other stuff, of course.
friend std::istream &operator>>(std::istream &is, student &s) {
int grade;
for (int i=0; i<10; i++) {
is >> grade;
s.grades.push_back(grade);
}
return is;
}
};
然后,当我们想读取多个学生的数据时,我们将执行以下操作:
Then when we want to read data for multiple students, we'd do something like this:
std::vector<students> the_class;
std::ifstream infile("grades.txt");
for (int i=0; i<CLASS_SIZE; i++) {
student s;
// use `operator>>` above to read all 10 grades for one student.
infile >> s;
the_class.push_back(s);
}
这篇关于如何一次从一个文件读取输入,类似于在C ++中使用cin/scanf从控制台读取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!