-11

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?

zomnombom
  • 986
  • 2
  • 9
  • 15
  • 1
    I suggest studying C++ and then asking here. This is way too basic and can be covered by any decent C++ book for beginners. – Marco A. May 25 '15 at 17:01
  • Can you explain more clearly? – Steve May 25 '15 at 17:01
  • 2
    Yes, if you configure the "open with" to use your program, then the program would get the file name as a command argument, accessible from `argv`. But you should specify the operating system (and if relevant, the desktop environment etc). – hyde May 25 '15 at 17:01
  • When you right click on a file to open it, that files info is sent to the program though the `args.` That has nothing to do with trying to create a text editor. You need to learn C++ and some sort of GUI. – Evan Carslake May 25 '15 at 17:02
  • I want to be able to right click on a file and select the "open with" menu, then select my prgram. And i want to be able to access the contents of the file I right-clicked in the previous example through the source code of my program. – zomnombom May 25 '15 at 17:04
  • Want to know if this is correct? Why not just try it? – n. m. could be an AI May 25 '15 at 17:13

1 Answers1

3

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].

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154