Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
How do i open a file withe the fopen command and if a file exists with that same name then re-name that file to something else?

Below is the code that i am starting with:

C++
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e)
{
	/* Open Log file */
	/*===============*/
	pLogFile = fopen("LogFile.txt","w");
	fprintf_s(pLogFile,"<< LogFile Started.... \r\n\n","w");
	//fclose(pLogFile);
}
Posted
Comments
Sergey Alexandrovich Kryukov 25-Apr-12 14:18pm    
If this is .NET and C++/CLI (not really C++), tag it.
--SA

Not the best idea. For logging, use System.Diagnostics.EventLog, http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

As to the files, it looks like you wanted to append some text to the same file (which will be created when it is opened for the first time). For this purpose, use the class System.IO.StreamWriter with the constructor StreamWriter(string path, bool append):
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^],
http://msdn.microsoft.com/en-us/library/36b035cb.aspx[^].

What else? Ah, RTFM RMSDN, I mean, read MSDN!

—SA
 
Share this answer
 
v2
If you really want to use fopen() to create a log file (which I'm sure you don't - whatever you're writing it's not C) then rather than mess about with renaming files just append the time the file was created to a base name. Then they sort nicely in a directory.

[Warning, this is going to get ugly... I'm not the best C programmer anymore, if I ever was any good]
C++
#include <time.h>
#include <stdio.h>

#include <time.h>
#include <stdio.h>

int main()
{
	char file_name[ 128 ] = { 0, };

	int file_name_size = sprintf( file_name, "log_file_%u.txt", time( 0 ) );
	if( file_name_size < 128 && file_name_size > -1  )
	{
		FILE *fp = fopen( file_name, "w" );
		if( fp )
		{
			fputs( "Are we open?", fp );
			fclose( fp );
			fp = NULL;
		}
	}

	return 0;
}


Anyway, the point of this example is to show you that sticking with whatever your current runtime is far more productive than hacking at a lump of code to do the same thing.

Cheers,

Ash
 
Share this answer
 
v2
I could suggest you name the log file with the current date/time.

The risk of collision will be very close to zero; and if you create multiple logs, it might be easier to manager.
 
Share this answer
 
fopen does not provide an automatic rename service, so you must program it yourself.

With function _access you can check the existence of a particular file.
With function rename you can rename an existing file.

So do something like:

if (_access ("LogFile.txt", 0) == 0)
    rename ("LogFile.txt", "LogFile.old");


Probably your code must be a little more sophisticated, otherwise you are going to run into problems when doing this multiple times. Perhaps you implement a version number scheme for the renamed files and name them LogFile.v1, LogFile.v2 etc.

Hope that gets you on the right track.
 
Share this answer
 

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