c++ sprintf 函数和 fstream 创建/检查文本文件

发布于 2024-11-18 14:10:05 字数 1568 浏览 3 评论 0原文

我在使用 sprintf 和 fstream 函数时遇到一些问题,以便为 POS 程序创建新的文本文件/检查文件是否已存在。我不知道我是否做错了什么,因为同一组函数在我的代码中的其他地方工作得很好...

这个特定的代码部分正在接受用户的输入来创建一个详细信息文件,名称是组成的输入到系统中的名字和姓氏详细信息。由于某种原因,未创建新文件。当我单步执行该程序时,我可以看到 custDetC 变量正在填充正确的数据。我还包括了文件存在检查,因为它可能与当前的问题有关,也可能无关......

托尼·米克尔

    sprintf(custDetC,"%s%s.txt", firstName.c_str(), lastName.c_str());
    cout << custDetC << endl;

    FileEX = FileExists(custDetC);

    if (FileEX == true)
    {
        fopen_s(&custDetF,custDetC, "rt");

        fprintf(custDetF, "%s %s\n", firstName, lastName);
        fprintf(custDetF, "$d\n", phoneNo);
        fprintf(custDetF, "%s $s\n", unitHouseNum, street);
        fprintf(custDetF, "%s %s %d", suburb, state, postCode);

        fclose(custDetF);
    }
    else
    {
        char *buf = new char[100];
        GetCurrentPath(buf);
        cout << "file " << custDetC << " does not exist in " << buf << endl;
    }
}

bool FileExists(char* strFilename) 
{
    bool flag = false;
    std::fstream fin;
    // _MAX_PATH is the maximum length allowed for a path
    char CurrentPath[_MAX_PATH];
    // use the function to get the path
    GetCurrentPath(CurrentPath);
    fin.open(strFilename, ios::in);

    if( fin.is_open() )
    {
        //cout << "file exists in " << CurrentPath << endl;
        flag = true;
    }
    else
    {
        //cout << "file does not exist in " << CurrentPath << endl;
        flag = false;
    }
    fin.close();

    return flag;
}

I am having some trouble with sprintf and fstream functions in order to create new text files for a POS program/check whether the file already exists. I don't know if i am doing something wrong because the same set of functions works fine in other places in my code...

This particular section of code is taking input from the user to create a details file, the name is made up of the first and last name details that were entered into the system. For some reason the new file is not being created. When I step through the program I can see that the custDetC variable is being filled with the correct data. I have also included the file existence check as it may or may not have something to do with the issue at hand...

Tony Mickel

    sprintf(custDetC,"%s%s.txt", firstName.c_str(), lastName.c_str());
    cout << custDetC << endl;

    FileEX = FileExists(custDetC);

    if (FileEX == true)
    {
        fopen_s(&custDetF,custDetC, "rt");

        fprintf(custDetF, "%s %s\n", firstName, lastName);
        fprintf(custDetF, "$d\n", phoneNo);
        fprintf(custDetF, "%s $s\n", unitHouseNum, street);
        fprintf(custDetF, "%s %s %d", suburb, state, postCode);

        fclose(custDetF);
    }
    else
    {
        char *buf = new char[100];
        GetCurrentPath(buf);
        cout << "file " << custDetC << " does not exist in " << buf << endl;
    }
}

bool FileExists(char* strFilename) 
{
    bool flag = false;
    std::fstream fin;
    // _MAX_PATH is the maximum length allowed for a path
    char CurrentPath[_MAX_PATH];
    // use the function to get the path
    GetCurrentPath(CurrentPath);
    fin.open(strFilename, ios::in);

    if( fin.is_open() )
    {
        //cout << "file exists in " << CurrentPath << endl;
        flag = true;
    }
    else
    {
        //cout << "file does not exist in " << CurrentPath << endl;
        flag = false;
    }
    fin.close();

    return flag;
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2024-11-25 14:10:05

您似乎打开文件进行读取,但需要打开它进行写入。

fopen_s() 中使用 "wt" 而不是 "rt"

You seem to be opening the file for reading, but you need to open it for writing.

Instead of "rt" use "wt" in fopen_s()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文