C++ 如何解决字符串和infle.open之间的数据冲突 #包括 #包括 #包括 #包括 使用名称空间std; **int main() { 双写(); 双读(); 字符串选择; 而(1) { coutchoice; 如果(选项==“读取”) cout
您可以使用cin.ignore(INT_MAX);在cin.getline(文件名、大小)之前清除输入缓冲区;问题在于您没有从输入流读取“EOL序列”(AKA'\n'):C++ 如何解决字符串和infle.open之间的数据冲突 #包括 #包括 #包括 #包括 使用名称空间std; **int main() { 双写(); 双读(); 字符串选择; 而(1) { coutchoice; 如果(选项==“读取”) cout,c++,string,conflict,C++,String,Conflict,您可以使用cin.ignore(INT_MAX);在cin.getline(文件名、大小)之前清除输入缓冲区;问题在于您没有从输入流读取“EOL序列”(AKA'\n'): #include<iostream> #include<fstream> #include<cstdlib> #include<string> using namespace std; **int main() { double write(); double
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
**int main()
{
double write();
double read();
string choice;
while(1)
{
cout<<"Enter read to read a file and write to write a file.\n";
cin>>choice;
if (choice == "read")
cout<< read();
if (choice == "write")
cout<< write();
}
}
double read()
{
const int size = 60;
ifstream inFile;
char filename[size];
cout<<"Enter the name of the file you want to read \n";
cin.getline(filename, size);
inFile.open(filename);**
if(!inFile.is_open())
{
cout<<"could not open "<<filename<<endl<<"program terminating";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
inFile >> value;
while(inFile.good())
{
cout<<value<<"\n";
++count;
sum += value;
inFile >> value;
}
if (inFile.eof())
cout<<"End of file reached. \n";
else if (inFile.fail())
cout<<"Input Terminated by data mismatch.\n";
else
cout<<"input terminated for unknown reason";
if (count == 0)
cout<<"no data processed";
else
{
cout<<"Items read: "<<count<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Average: "<<sum / count << endl;
}
inFile.close();
return 0;
}
double write()
{
char type[81];
char filename[81];
cout<<"this program is more or less pointless, any text edtor on earth is better than this for writing"<<endl;
cout<<"files; However This is the first step in learning how to create my file tranfer program."<<endl;
cout<<"Enter the name of a file you want to create.\n";
cin>>filename;
ofstream outFile;
outFile.open(filename);
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
while(!cin.fail()){
cin.getline(type,81);
outFile<<type<<endl;
}
outFile.close();
}
它读取一个单词,但不从输入中读取'\n'字符。因此,以下案文如下:
cin>>choice;
只读取上次读取后留在流中的“\n”字符(而不是预期的下一行)
从用户处读取输入时。我总是喜欢先读取输入行,然后在内部进行处理
cin.getline(filename, size);
那么,获取文件名的代码应该可以工作:
std::string command;
std::getline(std::cin, command); // reads the whole line (but discards the '\n')
if (command == "read") { /* Stuff */ }
都做完了
当我们在这里时:
这很聪明,因为您可以避免流上循环的正常问题。但有一个更好的解决方案(我说的更好是更紧凑):
infle>>值;
while(infle.good())
{
cout值)
{
当你想读取字符串时,你只需要协调“cin>>”和“getline”的用法。它们的行为略有不同
请记住,当使用cin
从控制台读取时,用户触摸的每个按键都会进入cin
缓冲区,包括返回键的\n
因此,当用户键入“read”并点击return以输入他们的选择时,cin
此时的内容是:
读取\n
然后你会:
while(inFile >> value)
{
cout<<value<<"\n";
++count;
sum += value;
}
问题是>
运算符被定义为一直读取,直到它碰到空白,因此它将以“读取”方式读取,但它将\n仍保留在cin缓冲区中。
因此,在您的cin>>选择
语句之后,cin
的内容是:
\n
然后你去做这个:
string choice;
cin >> choice;
它将正常工作,因为>
操作符也会忽略前导空格
您还可以将choice
的初始读取更改为如下所示(使用此处字符串标题中的getline
):
这将起作用,因为getline
不会将散乱的\n字符留在cin
缓冲区中
您可以使用cin.ignore
功能来清除cin
缓冲区。如果您不知道自己在做什么,您不想盲目使用ignore
,但我们知道在这种情况下,您会在cin
上留下一个杂散的\n,因此如果您想清除它,您可以这样做:
string choice;
getline(cin,choice);
#包括
字符串选择;
cin>>选择;
cin.ignore(std::numeric_limits::max(),'\n');
该ignore
语句实际上会清除cin
中的剩余数据,方法是说“丢弃下一个INT\u MAX字符或直到并包括a\n的所有内容。”
希望这能为你们澄清一切。谢谢大家;我用布伦特·纳什的答案找到了答案,把这个include放在源文件的顶部
(#)包括
将此代码放在我的cin>>选择之后
cin.ignore(std::numeric_limits::max(),'\n')
不过,我会研究其他答案;未插入的代码会伤害眼睛。我甚至不会看它。没有cin>>文件名
。这也是问题所在,因为如果您使用了它,您就不会有问题(除非您的文件名包含空格).嘿,谢谢大家,我用了char,因为我的书上是这么说的,我不能用cin.getline来训练它。我现在就用std::string,谢谢。当人们不辞辛劳地帮助陌生人时,我真的很感激。
string choice;
cin >> choice;
char filename[size];
cout<<"Enter the name of the file you want to read \n";
cin.getline(filename, size);
cin >> filename;
string choice;
getline(cin,choice);
#include <limits>
string choice;
cin >> choice;
cin.ignore(std::numeric_limits<int>::max(),'\n');