tags:

views:

56

answers:

3

Hello! i am a beginner and want to file handling concept in win32 application for writing anything say text in file which is present in some location on hard disk.

Please Reply

Thanks in advance...

+1  A: 

You can do in this way Suppose there is a folder called "XYZ" at "E:" drive & the file name is "abc.log" and the function which u call is say "WSL".

VOID __cdecl WSL(char *message);//function declaration

//Function definition:

VOID __cdecl WSL(char *message) 
{ 
   static int lineNo = 1;  
   FILE *fp = fopen("E:\\XYZ\\abc.log","a+");  
   if(fp!= NULL)  
   {   
        fprintf(fp,"%d : %s\n",lineNo++,message);   
        fclose(fp);
   } 
}

and at main function u r coding the following:

int main()
{
  FILE *fp = fopen("E:\\XYZ\\abc.log","w");
  WSL("Application opened");
  ........
  ........
  ........
  ........
  ........
  WSL("Application closed");
}

This will write two time at abc.log file since it is called twice in your main function.

First time it will print...

1. Application opened

& second time it will print

2. Application closed

Abhi
Thanks a lot.It worked nicely at my desk.
There are other various method to obtain this. Choice is yours.
Abhi
Nit: the mode for `fopen` need not be "a+": you never read from the file inside the WSL function. And you should either open the file inside the function (and close it before the function returns) or once in `main`, pass the handle to the function, and close the file in main.
pmg
+1  A: 

Use fstream to write to a file, rather than win32 API. This is for RAII reasons, and it is also more standard.

Pavel Radzivilovsky
Ok i will try in that way too. But the above example worked nicely at my desk.Thanks
+2  A: 

There are several ways to do that. 1. Use functions fopen(), fwrite(), fclose() 2. Use std::fstream

alemjerus