473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("TEM P","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;
}
Nov 14 '05 #1
3 2134
candy wrote:
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("TEM P","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]
Nov 14 '05 #2


candy wrote:
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.

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3
From: "candy" <ca********@yah oo.com>
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("TEM P","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


Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6082
by: delisonews | last post by:
I'm looking for a simple, filesystem-based message board. (No MySQL!) Something that I could include easily in my code: include '../inc/messageboard.php'; .... so that the board shows up at the bottom of every PHP page. The board should have just the basic features, like: - posting capability
24
6348
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
10
110331
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and it also integrates UPX (Ultimate Packer for eXecutables) into the build process, if you have it installed. It also claims functionality on linux, as a bonus (I didn't test this). I highly recommend the UPX options, and will cover how to get it...
14
2990
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
2
1432
by: Michael7 | last post by:
Hi everyone, I'm new to CSS of course, and have been trying to learn it. However, when I try to pull off something as simple as positioning of text . . . nothing works in my index page. So in frustration I made a test page. Just a but of words to test only the code I'm looking for. Still nothing! I apologize if this question is a nuisance, but could anyone perhaps at least let me know what I'm doing wrong with this very simple thing?...
30
3554
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
1
2220
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner: http://groups.google.com/group/comp.lang.python/browse_thread/thread/58a01273441d445f/ also pointed to a simple eval function by Fredrik Lundh: http://effbot.org/zone/simple-iterator-parser.htm. His solution, using module tokenize, was short and...
10
2142
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http:// wwwpreview.#deleted#.co.uk/~ptaylor/Customer.wsdl")_
1
2077
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is...
17
5829
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.