a simple c code

C

candy

why this code enters an infinite loop.
i dont know what is the problem in
while((c=fgetc(file))!='\n' && c!='\r')
It enters the infinte loop in this statement.

The aim of the program is to remove comments of type //
It uses a temp file and after the operation , deletes the file.



//this is a test.
#include<stdio.h>
#define FALSE 0
#define TRUE 1

//this is a test.
int main(void){
char c;
FILE *file,*temp;
int last=FALSE;
file= fopen("file2.c","r");
temp=fopen("TEMP","w+");
if(!file || !temp){
printf("error");
exit(1);
}

while((c=fgetc(file))!=EOF){
if(c=='/')
if(last==TRUE){

while((c=fgetc(file))!='\n' && c!='\r')
;
}
else
last=TRUE;
else{
last=FALSE;

fprintf(temp,"%c",c);
}
}
return 0;
}
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

candy said:
why this code enters an infinite loop.
i dont know what is the problem in
while((c=fgetc(file))!='\n' && c!='\r')
It enters the infinte loop in this statement.

The aim of the program is to remove comments of type //
It uses a temp file and after the operation , deletes the file.



//this is a test.
#include<stdio.h>
#define FALSE 0
#define TRUE 1

//this is a test.
int main(void){
char c;
FILE *file,*temp;
int last=FALSE;
file= fopen("file2.c","r");
temp=fopen("TEMP","w+");
if(!file || !temp){
printf("error");
exit(1);
}

while((c=fgetc(file))!=EOF){
if(c=='/')
if(last==TRUE){

while((c=fgetc(file))!='\n' && c!='\r')
;

Add a test for EOF here as well. Not all source files ends with a
newline, which means that you're stuck in an eternal loop.


HTH
Bjørn

PS: How about replacing the "int last+TRUE/FALSE" logic with just a
character storing the last read character? Much easier to read, IMHO.

[snip]
 
A

Al Bowers

candy said:
why this code enters an infinite loop.
i dont know what is the problem in
while((c=fgetc(file))!='\n' && c!='\r')
It enters the infinte loop in this statement.
....snip...

int main(void){
char c;
....snip...


while((c=fgetc(file))!=EOF){

....snip...

Two things are apparent.
1) EOF is type int. Function fgetc returns type int. You are
trying to put it in type char.
2) In the loop, while((c=fgetc(file))!='\n' && c!='\r') ;
You may encounter the EOF without ever reading a '\n'
or '\r'.

One or both of these problems may cause you to continuously bang
against the wall.

Change c to
int c;

and the loop to
while((c=fgetc(file))!='\n' && c!='\r' && c != EOF)

then try it again.
 
I

Ishtiyaq

From: "candy" <[email protected]>
Newsgroups: comp.lang.c
Sent: Sunday, November 21, 2004 3:09 AM
Subject: a simple c code

why this code enters an infinite loop.
i dont know what is the problem in
while((c=fgetc(file))!='\n' && c!='\r')
It enters the infinte loop in this statement.

The aim of the program is to remove comments of type //
It uses a temp file and after the operation , deletes the file.



//this is a test.
#include<stdio.h>
#define FALSE 0
#define TRUE 1

//this is a test.
int main(void){
char c;
FILE *file,*temp;
int last=FALSE;
file= fopen("file2.c","r");
temp=fopen("TEMP","w+");
if(!file || !temp){
printf("error");
exit(1);
}

while((c=fgetc(file))!=EOF){
if(c=='/')
if(last==TRUE){

while((c=fgetc(file))!='\n' && c!='\r')
;
}
else
last=TRUE;
else{
last=FALSE;

fprintf(temp,"%c",c);
}
}
return 0;
}


Hi
You code will give problem in files like this

Test File //fsdfsf
Test File //

And if we modify the inner loop something like this

while((c=fgetc(file))!='\n' && c!='\r' && c != EOF) ;
if(c == EOF)break;

it works fine
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top