C++ getline()函数在while循环中不起作用
我想用一个外部类“file\u Opening\u and\u Closing.cpp”制作一个简单的文件输入输出程序,当我使用没有任何循环的“getline()”函数时,它可以正常工作,但当我在“main.cpp”中使用带do while循环的“getline()”函数时,它会崩溃 请告诉我问题在哪里,我如何解决 main.cpp 文件\u Opening\u和\u Closing.cppC++ getline()函数在while循环中不起作用,c++,fstream,file-handling,C++,Fstream,File Handling,我想用一个外部类“file\u Opening\u and\u Closing.cpp”制作一个简单的文件输入输出程序,当我使用没有任何循环的“getline()”函数时,它可以正常工作,但当我在“main.cpp”中使用带do while循环的“getline()”函数时,它会崩溃 请告诉我问题在哪里,我如何解决 main.cpp 文件\u Opening\u和\u Closing.cpp #包括“文件_Opening_和_Closing.h” 文件打开和关闭::文件打开和关闭() { cou
#包括“文件_Opening_和_Closing.h”
文件打开和关闭::文件打开和关闭()
{
cout>文件名;
}
作废文件打开和关闭::放置数据(){
流文件的输出;
打开(文件名);
cin.ignore();
你可能有一大堆问题,其中最重要的一个问题是你把用来测试两者的变量混在一起了,do..while(…);
循环,例如
...
cin>>file_insert;
}while(file_make=='y');
// i++; (not needed and VLA's not allowed in C++
cout<<"Do you want to Make another file ? 'y' OR 'n'"<<endl;
cin>>file_make;
}while(file_insert=='y');
如果您需要一个以上的类实例,则可以使用类向量或类数组。但是,您的代码中不需要这两个实例。由于您通过(…)
do{…}while(…)循环创建了文件的新实例,因此您可以简单地使用:
File_Opening_and_Closing file;
你把file\u make
和file\u insert
声明为typechar
,这让你自己的事情变得异常艰难。相反,只需将它们std::string
并用if(file\u make==“y”)进行测试即可
。这将允许您使用getline
读取所有输入,避免混合std::cin
和getline
使用时出现问题
剩下的问题是,如果(!file_out.is_open()){/*handle error*/}
验证文件打开时完全失败,每个输入都需要进行类似的测试,以确保用户使用Ctrl+d(或windows上的Ctrl+z)生成手动EOF
时可以捕获输入取消
另外,避免将使用名称空间std;
放在头文件中。没有必要将标准名称空间拉入使用头文件的每个文件中(尽管你做得很好,并使用头保护文件\u打开\u和\u关闭\u H
)。事实上,这里根本没有理由使用命名空间std;。只需对cin、cout
等使用std::
命名空间解析运算符即可
将验证留给您添加,解决其他问题,您可以执行类似的操作:
文件打开和关闭.h
#ifndef FILE_OPENING_AND_CLOSING_H
#define FILE_OPENING_AND_CLOSING_H
#include<iostream>
#include<fstream>
class File_Opening_and_Closing
{
private:
std::string file_name;
std::string data;
public:
File_Opening_and_Closing();
void put_data();
void show_data();
protected:
};
#endif
结果输出文件
$ cat out1
barbaz
$ cat out2
bazbuz
如果你还有其他问题,请告诉我。
也许是与.<代码>主页.CPP < /代码> -“ISO C++禁止可变长度数组”<文件> <代码>无关的:看起来你可能有<代码>(FielsAudi= =‘y’)< /Calp>和<代码>(FielyEng=='y');< /Cuff>交换。@ USER481301,但循环不是“无关的”。"因为第一个是问我是否要在同一个文件中插入另一个字符串,第二个是检查我是否要创建另一个文件并在其中插入其他内容。:)谢谢@David C.Rankin先生,我将使用vector,但构造函数执行得很好,我已经看到我们可以在mingw co运行时分配数组长度谢谢你的链接。 ...
cin>>file_insert;
}while(file_make=='y');
// i++; (not needed and VLA's not allowed in C++
cout<<"Do you want to Make another file ? 'y' OR 'n'"<<endl;
cin>>file_make;
}while(file_insert=='y');
File_Opening_and_Closing file[i];
File_Opening_and_Closing file;
#ifndef FILE_OPENING_AND_CLOSING_H
#define FILE_OPENING_AND_CLOSING_H
#include<iostream>
#include<fstream>
class File_Opening_and_Closing
{
private:
std::string file_name;
std::string data;
public:
File_Opening_and_Closing();
void put_data();
void show_data();
protected:
};
#endif
#include "File_Opening_and_Closing.h"
File_Opening_and_Closing::File_Opening_and_Closing()
{
std::cout << "Enter the file name and type => ";
getline (std::cin, file_name);
}
void File_Opening_and_Closing::put_data(){
std::ofstream file_out;
file_out.open(file_name);
std::cout<<"Enter the string => ";
getline (std::cin, data);
file_out << data << '\n';
file_out.close();
}
void File_Opening_and_Closing::show_data(){
std::ifstream file_in;
file_in.open (file_name);
getline (file_in,data);
std::cout << data << '\n';
file_in.close();
}
#include<iostream>
#include<fstream>
#include "File_Opening_and_Closing.h"
int main (void) {
std::string file_make = "y";
std::string file_insert = "y";
do
{
File_Opening_and_Closing file;
do
{
file.put_data();
file.show_data();
std::cout << "Do you want to insert text again ? 'y' OR 'n'\n";
getline (std::cin, file_insert);
} while (file_insert == "y");
std::cout << "Do you want to Make another file ? 'y' OR 'n'\n";
getline (std::cin, file_make);
} while (file_make == "y");
return 0;
}
$ ./bin/main
Enter the file name and type => out1
Enter the string => foobar
foobar
Do you want to insert text again ? 'y' OR 'n'
y
Enter the string => barbaz
barbaz
Do you want to insert text again ? 'y' OR 'n'
n
Do you want to Make another file ? 'y' OR 'n'
y
Enter the file name and type => out2
Enter the string => bazbuz
bazbuz
Do you want to insert text again ? 'y' OR 'n'
n
Do you want to Make another file ? 'y' OR 'n'
n
$ cat out1
barbaz
$ cat out2
bazbuz