如何不使用fopen()命令在Visual C ++中用相同的文件名覆盖现有文件 [英] How to not over write an existing file with the same filename in Visual C++ with the fopen() command

查看:85
本文介绍了如何不使用fopen()命令在Visual C ++中用相同的文件名覆盖现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用fopen命令打开文件,如果存在同名文件,然后将该文件重命名为其他名称?

以下是我开始使用的代码:

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:

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);
}

推荐答案

不是最好的主意.对于日志记录,请使用 System.Diagnostics.EventLog http://msdn.microsoft.com/zh-CN我们/library/system.diagnostics.eventlog.aspx [ ^ ].

对于文件,您似乎想要在同一文件上附加一些文本(该文件将在首次打开时创建).为此,将类System.IO.StreamWriter与构造函数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 [ ^ ].

还有什么?啊, RTFM RMSDN,我的意思是,请阅读MSDN!

-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


如果您确实要使用fopen()创建日志文件(我确定您不会这样做-无论您做什么)在写它不是C),然后将文件的创建时间附加到基本名称上,而不是弄乱文件的重命名.然后它们在一个目录中很好地排序.

[警告,这将变得很丑...如果我有任何好的经验,我将不再是最好的C程序员]
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]
#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;
}



无论如何,该示例的目的是向您展示,坚持使用当前的运行时要比用大量的代码来做同样的事情要有效得多.

干杯,

Ash



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


我建​​议您使用当前日期/时间来命名日志文件.

碰撞的危险将非常接近于零;并且如果您创建多个日志,则可能更易于管理.
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.


这篇关于如何不使用fopen()命令在Visual C ++中用相同的文件名覆盖现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆