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

标签 c++ file notepad

所以我制作了一个程序来处理建筑物中的居住记录。除了一个功能之外,大部分代码与我的问题无关,即将该建筑物中居住者的所有记录写入.txt格式的打印输出文件。

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

  1. Open up File Explorer
  2. search up the file in the search box

现在,我想做的是在屏幕上打开文件,以便用户不必在文件资源管理器上查找它。我尝试在互联网上查找此内容,但我的每次搜索都与打开文件的 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
}

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

最佳答案

由于您使用的是 Windows,因此您可以使用 ShellExecuteW函数来做到这一点:

#include <Windows.h>
#pragma comment(lib, "shell32.lib)
...
ShellExecuteW(NULL, L"open", L"C:\\path\\to\\my\\file.txt", NULL, L"C:\\path\\to\\my\\", SW_SHOW);

关于c++ - 如何在屏幕上打开文件 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42034279/

相关文章:

c++ - 发出 textChanged() 信号时获取 QTextEdit 更改

c++ - 可以使用 C++11 基于范围的 for 在 [0, n-1] 区间上迭代吗?

c++ - 在 int 变量中查找 '1' 的奇偶校验

python - 在 Windows 7 中使用默认程序打开 PDF

python - 使用 python 更改视频文件属性

windows - 如何让 Vim 正确显示文件而不是显示 ^@ 字符

c++结合使用成员函数和重载运算符来过滤传递给流的数据

c++ - 哪个查找规则阻止编译器查找函数?

file - 从 TortoiseHG 数据源中删除文件

java - 将链接列表保存在Notepad.txt中