c++open-with

c++ "Open with"


I am trying to write a simple text editor compatible with the "Open with" menu you get when right clicking on a text file (meaning I want my program to be able to read the contents of a text file I right-clicked and opened with my program). A friend told me I could get the path to the text file with the arguments of "int main(int argc, char* argv[])".Is this correct? If not, how can I do it?


Solution

  • To start you off, try this foundation:

    #include <string>
    #include <iostream>
    using std::string;
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main(int argument_count,
             char * argument_list[])
    {
      std::string filename;
      if (argument_count < 2)
      {
        filename = "You didn't supply a filename\n";
      }
      else
      {
        filename = argument_list[1];
      }
      cout << "You want to open " << filename << "\n";
    
      cout << "\nPaused, press Enter to continue.\n";
      cin.ignore(10000, '\n");
      return EXIT_SUCCESS;
    }
    

    This program will display it's first parameter. So if you associate a file extension with this program, it should display the file name that you right clicked on (provided it has the correct extension).

    I'll let you build upon this for whatever application you are creating.

    Note: the argument_list[1] refers to the text of the first parameter passed to the program. The name of the program is at argument_list[0].