开发者

Error when running C program in task scheduler

开发者 https://www.devze.com 2023-03-18 00:15 出处:网络
I made a C program to get an image offline and save it in a file using fopen/fread/fwrite and libcurl. Everything compiles perfectly fine and I can run it perfectly fine by double clicking the program

I made a C program to get an image offline and save it in a file using fopen/fread/fwrite and libcurl. Everything compiles perfectly fine and I can run it perfectly fine by double clicking the program. But, when I try and set a task on my computer using task scheduler to run it every 10 minutes, the program opens and when it tries to save the file I get the error:

exception::handle: Exception: STATUS_ACCESS_VIOLATION open_stackdumpfile: Dumping stack trace to garden.exe.stackdump

I am runni开发者_StackOverflow社区ng Windows Vista, and attempting to use the native Windows Scheduled Tasks feature. I have checked the box marked "Run with highest privileges" in the task properties.


My guess is that whatever is running your program is doing so from a directory to which you don't have write permissions. The fact that there is no "garden.exe.stackdump" file gives that as a clue.

When you save it to a file, are you saving it as fopen("myfile","w") or are you using a fully qualified name? For example, let's say that the file you want to save is called "foobar.png" and you want to save it to the directory you named below, you'd have something like:

    char fname[256] = "foobar.png";
    char directory[256] = "C:/Users/Joel/Desktop/garden/snaps";
    char path[256];

    memset(path, 0, sizeof(path));
    strcpy(path, directory);
    strcat(path, "/");
    strcat(path, fname);

    if ((fp = fopen(path, "w")) == NULL) {
        fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
        exit(1);
    }

    fwrite(yourdata, yourdata_size, 1, fp);

Since your program also seems to dump errors to a file as well, you might do well to chdir("/home/myname") at the start of your program so that any ".stackdump" files get placed where you have access.

The other thing you might want to take into account is that your task scheduler may be running your script as nobody or some other permissions-deprived account. If that's the case, you'll want to use a full path in fopen and chdir to a globally writable area (such as /tmp) or a working directory with open permissions. For example:

mkdir /home/myname/scratch
chmod a+rwx /home/myname/scratch
chmod a+x   /home/myname

(You set the execute bit on your home directory so that the permission-less program can get access to its subdirectory even though it can't read anything in it.)

0

精彩评论

暂无评论...
验证码 换一张
取 消