why the hell cant i load this file?

Started by
6 comments, last by fireking 20 years, 5 months ago
you know how all windows programs can accept command lines? and if you create an exe, then drag a file on top of the exe, windows will pass the location of that file as the command line to your app well when i do this, it works great, i can do a nifty MessageBox() to verify that its actually doing that, but when i try to open that file, it wont let me... ive tried both fopen(cmd,"r") and CreateFile(cmd,....); its always gives me an invalid file handle
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
Advertisement
If your creating a console app then you use the main() parameters, cant remember the exact names they are given but they are an int of the total number of command line arguments and an array of strings which contains any files/options you pass to your program on the command line.

In your example you might want to check the first index in the array of arguments for the name of the file you are passing on the command line - FILE *file = fopen(args[0],"r")

EDIT: with windows pograms - those which contain WinMain(), you might want to look into the paramter LPSTR lpCmdLine, the name kind of gives it away really

here's an example main() with the parameters i mentioned:
int main(int argc, char *argv[])




[edited by - Spudder on November 12, 2003 9:02:49 AM]
Why the hell cant people ask a question without saying hell?
you might want to open argv[1] instead of argv[0]

in a windows console exe the first paramter is the name of the exe
quote:Original post by Anonymous Poster
Why the hell cant people ask a question without saying hell?

Satan''s stock is rising evidently.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Fireking, you might want to tell us if you''re doing a Console app or a Windows app. Just to help us help you. (I''m betting on Windows, but just to be sure tell us anyway, okay?)

And I do suspect something, I remember experimenting with this and finding that the ''active directory'' varies between starting a program by double-click, and starting it with a drag-and-dropped file on it. I invite you to check what directory is active, look exactly at what info you got in your main() params, etc etc.

If it''s like me, you''ll find out that current directories vary a bit too much for ease of use. Hope this helps.
=^.^= Leaders and teachers should remember: It is best to offer others what they Need, not what they Want.
Call GetLastError() to find out why CreateFile() is failing. However, if you''re trying to open a file that has been dragged and dropped on your app then try using OpenFile() since it already exists.
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
I think he means a Win32 app with WinMain. That "LPSTR lpCmdLine" parameter is the command line string. fireking, can you post the actual method that does the file opening?
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.

This topic is closed to new replies.

Advertisement