Odd results.. help?

This is a discussion on Odd results.. help? within the C Programming forums, part of the General Programming Boards category; I just started messing around with code again recently and was attempting to figure out how to drag and drop ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    8

    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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    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?

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    8

    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

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    no file is created, yet one is created when info is typed in manually.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    hmm printf testing shows that
    fop = fopen( "test.txt", "a+" );
    seems to get an address and execute, just no file shows up.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    You aren't checking the return value from fopen() though.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    8

    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+" );
    }

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    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?

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    8
    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

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    MCyborg: Where in your code do you attempt to write to fop?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    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

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    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.

  15. #15
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Oops, sorry about my misinterpretation.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. _chdrive odd results
    By cyreon in forum Windows Programming
    Replies: 2
    Last Post: 06-23-2005, 06:35 PM
  2. Odd results for such a simple procedure.
    By tyouk in forum Windows Programming
    Replies: 2
    Last Post: 01-14-2005, 08:16 PM
  3. C Subroutine produces odd results
    By IGAU in forum C Programming
    Replies: 8
    Last Post: 11-24-2003, 09:47 AM
  4. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 03:39 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21