我尝试编译这段代码:
#include <stdio.h>
void print(FILE *a)
{
int main();
int count=20;
int c;
int stop=0;
char answer;
while(!stop){
while((c=getc(a))!=EOF){
fprintf(stdout,"%c",c);
if(c=='\n'){
count--;
if(!count){
printf("do you want continue:y=for continue/q=for quit");
fflush(stdin);
answer=getchar();
if(answer=='y' || answer=='Y')
count=20;
else if(answer=='Q' || answer=='q'){
printf("you quit this program,press any key and hit the enter to close");
stop=1;
break;
}
else{
printf("argument is unacceptable,rolling back action");
main();
}
}
}
}
if(c==EOF)
stop=1;
}
}
void halt()/*do nothing just for halt and waiting for input*/
{
int a;
scanf("%d",&a);
}
int main()
{
FILE *in,*fopen();
char name1[25];
int a;
printf("enter the name of the file you want to show:");
scanf("%24s",name1);
in=fopen(name1,"r");
if(in==NULL){
printf("the files doesnt exist or it is in another directory, try to enter again\n");
main();
}
else
print(in);
fclose(in);
halt();
return 0;
}
该程序的目的是显示一个文件的 20 行内容。我用 lccwin32 在 windows xp 中编译它,它按预期工作。但是当我将我的操作系统更改为 linux ( Ubuntu:pricise pangolin 12.04 LTS Desktop
) 并使用 gcc.first 编译它时出现问题,它似乎工作正常,但直到第 20 行和提示出来,当我输入参数(y
继续,q
退出)并按回车时,但什么也没发生。它只是滑到了else
重新启动程序的部分。所以是我有错误的 gcc 还是我的代码不适合 gcc 或者我错过了什么?