silverthunder01
silverthunder01

Reputation: 11

gcc and lccwin32:different result

i try to compile this code:

#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;
}

the purpose of the program is to show 20 line content of a file. i compiled it in windows xp with lccwin32 and it works as expected. but problem arise when i change my os to linux (Ubuntu:pricise pangolin 12.04 LTS Desktop) and compile it with gcc.first it seems works fine but until the 20th line and prompt is out, when i put the argument (y for continue , q for quit)and hit the enter, but nothings happen. it just slipped away to elsepart which is starting again the program.so is it the gcc i have buggy or my code doesnt suit with gcc or may be i missed something?

Upvotes: 0

Views: 256

Answers (2)

Laurent Parenteau
Laurent Parenteau

Reputation: 2576

In addition to the issues reported by @Foon you also have those problems :

  1. fflush(stdin) is not working as you think it does.
  2. scanf() leaves the newline character in the input buffer.

Your problem is that there is still a newline (\n) in the input buffer when you call getchar(), so your y/q answer is not even read.

Replacing fflush(stdin) with a solution from 1., or replacing fflush()+getchar() with scanf("\n%c",&answer); should solve that particular issue.

Upvotes: 1

Foon
Foon

Reputation: 6468

I hate scanf. I would suggest replacing the scanf("%24s",name1) with fgets(s,24,stdin); (And then unfortunately doing if (s[strlen(s)-1] == '\n') s[strlen(s)-1] = '\0' to get rid of the \n at the end.

I would also suggest:

  1. Not use recursion on main
  2. Use int main(int argc, char *argv[]) and then passing the name of your file as an argument (so you would check that argc > 1 and then use argv[1] as the filename, and then when running the program do ./programname filename)
  3. Still not using scanf

Upvotes: 1

Related Questions