0

I have a problem with showing file directory. I used _splitpath function to cut file direction and file name. Then, I used strcat to concatenate the drive name and directory. I also used this function for file name and it was Ok. I noticed that I can't concatenate directly to drive because the buffer size of driver is only 3. So, I concatenate drive to another character array and then concatenate with directory. Here comes the problem. When I put a breakpoint and debug, there's an exception after the _splitpath line. I don't know how to fix it so, please help me with my problem.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[]) 
{
    char szPath[256] = {NULL};          
    char cDirCheck = '\\';                  
    char szDrive[_MAX_DRIVE] = {0};
    char szDir[_MAX_DIR] = {0};
    char szFileName[_MAX_FNAME] = {0};
    char szExt[_MAX_EXT] = {0};
    char pszPath[256] = {NULL};
    char* pszFname = NULL;

     if(cDirCheck != NULL) {
        _ splitpath(argv[0], szDrive, szDir, szFileName, szExt);
        strcat(pszPath, szDrive);
        strcat(pszPath, szDir);
        pszFname = strcat(szFileName, szExt);
        printf("\nUsage: %s fileName\n", pszFname);
        printf("EXE Path:%s", pszPath\n);
     } else {                       
        printf("\nUsage: %s fileName\n", argv[0]);
        printf("EXE Path : Current Folder\n");
    }
   return 0;
}

You may think file path and file name output correctly but the last printf of the else statement only show "EXE Path : ". I also don't know why it happened. Please give me some ideas. With regards...

12
  • BTW your program only compiles after some typos have been corrected. The test cDirCheck != NULL is always true so it is useless. But apart from that your program works fine here. You are writing about an exception after _splitpath. Tell us more about that. Commented Apr 11, 2014 at 6:42
  • When I used breakpoint and debugger as you said, "mov ecx,[esp+4]; ecx -> dest strin CXX0013: Error: missing operator" this error appeared. Please can you explain what is it about. Commented Apr 11, 2014 at 8:34
  • Thanks for correcting my mistakes. English is not my native language and I'm not very good at English. Commented Apr 11, 2014 at 8:38
  • Which operating system and which compiler/visual studio do you use ? Commented Apr 11, 2014 at 8:46
  • Windows 7 Enterprise and Visual Studio 2010 Express. Commented Apr 11, 2014 at 8:47

1 Answer 1

2

Try this. This is a stripped down version of your program that shows what is actually happening:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[]) 
{
    char szDrive[_MAX_DRIVE] = {0};
    char szDir[_MAX_DIR] = {0};
    char szFileName[_MAX_FNAME] = {0};
    char szExt[_MAX_EXT] = {0};

    _splitpath(argv[0], szDrive, szDir, szFileName, szExt);

    printf ("argv[0]    = %s\n\n", argv[0]) ;
    printf ("szDrive    = %s\n", szDrive) ;
    printf ("szDir      = %s\n", szDir) ;
    printf ("szFileName = %s\n", szFileName) ;
    printf ("szExt      = %s\n\n", szExt) ;

    return 0;
}

argv[0] is actually the name of the command you typed in the cmd window to invoke your program. So if you type dircut, argv[0] is dircut, if you type dircut.exe, argv[0] is dircut.exe and if you type c:\xxx\dircut, argv[0] is c:\xxx\dircut and so on.

Tests:

S:\>s:\dircut\debug\dircut.exe
argv[0]    = s:\dircut\debug\dircut.exe

szDrive    = s:
szDir      = \dircut\debug\
szFileName = dircut
szExt      = .exe


S:\>s:\dircut\debug\dircut
argv[0]    = s:\dircut\debug\dircut

szDrive    = s:
szDir      = \dircut\debug\
szFileName = dircut
szExt      =


S:\>cd dircut\debug

S:\dircut\Debug>dircut.exe
argv[0]    = dircut.exe

szDrive    =
szDir      =
szFileName = dircut
szExt      = .exe
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, Micheal. Sorry about spending your times. I appreciate your answers.
That's what Stackoverflow is made for. Please accept the answer if it is what you wanted.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.