//以下是调试好的一段程序,供你参考,大虾们考虑用日期做文件名有没有意义,我觉得如此讨论还不如给你段代码可能更有意义,就当我自己复习了一遍。//要了解时间的获取,请查阅以下网址://http://www.xrss.cn/Info/14539.Html#include <stdio.h> //printf(),fopen(),fclose()#include <io.h> #include <conio.h> //getch(),clrscr()#include <time.h> //时间函数定义 time_t,struct tm#include <string.h>#include <stdlib.h> //类型转换函数库,如itoa()#define NULL 0int main(){ clrscr(); //清屏 char fname[50],year[5],month[3],day[3]; //定义文件名,年月日 FILE *fp; //声明文件指针 struct tm *local; //声明时间结构指针 time_t t; //声明时间变量 t=time(NULL); //最1900年以来至现在的秒数 local=localtime(&t); //t里的秒数通过localtime函数转换为年月日时分秒等整数存到结构tm中,指针变量为local printf("%d/%d/%d\n",local->tm_year+1900,local->tm_mon+1,local->tm_mday);//打印结构里的日期成员变量值,年+1900进行修正,月+1进行修正 itoa(local->tm_year+1900,year,10); //整数型时间转换为字符串型存入year变量 itoa(local->tm_mon+1,month,10); itoa(local->tm_mday,day,10); strcpy(fname,"c:\\tc\\lx\\"); //拷贝路径至fname strcat(fname,year); //连接年份 strcat(fname,month); //连接月份 strcat(fname,day); //连接日期 strcat(fname,".txt"); //连接扩展名 printf("%s\n",fname); //打印文件名 fp=fopen(fname,"w+"); //打开文件 fprintf(fp,"%s","OK"); //将"OK"写入文件 fclose(fp); //关闭文件 getch(); return(0);}

评论