0

I'm writing some code, and what it's supposed to do is return the path to a file without the filename itself. Here it is

#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

int main() {
    char binaryPath[MAX_PATH]; //I think my problems stem from the fact that 
                               //isnt truly a string

    GetModuleFileName(NULL, binaryPath, MAX_PATH); //Get filename
    string i = (string) binaryPath;
    string fileDir = "";
    int tempSlashes = 0;
    for (int x = 0; x < strlen(binaryPath); x++) {
        if (i[x] == '\\') { //In eclipse CDT this gives warning 1 (below)
            tempSlashes++; 
        }
    }
    for (int y = 0; y < tempSlashes; y++) {
        fileDir.append(binaryPath[y]); //Line gives warning 2
    }
    cout << fileDir;
    return 0;
}

Anyway, the little warning annotations

Error 1:
Multiple markers at this line
- comparison with string literal results in unspecified behaviour [-
 Waddress]
- ISO C++ forbids comparison between pointer and integer [-
 fpermissive]

Error 2:
Multiple markers at this line
- Invalid arguments ' Candidates are: std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(const 
 std::basic_string<char,std::char_traits<char>,std::allocator<char>> &) std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(const 
 std::basic_string<char,std::char_traits<char>,std::allocator<char>> &, unsigned int, unsigned int) std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(const 
 char *, unsigned int) std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(const char *) std::basic_string<char,std::char_traits<char>,std::allocator<char>> & 
 append(unsigned int, char) std::basic_string<char,std::char_traits<char>,std::allocator<char>> & append(#10000, #10000) '
- invalid conversion from 'char' to 'const char*' [-fpermissive]

The program itself refuses to compile, and runs an earlier .exe

I'm not sure what I'm doing wrong here, and help (as well as an explanation) would be very nice.

Thanks!

2
  • My original answer was wrong. But here is a fixed version of your code (although there are better ways to do this).
    – Jesse Good
    Apr 19, 2013 at 22:52
  • I just wanted to code it myself. Oh also, another question. I'm trying to pass the a string named s to an os command like this: system("cd "+s) but Eclipse is complaining that It - cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)' - And That i'm also giving it: Invalid arguments EDIT: Solved by calling s.c_str(); Apr 19, 2013 at 23:19

2 Answers 2

2

I suggest iterating backward and looking for '\'. Something like:

int len = strlen(binaryPath);
char outPath[MAX_PATH];
strcpy(outPath, binaryPath);
for (int x = len - 1; x >= 0; --x) {
    outPath[x] = 0;
    if (binaryPath[x] == '\\') {
        break;
    }
}
1

First, in modern Win32 C++ code you may want to use Unicode instead of ANSI/MBCS (so, wchar_t instead of char, and std::wstring instead of std::string).

For your particular question, you may want to use an existing Win32 API: PathRemoveFileSpec().

Here is a sample compilable code:

#define UNICODE
#define _UNICODE
#include <iostream>
#include <string>
#include <Windows.h>
#include <Shlwapi.h>
using namespace std;

#pragma comment(lib, "Shlwapi.lib")

int main()
{
    wchar_t binaryPath[MAX_PATH];
    GetModuleFileName(nullptr, binaryPath, MAX_PATH);
    PathRemoveFileSpec(binaryPath);

    // You can assign to a std::wstring as well...
    wstring fileDir = binaryPath;

    wcout << fileDir << endl;
}

Here is a sample test:

C:\Temp\CppTests>cl /EHsc /W4 /nologo /MTd test.cpp
test.cpp

C:\Temp\CppTests>test.exe
C:\Temp\CppTests

As an alternative, once you have the full path with file name read in the raw C buffer:

wchar_t binaryPathBuffer[MAX_PATH];
GetModuleFileName(nullptr, binaryPathBuffer, MAX_PATH);

you can copy it in a std::wstring, and then use its rfind() method to find the position of the last '\', and then extract the proper substring using substr method of std::wstring:

wstring binaryPath = binaryPathBuffer;
size_t last = binaryPath.rfind(L'\\');
wstring fileDir = binaryPath.substr(0, last);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.