Path

I want to make a launcher for my program but the installation folder is different from one computer to another so I need a function or something that shows the path of the executable file. If you can please tell me how to import a file from another folder.

If I can do this don't answer the second question:
1
2
3
4
5
6
7
#include<stdio.h>

int main(void)
{
    FILE *fp;
    *fp=fopen("C:\Documents and Settings\...\something.txt","r");
}


You see I want to set the import path.
Thank you for help.
From http://www.cplusplus.com/forum/beginner/1962/#msg7180

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string ExtractDirectory( const std::string& path )
  {
  return path.substr( 0, path.find_last_of( '\\' ) +1 );
  }

std::string ExtractFilename( const std::string& path )
  {
  return path.substr( path.find_last_of( '\\' ) +1 );
  }

std::string ChangeExtension( const std::string& path, const std::string& ext )
  {
  std::string filename = ExtractFilename( path );
  return ExtractDirectory( path ) +filename.substr( 0, filename.find_last_of( '.' ) ) +ext;
  }


From http://www.cplusplus.com/forum/general/28684/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  std::string find_executable( const std::string& argv0 )
    {
    WCHAR widename[ MAX_PATH ];
    char  ansiname[ MAX_PATH + MAX_PATH ];

    if (GetModuleFileNameW( NULL, widename, MAX_PATH ) == 0)
      {
      GetModuleFileNameA( NULL, ansiname, sizeof( ansiname ) );
      MultiByteToWideChar( CP_ACP, 0, ansiname, -1, widename, MAX_PATH );
      }

    WideCharToMultiByte( CP_UTF8, 0, widename, -1, ansiname, sizeof( ansiname ), NULL, NULL );

    std::string name = ansiname;
    std::replace( name.begin(), name.end(), '\\', '/' );

    return name;
    }

Use it thus:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(int argc, char** argv)
{
    FILE *fp;
    std::string filename;

    filename=ExtractDirectory(find_executable(argv[0]))+"something.txt";
    fp=fopen(filename.c_str(),"r");
    if (!fp)
    {
        fooey!
    }

    ...

    fclose(fp);
}

Hope this helps.
Topic archived. No new replies allowed.