-1

After researching the whole week every day (since previous Monday, 2016-05-30), having tested various "solutions" and having built program hundreds of times, I finally managed to write this code which does not throw any errors or warnings and should find .txt file path in main.cpp folder, successfully replace backslashes with forward slashes and open the text file ready to edit.

Before writing part with LPCSTR, an error was throwing out when I have been trying to use string variable path in ShellExecute() instead of manually written path:

cannot convert 'std::string {aka std::basic_string<char>}' to 'LPCSTR {aka const char*}' for argument '3' to 'HISTANCE__* ShellExecuteA(HWND, LPCSTR, LPCSTR... [documentation]

Now it finds path and replaces slashes perfectly, but does not open a .txt file.

Also, I have tested function CreateProcess() but I have managed to open (with manually written path in code) only .exe files, not .txt. And when I tried to call function with a string variable in it (at first parameter, which should be LPCSTR), I got the same error, then did the same solution (for error) as shown here and CreateProcess() did not open .exe file (for example, notepad.exe).

I use GNU GCC compiler, computer OS is 32-bit, Windows.

EDIT: Problem resolved (thanks to Andy) by adding .txt to "Text" in GetFullPathName():

GetFullPathName("Text.txt", 256, szDir, &fileExt);

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <shellapi.h>
#include <stdio.h>
#include <cstring>

using namespace std;

string path; //global variable

void FindFilePath()
{
    char szDir[256], buffer[256];
    char* fileExt;
    GetFullPathName("Text", 256, szDir, &fileExt);
    snprintf(buffer, 256, "\"%s\"", szDir); //EDIT: quotes aren't necessary
    path = buffer;
    string find("\\"), insert("/");
    int found = path.find(find);
    while (found >= 0) {
        if (found >= 0)
            path.replace(found, find.size(), insert);
        found = path.find(find);
    }
    /*
    If path.find(find) didn't find required symbol, for example,
    when string ends, it returns -1 and consequently terminates with
    an error 'std::length_error'.
    EDIT: replacing isn't necessary.
    */
    LPCSTR path2;
    {
        path2 = path.c_str();
        ShellExecute(GetDesktopWindow(), "edit", path2, NULL, NULL, SW_SHOW);
        cout << "TEST path2:\n" << path2 << endl;
    }
}

int main()
{
    ofstream REtext("Text.txt");
    REtext.close();
    //Created (so that program could find path) or
    //restarted text file (so that user had an empty file) in main.cpp folder.
    FindFilePath();
    cout << "\nPath is: " << path;

    return 0;
}
  • 2
    Are you sure that GetFullPathName() is returning the path correctly? It won't automatically add a .txt extension on the file name you give it, so it's probably returning an error. Also, why are you replacing backslashes with forward slashes? Backslash is the path separator character in Windows. – Andy Jun 06 '16 at 18:16
  • 1
    Please indent your code systematically and repost. – Cheers and hth. - Alf Jun 06 '16 at 18:17
  • Why are you putting `szDir` into quotes? `snprintf(buffer, 256, "\"%s\"", szDir);` – rrauenza Jun 06 '16 at 18:21
  • @Andy Only what is needed to be done here is adding ".txt" to the "Text" in GetFullPathName(). How I did not notice that, I don't know. Thank you for your answer. Also, replacing backslashes isn't necessary - it was when path was written by hand (double backslashes work as well). rrauenza, One time putting path without quotes didn't work, so I have done this just in case. Now works without them. – Rocky M. K. Jun 06 '16 at 19:40
  • @RockyM.K. It's ok - sometimes all it takes is a second set of eyes to see the problem that is staring you in the face. :) – Andy Jun 06 '16 at 22:30

1 Answers1

1

The problem is that GetFullPathName() isn't returning the full path to the file because only the filename is supplied, not the extension. Once the extension is added, then it will return the absolute path to the file and ShellExecute() will be able to open it.

Andy
  • 30,088
  • 6
  • 78
  • 89