C++ Application not working on system boot

Hello..
This is my simple c++ code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    cout<<"Writing Hello World.\n";
    
    FILE *file; 
    file=fopen("myfile.txt","a+"); 
    
    if(file!=NULL)
    {    
        fputs("Hello World!\n",file);
        fclose(file);
        cout<<"Succes!\n";
    }  
        
    system("PAUSE");
    return EXIT_SUCCESS;
}


This is working fine. Application creates myfile.txt(if it is not present). and writes "Hello World!" to the file.

I want to automatically launch this application when PC starts. So I manually made the registry setting to run this application on system boot.
 
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run


Application runs when pc starts. It shows the console window.
1
2
3
Writing Hello World.
Succes!
Press any key to continue . . .


but the problem is "myfile.txt" is not created. even if the file exist in the directory it does not write "Hello World!" to the file.

I dont know why it doesnt create/write file.
If i manually start the application it works.

Hope you guys will help me to solve this problem.
Thanks.


It probably is working but the working directory of the program when windows boots may not be where your program is. You can either search your HD for myfile.txt or specify the path in your cpp file with something like:
file=fopen("c:\\myfile.txt","a+");
so that where ever it is executed from it will output the file to the same place.
Last edited on
Thanks for the reply.
You are absolutely right.

I searched in my c: drive. found the file on system32 folder.
But how the working directory can change. my application placed in "c:\myapplication\HelloWorld.exe". how it can write it in system32 folder.

Just to get the current directory from the application i add some code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include <cstdlib>
#include <iostream>
#include <unistd.h>
using namespace std;

int main(int argc, char *argv[])
{
    
    char *path=NULL;
    size_t size;
    path=getcwd(path,size);
    cout<<"Current Path = "<<path<<"\n";
    
    
    
    cout<<"Writing Hello World.\n";
    
    FILE *file; 
    file=fopen("myfilekiruba.txt","a+"); 
    
    if(file!=NULL)
    {    
        fputs("Hello World!\n",file);
        fclose(file);
        cout<<"Succes!\n";
    }  
      
    system("PAUSE");
    return EXIT_SUCCESS;
}


after the restart, application launched.
here is the screenshot of the console window
http://i780.photobucket.com/albums/yy87/kirubalks/Application.jpg

Do you know why it happens like this.
My application placed in somewhere and its executing from system32 folder.
How to solve this problem.

do i need to add some codings? or this is windows fault?


hope you can help me.
Thanks.


I don't know for sure why the working directory is set to system32, but as most of the files Windows loads at boot time will be in that directory, is makes sence to set it there.

Whatever Windows uses to invoke your program either sets the working dir to system32, or at least dosn't change it from system32 to the path of your program. (you can see how working directorys are used by looking at the properties of a shortcut icon)

You can force your program to store it's files in a place you specify by including the path in your code as I showed above, you could also use the argc and argv perameters passed to main() as a method to alter where you want the output files to go, and then change your registry key to pass in a directory.

Does that help?
Topic archived. No new replies allowed.