Archived

This topic is now archived and is closed to further replies.

opening file

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i want my program to read from itself....it will be easy if the user was not able to rename the file but if the user renames the file how can i make it read its own file name and location...i''m vb programmer and would have written something of this nature maybe this will help... Open App.Path & App.EXEName & ".exe" For Binary As #1 how would i get the same result in c++?? i know how to open and read files using the fstream header...thanx for the help in advance... -face ============================= you laugh at us because we''''re different, we laugh at you because your all the same... =============================

Share this post


Link to post
Share on other sites
1) Why?

2) You can just try opening the file (with the ios::nocreate flag of course); odds are the user won''t rename it. If they do, you can get the path of the executable using certain Win32 functions and search for all EXEs in the directory. If there''s only 1, that''s yours; otherwise you''re out of luck, though there may be better methods out there.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
well the reason i''m doing is for no reason other then a learning experience....i''m binding multiple files together and trying to seperate them individually....anyone have any other ideas?

-face

=============================
you laugh at us because we''''re
different, we laugh at you
because your all the same...
=============================

Share this post


Link to post
Share on other sites
Look at the output of one on these programs (written in C):

#include <stdio.h>

int main(int argc, char *argv[]) {
printf("%s\n",argv[0]);

return 0;
}

Or this if you don''t have access to the argc/argv parameters the standard main function provides (if you are using WinMain, for instance):

#include <windows.h>
#include <stdio.h>

int main(void) {
printf("%s\n",GetCommandLine());

return 0;
}


Share this post


Link to post
Share on other sites
hey man just what i was looking for that for both versions....much appreciated....

-face

=============================
you laugh at us because we''''re
different, we laugh at you
because your all the same...
=============================

Share this post


Link to post
Share on other sites
As a note: Be careful with those examples, they don''t always give you the exact same results (they first is sometimes prefixed with the path, and the second will contain all kinds of stuff depending on how the program is launched).

Share this post


Link to post
Share on other sites
If yer using Windows, you can do this:



  
#include <windows.h>

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
char filename[MAX_PATH]; // 260 or so

GetModuleFileName(GetModuleHandle(NULL),filename,MAX_PATH);
MessageBox(NULL,filename,"This File",MB_OK);
return 0;
}

Share this post


Link to post
Share on other sites
hey man even better....thanx you guys...you make my life a whole lot simpler...

-face

=============================
you laugh at us because we''''re
different, we laugh at you
because your all the same...
=============================

Share this post


Link to post
Share on other sites