2017-02-03 72 views
1

因此,我制作了一个程序来处理建筑物中的占有者记录。大多数代码与我的问题无关,只有一个功能,即将本建筑中所有人的所有记录写入.txt格式的打印输出文件。如何在屏幕上打开一个文件C++

如果我要访问我的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 
} 

任何输入或建议,将不胜感激。如果您需要更多信息或对我的问题作出澄清,请在评论中通知我。

+0

这个问题的回答者可以给出一个降低问题的原因吗? –

回答

4

由于您使用的是Windows,你可以使用ShellExecute功能来做到这一点:

ShellExecuteW(NULL, L"open", L"C:\\path\\to\\my\\file.txt", L"", L"C:\\path\\to\\my\\", SW_SHOW); 

包括<Windows.h>访问ShellExecute在你的代码。

+0

谢谢@GovindParmar!但是,你可以描述每个参数吗?我不明白其中一些:( –

+1

另外,它要求我输入6个参数 –

+0

@ABusyProgrammer固定,我的坏! –

相关问题