前往
大廳
主題

C/C++紀錄二十一.<<檔案處理,輸入資料>> 2018/10/18

艾倫D索妮雅 | 2021-06-08 19:57:49 | 巴幣 0 | 人氣 79

//指定的檔案路徑:D:\MicroSoft\Desktop\c++goal.txt
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<io.h>
#include<ctime>
#define len 100
using namespace std;
int main()
{
FILE *fptr;
char fpath[len];
int i,j,max,count,n,pick;
char lott[49][3]={"01","02","03","04","05","06","07","08","09","10",
                   "11","12","13","14","15","16","17","18","19","20",
   "21","22","23","24","25","26","27","28","29","30",
   "31","32","33","34","35","36","37","38","39","40",
   "41","42","43","44","45","46","47","48","49"};
char temp[49][3];   
char select[7][3];   
unsigned seed;
strcpy(select[6],"\r\n");
cout<<"-傳送10組樂透彩至指定檔案試驗-"<<endl<<endl;
cout<<"請輸入欲將資料輸進去的檔案路徑:";
gets(fpath);
fptr=fopen(fpath,"w");
if(fptr==NULL)
{
cout<<"檔案路徑不存在"<<endl;
exit(0);
}
seed=(unsigned)time(NULL);
srand(seed);
count=0;
while(count<10)
{
n=0;
max=49;
for(i=0;i<49;i++)
{
strcpy(temp[i],lott[i]);
}
while(n<6)
{
pick=rand()%max;
strcpy(select[n],temp[pick]);
for(i=pick;i<max-1;i++)
{
strcpy(temp[i],temp[i+1]);
}
max--;
n++;
}
for(i=0;i<6;i++)
{
cout<<select[i]<<" ";
}
cout<<endl;
fwrite(select,sizeof(select),1,fptr);
count++;
}
return 0;
}

創作回應

更多創作