如何在屏幕上打开文件 C++

Bus*_*mer 2 c++ notepad file

所以我制作了一个程序来处理建筑物中的住户记录。大部分代码与我的问题无关,除了一个功能,就是将这栋楼里住户的所有记录写到一个.txt格式的打印输出文件中。

如果我想在我的 Windows 笔记本电脑上访问这个文件,我必须:

  1. 打开文件资源管理器
  2. 在搜索框中搜索文件

现在,我想要做的是在屏幕上打开文件,这样用户就不必在文件资源管理器上查找它。我试着在互联网上查找这个,但我的每一次搜索都令人困惑fstream对打开文件的方法。

同样,为了避免任何混淆,我的问题是:是否有任何库、函数或代码段可以让我在计算机屏幕上显示 .txt 文件,以便用户可以避免在文件资源管理器(比如在记事本上打开这个文件)?这个问题无处不在,我不确定这项任务的难度有多大。

如果有帮助,这是我写入文件的代码:

    ofstream print_file;
    print_file.open("PrintOut.txt"); // All records written to this file
    for (int i = 0; i < 57; i++) // Exactly 57 records
    {
        // Here I'm just writing each field of the occupant record in a formatted method to the file.
        print_file << "\nRecord for occupant in appartment number " << Occupant::occupant_apartments[i] << ":\n";
        print_file << "\tOccupant Name:       " << this->occupant_name[i] << endl;

        print_file << "\tLeasing Rights:     ";
        if (this->occupant_leased[i] == "Yes")
            print_file << " Authorized" << endl;
        else if (this->occupant_leased[i] == "No")
            print_file << " Unauthorized" << endl;
        else
            print_file << " Empty Appartment" << endl;

        print_file << "\tParking Issued:     " << this->occupant_parking[i] << endl;
    }
    // Here I'd like to add the code (or make my own code) that will be able to open up Notepad to display the file PrintOut.txt
}
Run Code Online (Sandbox Code Playgroud)

任何意见或建议将不胜感激。如果您需要有关我的问题的更多信息或澄清,请在评论中通知我。

Gov*_*mar 5

由于您使用的是 Windows,您可以使用该ShellExecute函数来执行此操作:

ShellExecuteW(NULL, L"open", L"C:\\path\\to\\my\\file.txt", L"", L"C:\\path\\to\\my\\", SW_SHOW);
Run Code Online (Sandbox Code Playgroud)

在您的代码中包含<Windows.h>访问权限ShellExecute