Click here to Skip to main content
15,905,148 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

My program is running into an infinite loop. Can you please tell me what is wrong with the code?

char  ch=0;
FILE *p = NULL, *q = NULL;

p = fopen(argv[1], "r+");
q = fopen(tempfilename, "w+");
  while ((ch = getc(p)) != EOF)
     // Normal if else statements


I also tried giving.. but in vain.. :(
int ch;

Thanks in advance,

code has no mistake. only mistake i can see you are not verifying whether fopen can open the file or not. verify if p!=NULL. if p is null I need to ask what compiler are you using? probably old
Share this answer
Faez Shingeri 7-Aug-12 1:21am    
It indeed was the problem. The file isn't opening. BTW am using MinGW in eclipse. The problem is I am not able to get the input file. When I tried the debug mode, the code hangs here without asking me for the filename.

char inputfile[20];

printf("\n Enter the file name (i.e. input.txt or ABC.cbl etc): ");

Any idea why it behaving so..?
Mohibur Rashid 7-Aug-12 3:02am    
nope, have no idea. But I can suggest, why not trying absolute path and see if it can open the file, another thing- add the verification to ensure file is being opened
while ( 1 )
               ch = fgetc ( fs ) ;

               if ( ch == EOF )
                      break ;

                       fputc ( ch, ft )  ;


Happy Coding!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900