#include <stdio.h>
#include <string.h>
#include <time.h>
void main()
{
FILE *fp;
char ch[50];
char name[10] = "111";
char data[5] = ".txt";
ch[0] = '\0';
data[4] = '\0';
time_t timep;
time(&timep);
*(asctime(localtime(&timep)) + 24) = '\0';
strcat(ch, asctime(localtime(&timep)));
strcat(ch, data);
fp = fopen(ch, "a");
fclose(fp);
}
这哪知道在哪里出问题
文件创建失败,文件名ch中含有不合法字符 :
时间分隔符: 10:06:03
你是想以日期做文件名吧,参考:
http://www.cnblogs.com/doublesnke/archive/2011/08/05/2128558.html
#include <stdio.h>
#include <string.h>
#include <time.h>
void main()
{
FILE *fp;
char ch[50];
time_t timep;
time(&timep);
strftime( ch, sizeof(ch), "%Y%m%d%H%M%S.txt",localtime(&timep) );
fp = fopen(ch, "a");
fclose(fp);
}
这是因为你所要打开的文件失败,即fp返回的是NULL,所以在close的时候报错,一般在fopen之后都要判断fp例如
fp = fopen(ch,"a");
if(fp != NULL)
{
fclose(fp);
}
#if 1
#include
#include
#include
void main()
{
FILE *fp;
char ch[50] = "";
char name[10] = "111";
char data[5] = ".txt";
ch[0] = '\0';
data[4] = '\0';
time_t timep;
time(&timep);
*(asctime(localtime(&timep)) + 24) = '\0';
strcat(ch, asctime(localtime(&timep)));
ch[24] = '#';
ch[13] = ' ';
ch[16] = ' ';
strcat(ch, data);
//fp = fopen("Mon Jan 11 15:47 : 36 2016\n.txt", "a");
//此处文件打开失败!所以会在此处抛异常...
//if (NULL == (fp = fopen("Mon Jan 11 15:47 : 36 2016.txt", "a")))
//微软明确规定,给文件命名的时候不能够去加:等需要转义的字符,包括\n...
if (NULL == (fp = fopen(ch, "a")))
{
printf("打开失败\n");
}
else
{
printf("打开成功\n");
fclose(fp);
}
}
#endif
我给你把\n跟:改成了空格,就可以
代码呢?只有图。。。