【问题标题】:Text file Issues (Visual C++)文本文件问题 (Visual C++)
【发布时间】:2019-04-20 02:47:27
【问题描述】:

我正在尝试让程序提示用户输入文件名,然后提示他们输入要删除的单词。 有没有办法做到这一点,而无需在 main 之外创建任何功能? (提前致谢!) 到目前为止,这是我的代码:

ofstream outFile;
string in;
char in2[800];
char fileName = ' ';
while (in != "XXXX") {
    cout << "What do you want to name the file? Enter a name or enter XXXX to quit.\n";
    cin >> in;

    for (int i = 0; i < in.length(); i++)
    {
        fileName += in[i];
    }
    outFile.open(in + ".txt");
    if (outFile.fail())
    {
        outFile << "Can't do it.";
    }
    else
    {
        cout << "What do you want in it?\n";
        cin >> in2;
        cin.getline(in2, sizeof(in2));
        outFile << in2;
    }
    outFile.close();
    ifstream reader;
    string oneLine;
    reader.open(in + ".txt");
    if (reader.fail())
    {
        cout << "Could not read file.\n";
        return 0;
    }
    else
    {
        reader.clear();
        reader.seekg(60L, ios::beg);
        getline(reader, oneLine);
        cout << "Is there something you'd like to remove from " << in << ".txt?\n";
        cin >> in2;
        ofstream write("tmp.txt");
        while (getline(reader, oneLine)) {
            if (oneLine != in2)
                write << oneLine << endl;
        }
        reader.close();
        write.close();
        remove(fileName + ".txt");
        rename("tmp.txt", fileName + ".txt");
        return 0;
    }
    return 0;
}

【问题讨论】:

  • 道歉。误解。 in2char 数组,而不是 in
  • 你有什么理由不想要函数吗?从职责分工和代码可读性的角度来看,它们几乎总是更受欢迎。
  • 逐行处理您的代码并了解每个语句的作用。 fileName += in[i] 不会做你可能做的事情。
  • @Hristijan Gjorshevski 他的输出文件也没有

标签: c++ visual-c++ fstream iostream


【解决方案1】:
while (in != "XXXX") {
    cout << "What do you want to name the file? Enter a name or enter XXXX to quit.\n";
    ...
}

如果您输入XXXX,则循环不会退出。它将尝试处理其余的代码。

char fileName = ' '; 声明一个字符。你想要一个字符数组。更好的是,将其声明为std::string fileName;

remove(fileName + ".txt"); 不会编译,因为remove 是一个 C 函数。 C 不支持类,也不知道如何处理诸如std::string 之类的类。你想提供一个 C 字符串,你可以用 fileName.c_str() 做到这一点以下是一些建议:

int main()
{
    ofstream outFile;
    string fileName;
    string text;
    for(;;)
    {
        cout << "What do you want to name the file?\n";
        cout << "Enter a name or enter 999 to quit.\n";
        cin >> fileName;

        if(fileName == "999")
            break;

        fileName += ".txt";
        outFile.open(fileName);
        if(outFile.fail())
        {
            cout << "Can't do it.";
            continue;
        }
        else
        {
            for(;;)
            {
                cout << "What do you want in it?\n";
                cout << "Enter a name or enter 999 to stop.\n";
                cin >> text;
                if(text == "999")
                    break;
                outFile << text << endl;
            }
        }
        outFile.close();

        ifstream reader;
        reader.open(fileName);
        if(reader.fail())
        {
            cout << "Could not read file.\n";
        }
        else
        {
            cout << "Is there something you'd like to remove from " << fileName << "?\n";
            cin >> text;

            string oneLine;
            ofstream write("tmp.txt");
            while(getline(reader, oneLine)) 
            {
                if(oneLine != text)
                    write << oneLine << endl;
            }
            reader.close();
            write.close();

            remove(fileName.c_str());
            rename("tmp.txt", fileName.c_str());
            break;
        }
    }
}

【讨论】:

  • 谢谢巴马克!这正是我想要的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多