1

My goal is to send an arbitrary text file to a exe which is a build of a c++ project. In the c++ project I would like to read the file which was sent to the exe. Hence, I think I need to pass the path of the sent file to the application (exe).

My c++ code [is working!]:

#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  std::string readLineFromInput;

  ifstream readFile("input.txt"); // This is explizit. 
                                  // What I need is a dependency of passed path.
  getline(readFile, readLineFromInput);

  ofstream newFile;
  newFile.open("output.txt");
  newFile << readLineFromInput << "\n";

  newFile.close();
  readFile.close();
}

My Windows configuration:

In the following path I created a shortcut to the exe (build of c++ project): C:\Users{User}\AppData\Roaming\Microsoft\Windows\SendTo

Question:

I want to right click to an arbitrary text file and pass it (SendTo) to the exe. How can I pass the path of the sent file as an argument to the application such that the application can read the sent file?

When the path is passed as an argument the line of code should be like this, I guess:

ifstream readFile(argv[1]); 

Much thanks!

David

david-
  • 25
  • 4
  • SendTo already does exactly this. Did you test it and have any problems? – smead Mar 25 '16 at 01:25
  • I have not tested it because argv[1] seemed too weird to me. As "Remy Lebeau" explained I have to do some string handling to get an interpreable path to the source file. So, `ifstream readFile(argv[1]); ` does exactly what I want. Next is the understand its content and do some string handling. Thank you. – david- Mar 25 '16 at 09:35

1 Answers1

1

Whether you use SendTo or OpenWith, the clicked filename will be passed to your executable as a command-line parameter. As such, the argv[] array will contain the filename (at argv[1], unless your SendTo shortcut specifies additional command-line parameters, in which case you would have to adjust the argv[] index accordingly).

I just did a test with SendTo and argv[1] works fine. Just make sure you check argc before attempting to open the filename, eg:

int _tmain(int argc, _TCHAR* argv[])
{
  if (argc > 1)
  {
    std::string readLineFromInput;

    std::ifstream readFile(argv[1]);
    if (readFile)
      std::getline(readFile, readLineFromInput);

    std::ofstream newFile(_T("output.txt"));
    if (newFile)
        newFile << readLineFromInput << "\n";
  }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Yes, you are right. It works! How can I write output.txt to the same directory where input.txt was read? Im my case the output directory is where the exe is located. I tried to print argv[1] but it seems to be an identifier and not a full path (c:\foo\input.txt) to the source file. – david- Mar 25 '16 at 01:33
  • 1
    Check again. The `argv[]` value will contain the *full* path and filename of the input file. You will have to write code to parse the value to split the path from the filename, then you can append the desired output filename to the extracted path. This is basic string handling 101 kind of stuff, you should be able to search around and find plenty of tutorials/examples to help you. If you still can't figure it out, post a new question. This question has been answered as asked. – Remy Lebeau Mar 25 '16 at 01:54
  • 1
    Or just use `boost::filesystem::path::parent_path`. – MSalters Mar 25 '16 at 09:22
  • Ok, will try it. Thanks a lot! – david- Mar 25 '16 at 09:30