小编mpi*_*tek的帖子

Fstream不打开文件,ofstream确实

嗨我正在使用两个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)

c++ fstream ofstream

-1
推荐指数
1
解决办法
357
查看次数

标签 统计

c++ ×1

fstream ×1

ofstream ×1