33,311
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<string.h>
int main()
{
int i=0,j=0,n=0;
char file_name[20],*pstr; //存放文件名
pstr=file_name;
FILE* fpin;
printf("输出几阶乘法表?");
while(n<1 || n>99) //输入数字限制在1~99之间
{
scanf("%d",&n);
if(n<1 || n>99)
{
printf("数字大于99或非法,请重新输入!\n");
}
}
//修改文件名为“n X n chengfb.txt”,存放在file_name中
//下面是出错的地方
//*pstr=((char)n);//刚开始这样写,但是存进去的是ASCII码,输出后不是数字,还出错
strcpy(pstr,(char)n);pstr++;
strcpy(pstr," X ");pstr+=3;
strcpy(pstr,(char)n);pstr++;
strcpy(pstr," chengfb.txt");
if((fpin = fopen(file_name,"w")) == NULL) //打开文件
{
printf("无法打开文件!\n");
system("pause");
exit(1);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
fprintf(fpin,"%d X %d = %-3d ",i,j,j*i);
}
fputc('\n',fpin);
}
fclose(fpin);
printf("已输出到当前目录下,文件名为“%d X %d chengfb.txt”\n",n,n);
system("pause");
}
#define openfile(n) fopen("n X n chengfb.txt","w")