我不明白 文件操作 写入和关闭会报错
程序代码:
#include <stdio.h> #include <time.h> #include <string.h> #include <stdlib.h> void sort_red(int *a); //排序红球 int save(int *a,int b,int c) { const char * filename="C:\\Users\\lxz\\Desktop\\z\\abc.txt"; FILE * fp = fopen(filename,"ab"); if(fp=NULL) { printf("can not open the file : %s\n",filename); return -1; } char buf[1024]; sprintf(buf,"购买%d次\t 红球:%d %d %d %d %d %d 蓝球:%d\n",c,a[0],a[1],a[2],a[3],a[4],a[5],b); int n =strlen(buf); buf[n]=0; fwrite(buf,1,strlen(buf),fp); //fclose(fp); } void NOW_TIME() { time_t now = time(NULL); tm info = * localtime(&now); printf("启动时间为:%d年%d月%d日,%d:%d:%d\n", info.tm_year+1900,info.tm_mon+1,info.tm_mday,info.tm_hour,info.tm_min,info.tm_sec); } void END_TIME() { time_t end = time(NULL); tm info = * localtime(&end); printf("结束时间为:%d年%d月%d日,%d:%d:%d\n", info.tm_year+1900,info.tm_mon+1,info.tm_mday,info.tm_hour,info.tm_min,info.tm_sec); } void winning_number(int *a,int* b) { for(int i = 0 ; i<6;i++) //获奖红球 { a[i]=rand()%33+1; } for(int i =0;i<6;i++) //去除重复 { for(int j=1;j<6;j++) { if(a[i]==a[j]) a[j]=rand()%33+1; } } sort_red(a); *b = rand()%16+1; //获奖蓝球 } void sort_red(int *a) { for(int i =0;i<6;i++) //排序红球 { for(int j=i;j<6;j++) { if(a[i]>a[j]) { int tmp = a[i]; a[i]=a[j]; a[j]=tmp; } } } } void buy_number(int *a,int* b) { for(int i = 0 ; i<6;i++) //购买红球 { a[i]=rand()%33+1; } for(int i =0;i<6;i++) //去除重复 { for(int j=1;j<6;j++) { if(a[i]==a[j]) a[j]=rand()%33+1; } } sort_red(a); *b = rand()%16+1; //购买蓝球 } int bingo(int *a,int*b,int b1,int b2) { int ture = 0; for(int i = 0;i<6;i++) { if(a[i]==b[i]) { continue; } else return ture; } if(b1==b2) ture=1; return ture; } int main() { srand(time(NULL)); NOW_TIME(); int win_red[6]; int win_blue=0; winning_number(win_red,&win_blue); int buy_red[6]; int buy_blue=0; buy_number(buy_red,&buy_blue); printf("\n\n开奖结果为:\n"); printf("红球:\t"); for(int i =0;i<6;i++) printf("%d\t",win_red[i]); printf("蓝球\t%d\n",win_blue); int scond = 0; while(1) { scond++; if(bingo(win_red,buy_red,win_blue,buy_blue)!=0) break; save(buy_red,buy_blue,scond); printf("购买次数为:%d\t\t\t 总计花费:%d\n",scond,scond*2); } printf("购买次数为:%d\t\t\t 总计花费:%d\n",scond,scond*2); END_TIME(); //printf("总共历时%d\n",) //save(); return 0; }
这是代码,双色球测试什么时候获得一等奖, 当我文件操作 save函数, fwrite报错 fclose也报错 不知道为甚恶魔
第19/20行位置