Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello..
This is my simple c++ code.


C#
#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.
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.
Posted

What happens if you put a full path in for the call to fopen? Something like:

FILE *file_ptr = fopen( "c:\\test.txt", "a+" );


making sure the path is somewhere that can be written to by just about anything. If that works then you know the problem is the current working directory of whatever it is that invokes the program.

Cheers,

Ash

PS: It seems a bit weird to be using ostreams to write to the console and then fall back on C style I/O for the file. Especially considering that you don't care if your writes work or whether the file closed properly.
 
Share this answer
 
I suppose it is executed with the wrong folder as application's current directory.
:)
 
Share this answer
 
Thanks for the answers friends.
Im not a programming expert.. just learning by myself.
So forgive me if i made any mistakes in coding style.

After reading CPallini's answer I did a search for myfile.txt in my c: drive. and found the myfile.txt file in system32 folder.
Im confused coz my application placed in "c:\myApplication\HelloWorld.exe" but it writes the file in system32 folder.

So i thought of getting the current directory in coding. so i added some codes to detect the current application directory.

modified code.

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;unistd.h&gt;
using namespace std;

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


After the system reboot. Application started and it showed "system32" folder as current directory. but my application not placed in system32 folder. and also i checked in system32 folder could not find my application.

here is the screenshot of the console window.
http://i780.photobucket.com/albums/yy87/kirubalks/Application.jpg[^]

how to solve this problem.

@Aescleal.. Thanks friend. I tried your suggestion its working.

but how to solve this problem?
my application placed in somewhere & it is executing from system32 folder.

hope you guys can help me.
Thanks.
 
Share this answer
 
It's not a bug. The current directory and the application's location (the directory where it resides) are two separate things.

If you want to get the path of the directory containing your exe, use the Windows API GetModuleFileName.
 
Share this answer
 
v2
Comments
Ajay Vijayvargiya 28-Oct-10 11:35am    
GetModuleFileName, to get current directory? I think you mean GetCurrentDirectory.
Nish Nishant 28-Oct-10 11:37am    
That was a typo, I've edited the post. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900