i just made a little program that reads the name of a file dragged over it's icon and writes it into an output text file
but if i compile this 开发者_开发知识库program, it crashes when i try to drag a file over it. if i open it with a doble click it's ok; if i open it with command line and parameters it's ok; but if i just drop a file over the program i have compiled, it always crashes and i don't know why
just try to compile like this:
#include <stdio.h>
int main(int argc, char * argv[])
{
FILE * File=fopen("file.txt", "w");
fclose(File);
return 0;
}
if you drag&drop a simple file over that program icon, the program crashes
does anyone knows why?
You are probably making assumptions about the current working directory and its permissions when your executable runs. Calling fclose on an invalid FILE * (e.g. NULL) will most likely result in a crash. You need to verify that fopen succeeds, e.g.
#include <stdio.h>
int main(int argc, char * argv[])
{
FILE * f = fopen("file.txt", "w");
if (f != NULL)
{
//
// write stuff to file here if you want...
//
fclose(f);
}
return 0;
}
精彩评论