0

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?

2 Answers2

1

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
Foon
  • 6,148
  • 11
  • 40
  • 42
  • To be specific: scanf("%24s",name1) is taking the input you typed in up to the newline and storing it in name1. The next time you get something from stdin (using getchar), it gets the newline (\n) character. I have no idea why this behaves differently under Windows versus Linux... could be something to do with the whole stupid \r\n versus \n but I didn't think so. – Foon Jun 28 '12 at 15:14
  • And if you want to keep your code as written, I'd suggest just adding another check for if answer == "\n" retry getting input (EDIT on second thought) or better yet doing scanf("%24s\n",name) to cause the first scanf to discard the newline. – Foon Jun 28 '12 at 15:15
  • but if it like that. did it is flushed by `fflush(stdin)` back in the code, or somehow it doenst give any effect? – silverthunder01 Jun 28 '12 at 15:58
  • From man fflush on linux: For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been by the application. (Also note that "The standards do not specify the behavior for input streams. Most other implementations behave the same as Linux.") – Foon Jun 28 '12 at 17:03
  • so basically there is no standarization for getting rid of 'garbage' in memory and it cause other expression mistake it for real input...okay i get now..thanks for both of your help.. – silverthunder01 Jul 01 '12 at 01:42
1

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.

Community
  • 1
  • 1
Laurent Parenteau
  • 2,516
  • 20
  • 31
  • so why does it fail to work? the fflush? i still dont get it? especialy the absurd different between linux and windows compiler. – silverthunder01 Jun 28 '12 at 16:06
  • @silverthunder01 yes, since fflush(stdin) does nothing, so the '\n' from when you entered the file name is still in the input buffer. When you call getchar() it return you that character instead of the 'y' or 'q' you would press. – Laurent Parenteau Jun 28 '12 at 17:04
  • @silverthunder01 As for the differences between C compilers, remember that when things are not defined by the C standard they can do as they wish, which may result in different behaviours. – Laurent Parenteau Jun 28 '12 at 17:06