C++ 无法使用std::getline()与ifstream和ofstream一起工作
如果我将代码拆分为两个独立的程序,它的工作方式与我希望的一样(将创建文件的第1部分和尝试以用户身份访问文件的第2部分分开);但是,一旦我将代码放入一个程序(见下文),我就无法使用getline()方法从标准输入中收集输入;程序只需一步一步地完成,就不会在源代码中的getline方法处停止收集用户输入 我读了一大堆已回答的问题,但都没有回答以下问题:C++ 无法使用std::getline()与ifstream和ofstream一起工作,c++,stream,getline,filestreams,C++,Stream,Getline,Filestreams,如果我将代码拆分为两个独立的程序,它的工作方式与我希望的一样(将创建文件的第1部分和尝试以用户身份访问文件的第2部分分开);但是,一旦我将代码放入一个程序(见下文),我就无法使用getline()方法从标准输入中收集输入;程序只需一步一步地完成,就不会在源代码中的getline方法处停止收集用户输入 我读了一大堆已回答的问题,但都没有回答以下问题: 键入#包括 键入outFile.clear() 键入infle.clear() 在过去的3个小时里,我尝试了其他的方法,比如注释掉部分代码,看看是否
#包括
outFile.clear()代码>
infle.clear()代码>
#include <iostream>
#include <fstream> //file steam for access to objects that work with files
#include <cstdlib>
//using namespace std;
int main()
{
//****************PART #1 - CREATE A FILE & ADD DATA *************************************************
std::cout << "Part #1 - create file & put data into it.\n" << std::endl;
//create an object called "outFile" of type ofstream (output file stream)
// arg #1 - path and file name
// arg #2 - constant that represents how we want to work with file (output stream in this case)
std::ofstream outFile("/home/creator/Desktop/creation.txt", std::ios::out);
//get user to enter 5 numbers:
int userGrade;
for(int i = 0; i < 2; i++)
{
std::cout << "Enter grade number " << (i+1) << ": ";
std::cin >> userGrade; //collect a grade from the user
outFile << userGrade << std::endl; //write data to file (each number on it's own line)
}
outFile.close();//close the stream
std::cout << "All is well and good - file is created and data is populated" << std::endl;
//****************PART #2 - READ & MUNIPILATE DATA FROM FILE*****************************************
std::cout << "\nNext, lets read the data from the file we created." << std::endl;
std::cout << "please enter the path to the file: (ex: /home/creator/Desktop/creation.txt)" << std::endl;
std::string fileName; //the path to the file we want to read.
std::getline(std::cin, fileName);//<<< THIS IS MY QUESTION/PROBLEM
std::ifstream inFile(fileName.c_str(), std::ios::in);
if(!inFile)
{
std::cout << "File not found!" << std::endl;
exit(1);
}
double grade = 0;//this holds the data we retrieve from file
double total = 0; //get the sum of all the grades as a total
double average = 0; //get the average of all the grades
int numberOfGrades = 0; //number of grade values in file
//retreive and munipilate the data from the file.
while(!inFile.eof())
{
inFile >> grade;
total = total + grade;
numberOfGrades++;
std::cout << grade << std::endl;
}
average = total / numberOfGrades;
std::cout << "The average of the grades in the file is: " << average << std::endl;
inFile.close();
return 0;
}
#包括
#包括//文件蒸汽以访问使用文件的对象
#包括
//使用名称空间std;
int main()
{
//****************第1部分-创建文件并添加数据*************************************************
std::cout问题在于,前一个输入循环在输入缓冲区中留下最后一个换行符,对std::getline
的下一次调用将其读取为空行
阅读成绩后,这一点很容易被这一行的其余部分解决
根据链接参考中的示例:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.ignore(std::numeric_limits::max(),'\n');
另外,不要使用while(!infle.eof()){…}
,它将不会像您期望的那样工作。相反,在您的情况下,您应该在(infle>>评分){…}
时使用,非常感谢。在收集第二次输入之前,我添加了std::cin.ignore();
,一切正常。再次感谢!