C++ code :: blocks ios :: in Works,ios :: Out | ios :: app否

发布于 2025-01-26 02:30:36 字数 1181 浏览 3 评论 0原文

我有一个非常简单的代码通过Code :: blocks:

#include <iostream>
#include <fstream>

using namespace std;

string firstName, lastName;
int phoneNumber;

int main()
{
    fstream file;

    file.open("card.txt", ios::in);

    if (file.good())
    {
        cout << "File is good (read)" << endl;
        file.close();
    }
    else
    {
        cout << "Cannot open file (read)." << endl;
    }

    file.open("card.txt", ios::out | ios::app);

    if (file.good())
    {
        cout << "Enter your First Name: \n";
        cin >> firstName;
        cout << "Enter your Last Name: \n";
        cin >> lastName;
        cout << "Enter your Phone Number: \n";
        cin >> phoneNumber;

        file << firstName << endl;
        file << lastName << endl;
        file << phoneNumber << endl;

        file.close();
    }
    else
    {
        cout << "Cannot open file (write)." << endl;
    }

    return 0;
}

card.txt文件位于项目主文件夹中。从第一个开始,如果我得到“文件很好(读)”。从第二个开始,我得到“无法打开文件(写)”。

真的不知道为什么。也许代码::阻止属性设置不正确。对我来说,他们似乎还可以,自安装以来就没有改变。

I have a very simple code written in C++ via Code::Blocks:

#include <iostream>
#include <fstream>

using namespace std;

string firstName, lastName;
int phoneNumber;

int main()
{
    fstream file;

    file.open("card.txt", ios::in);

    if (file.good())
    {
        cout << "File is good (read)" << endl;
        file.close();
    }
    else
    {
        cout << "Cannot open file (read)." << endl;
    }

    file.open("card.txt", ios::out | ios::app);

    if (file.good())
    {
        cout << "Enter your First Name: \n";
        cin >> firstName;
        cout << "Enter your Last Name: \n";
        cin >> lastName;
        cout << "Enter your Phone Number: \n";
        cin >> phoneNumber;

        file << firstName << endl;
        file << lastName << endl;
        file << phoneNumber << endl;

        file.close();
    }
    else
    {
        cout << "Cannot open file (write)." << endl;
    }

    return 0;
}

Card.txt file is located in the project main folder. From the first if value I get "File is good (read)". From the second one I get "Cannot open file (write)."

Really don't know why. Maybe Code::Blocks properties are set incorrect. For me they seem ok, they were not changed since installation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文