关于字符串的问题
原先,我是这么编译的:char infile[40],outfile[40];
int a[9][10]={0};
int x,y;
printf("请输入数,每行两个,例如 2 3\n");
printf("输入完成后请输入0\n");
infile="input.txt";
printf("output filename:");
gets(outfile);
但是在C语言编译中显示错误,原因是infile的赋值出问题了;
但是只要改成
char infile[40]="input.txt",outfile[40];
int a[9][10]={0};
int x,y;
printf("请输入数,每行两个,例如 2 3\n");
printf("输入完成后请输入0\n");
printf("output filename:");
gets(outfile);
问题就解决了,求教一下原先的问题主要是出在了哪,为什么?
万分感谢。
错误源程序:
#include <stdio.h>
#include <stdlib.h>
void main()
{FILE *fp;
char infile[40],outfile[40];
int a[9][10]={0};
int x,y;
printf("请输入数,每行两个,例如 2 3\n");
printf("输入完成后请输入0\n");
infile="input.txt";
printf("output filename:");
gets(outfile);
if((fp=fopen(infile,"r"))==NULL)
{
printf("Can't open the %s\n",infile);
exit(1);
}
do
{
fscanf(fp,"%d",&x);
if (x==0)
continue;
fscanf(fp,"%d",&y);
a[x-1][y-1]=a[x-1][y-1]+1;
a[x-1][9]=a[x-1][9]+1;
}while(x!=0);
fclose(fp);
if((fp=fopen(outfile,"w"))==NULL)
{
printf("Can't open the %s\n",outfile);
exit(2);
}
for (x=0;x<9;x++)
{
switch (x)
{
case 0 :fprintf(fp,"大学生活 "); break;
case 1 :fprintf(fp,"志愿填报 "); break;
case 2 :fprintf(fp,"自主招生 "); break;
case 3 :fprintf(fp,"假期安排 "); break;
case 4 :fprintf(fp,"学长高中生活 "); break;
case 5 :fprintf(fp,"学习方法 "); break;
case 6 :fprintf(fp,"复习技巧 "); break;
case 7 :fprintf(fp,"应考心理 "); break;
case 8 :fprintf(fp,"大学准备 "); break;
}
fprintf(fp,"选择人数:%d ",a[x][9]);
for (y=0;y<9;y++)
fprintf(fp,"顺序%d:%d人 ",y+1,a[x][y]);
fprintf(fp,"\n");
}
fclose(fp);
}