-
Odd results.. help?
I just started messing around with code again recently and was attempting to figure out how to drag and drop a file into an executable and then edit the file and place the result into another file.. this is what i came up with, and I don't get what is breaking the code.. if i type in the file name through scanf, the file gets loaded and it can be read from and the output file works fine, yet the same code if i drag and drop, the file still gets loaded fine, yet the same code for the second file doesnt create anything.. and Im pretty sure i cant debug it with VS due to the dragging and dropping taking place outside the program.. anyway.. any suggestions or help would be appreciated.
Code:
#include <stdio.h>
void InitString(char* a)
{
int b = 0;
while(b != 1000)
{
a[b++] = '\0';
}
}
//argv & argc == code i found online for dragging and dropping/other commands
int main(int argc, char *argv[])
{
FILE *fip, *fop;
char z = '\0';
char g[1000];
int a = 0;
//executes fine if drag and drop takes place
if (argv[1] != 0)
fip = fopen( argv[1], "r" );
//executes fine if no drag and drop
else{
InitString(g);
printf("Enter File Name: ");
scanf("%s", g);
fip = fopen( g, "r" );}
//executes if no drag and drop, but NOT if drag and drop.......
fop = fopen( "test.txt", "a+" );
InitString(g);
//proving that it is actually opening the first file, works same with both
z = fgetc (fip);
while (a < 3 && z != '\0'){
z = fgetc (fip);
g[a++] = z;}
printf("%s", g);
fclose(fop);
if(fip != 0)
fclose(fip);
while(1);
return 1;
}
-
Blame your OS. This really has nothing to do with your program. Your OS handles dragging and dropping of files, and whatever redirection that may or may not cause.
Quzah.
-
Code:
//executes if no drag and drop, but NOT if drag and drop.......
fop = fopen( "test.txt", "a+" );
What makes you think this code isn't being executed when you drag and drop?
-
heh
yes yes.. God bless microsoft... i kinda wondered if it was the crappy visual express beta im using... thats what I will assume I guesses.. it cant be in my code from what I can see anyway.. I was just wondering if there was some sort of rules for using argv, like 1 file open at a time or something... oh well.. anyone have any other suggestions for dragging and dropping a file into an executable? thanks
-
no file is created, yet one is created when info is typed in manually.
-
hmm printf testing shows that
fop = fopen( "test.txt", "a+" );
seems to get an address and execute, just no file shows up.
-
You aren't checking the return value from fopen() though.
-
hmm
hmmm, anyone know if its something I don't know about argv? or is it my compiler? the following code creates a file when opened normally, yet doesnt create a file when opened from an object being dragged into it. and fopen returns the same value in both situations.
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fip;
fip = fopen( "test5.txt", "a+" );
}
-
hmmmm.... not the compiler... not argv i dont think..
argv works fine with multiple entries in the command line, creates the text file as expected.
and ive now tried multiple compilers and all have the exact same result, so it has to be strictly windows based (something limiting the program when executed from a drag and drop..?)... anyone know of alternate ways of drag and drop program execution? or some sort of windows override?
-
oh ya.. same result with C++ fstreams, works fine w/ command line arguments and normal click execution, but dragging and dropping doesnt work at all with creating the actual file (its loaded into memory fine but nothing on the disk) even though it is practically the exact same thing as
\programname.exe filename.txt etc
sending the same command line arguments to the program through argv... sigh
-
You can't just drag things anywhere you feel like and have it know what you're expecting it to do. It doesn't work that way. This is not a programming issue on your end at all, as I said. It's an OS issue.
Quzah.
-
MCyborg: Where in your code do you attempt to write to fop?
-
MCyborg,
First of all you're posting in the wrong forum. Drag and Drop is a function of Windows GUI. I'm assuming your OS is Windows since you refer to VS.
You have to create a Windows GUI application, then call DragAcceptFiles and finally process the WM_DROPFILES message.
Post your drag and drop questions in the Windows forum and I will provide the details of this Shellapi32 function.
Have fun ;)
Bob
-
Bob, that information is correct assuming that he wants to drag files onto a window after the program has been started. In this case, I think he just wants to drag a file onto his executable icon, in which case the path to the file is included in the executable's argument list.
MCyborg >> Your code is fine. The reason you are getting unexpected results is probably due to the default creation directory of the file. When you run the file from visual studio, by default it will create the file test.txt in your project directory. When you run the file by dragging another file onto the executable, the working directory is your "Documents and Settings\<logged in user>" folder. I bet if you check there, you will find the file test.txt.
-
Oops, sorry about my misinterpretation.