嗨我正在使用两个txt文件.在第一个,我用fstream打开文件,但是当我试图用fstream打开第二个文件时不起作用,但是如果我尝试用ofstream打开它的话.有什么想法发生了什么?以下是功能.谢谢
void stack::read()
{
string name = "file.txt", line;
fstream file;
file.open(name.c_str());//open the file
char cc;
if (!file)
{
cout << "Error could not open the file" << endl;
return;
}
else
{
//file was opened succesful
while (file)
{
file.get(cc);//get each character of the string
push(cc);//insert the character into the stack
}
}
file.close();//close the file
}
void stack::write()
{
item *r = stackPtr;//point to the top element of the stack
ofstream file;
string name = "f.txt";
file.open(name.c_str());
if …Run Code Online (Sandbox Code Playgroud)