怎样才能建立一个以当前日期为文件名的txt文件?

CodeMaker_12138 2011-12-14 09:06:07
如题、
...全文
741 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pathuang68 2011-12-15
  • 打赏
  • 举报
回复
C语言取系统时间的说明及示例代码,可以参考:
http://zhidao.baidu.com/question/33201114.html
pathuang68 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lishanchao 的回复:]

C/C++ code

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

int main()
{
time_t t;
struct tm *p;
FILE *fp = NULL;
char fname[256] = {0};

t = time(NULL);
p = gm……
[/Quote]
++
这个应该符合楼主的要求。
Gloveing 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 holly_worm 的回复:]

引用 1 楼 wcg_jishuo 的回复:

C/C++ code

CFileException e;

// CDialog::OnOK();
char pFileName[30];
COleDateTime timeNow, dateNow;
timeNow = COleDateTime::GetCurrentTime(); // 获取当前日期时间
……
[/Quote]
MFC的
沭水河畔 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yingzijuntuan 的回复:]

想问一个问题的,就是说建立了一个文件,那个文件会在哪里呀?
[/Quote]
FILE *fopen(const char *fname, const char *mode);
fname如果写成“test.txt”,那么生成的test.txt和程序处于同样的目录。如果用VC++,那么的到工程的Debug目录找找。
fname如果写成“c:\data\test.txt”,即指定全路径,那么就到包含它的文件夹里找。
赵4老师 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yingzijuntuan 的回复:]
想问一个问题的,就是说建立了一个文件,那个文件会在哪里呀?
[/Quote]
Get the current working directory.

char *_getcwd( char *buffer, int maxlen );

yingzijuntuan 2011-12-14
  • 打赏
  • 举报
回复
想问一个问题的,就是说建立了一个文件,那个文件会在哪里呀?
AIGPTchina 2011-12-14
  • 打赏
  • 举报
回复
在VC工具里的。语法都是C语言。
CodeMaker_12138 2011-12-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wcg_jishuo 的回复:]

C/C++ code

CFileException e;

// CDialog::OnOK();
char pFileName[30];
COleDateTime timeNow, dateNow;
timeNow = COleDateTime::GetCurrentTime(); // 获取当前日期时间
dateNow = C……
[/Quote]
不好意思,我刚刚学C语言,不懂的很多,上面的程序是c的么?
沭水河畔 2011-12-14
  • 打赏
  • 举报
回复

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

int main()
{
time_t t;
struct tm *p;
FILE *fp = NULL;
char fname[256] = {0};

t = time(NULL);
p = gmtime(&t);

sprintf(fname, "%d-%d-%d.txt",1900+p->tm_year,1+p->tm_mon, \
p->tm_mday);
printf("%s\n", fname);
if((fp = fopen(fname, "w+")) == NULL)
perror("");
fclose(fp);
return 0;
}

只是一些时间函数而已。
AIGPTchina 2011-12-14
  • 打赏
  • 举报
回复 1

CFileException e;

// CDialog::OnOK();
char pFileName[30];
COleDateTime timeNow, dateNow;
timeNow = COleDateTime::GetCurrentTime(); // 获取当前日期时间
dateNow = COleDateTime::GetCurrentTime(); // 同样获取当前日期时间,这么写只是为了清晰
CString sTime = timeNow.Format(VAR_TIMEVALUEONLY); // 获取当前时间
CString sDate = dateNow.Format(VAR_DATEVALUEONLY); // 获取当前日期
CString FileName = sDate + "_" + sTime + ".txt"; // 文件名 = 日期_时间.txt

int fileNameLength = FileName.GetLength(); // 获得文件名长度,CFile不能用CString,只能用string做参数,要做转换
for(int i=0; i < fileNameLength ; i++)
{
pFileName[i] = FileName.GetAt(i); // CString -> string
if( pFileName[i] == ':') // 剔除':'等不能作为文件名的符号
pFileName[i] = '-';
}
pFileName[i] = '\0'; // 文件名结束,一定要加,不然有错

CFile f;
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) ) // 通过CFile的class member建立文件
{
AfxMessageBox("File can't be opened.");
}

return;

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧