The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Floating Point Exception Mandar123 Linux 3 01-22-2009 09:51 AM
Floating point exception !!! ssk01 Linux 3 12-24-2008 02:20 PM
Floating point error in C Hara High Level Programming 2 06-18-2008 06:43 AM
Rounding off the value of Floating point value damansingh Shell Programming and Scripting 7 05-21-2008 10:46 AM
Floating Point Division gsatch Shell Programming and Scripting 1 07-25-2002 05:03 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-11-2009
headrush headrush is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 1
floating point error in linux + C

Here's a program and its pretty simple .It requires file handling and some calculations but on running it I am not getting the required result.It seems that the code outside the file read's outer while loop is not executing e.g the print statement is not being printed.Plz Help!

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main(int argc,char *args[])
{
FILE *fp;
char ch;
int load=0,value=0,weight=0;
int i=0,count=0,p,k=1,j,*cost,line=1,u;
if(argc!=2){
printf("\nInsufficient no. of arguments");
exit(0);
}
fp=fopen(args[1],"r");
if(fp==NULL){
printf("\nFile could not be opened");
exit(0);
}
while((ch=fgetc(fp))!='\n'){
if(isdigit(ch)){
p=ch-'0';
load=load*10+p;
}
}
printf("\nload= %d \n",load);
i=0;
while((ch=fgetc(fp)!=EOF)){
value=0;
weight=0;
k=0;
u=0;
while(ch!='\n'){
// printf("\nline=%d,weight=%d,value=%d",line,weight,value);

if((isalpha(ch)||isdigit(ch))&&k==0){
while(ch!=' '){
ch=fgetc(fp);
}
k++;
}
if(weight==0){
while(ch!=' '){
p=ch-'0';
weight=weight*10+p;
ch=fgetc(fp);
}
}
// printf("\nweight=%d",weight);
if(value==0){
while(ch!='\n'){
p=ch-'0';
value=value*10+p;
ch=fgetc(fp);
}
}
// printf("\nValue=%d",value);
if(ch==' '){
while(ch==' '){
ch=fgetc(fp);
}
}
}

u=value/weight;
printf("line=%d weight=%d,value=%d,cost=%d\n",line,weight,value,u);
line++;
}
// count=i-1;
fclose(fp);
return 0;
}

The text file that I am using is strictly of the following format:
1250
LJS93K 1300 10500
J38ZZ9 700 4750
HJ394L 200 3250
01IE82 75 10250

there is a line-break after each line and the first line(1250) is the "load" value,the first column of every is to be avoided the 2nd column is weight and the 3rd column is value.
One more thing I am using ubuntu distro which uses bash shell!
  #2 (permalink)  
Old 03-11-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,969
Code tags for code please, they make the unreadable readable. [ code ] stuff [ /code ] without the extra spaces.

You get 'floating point error' when you divide by zero, check to see if things are getting parsed the way you expect.

You do not need to use fgetc to read in text data! Try scanf. Or better yet, sscanf. Read in data one line at a time then feed it through scanf to get what you want in one go.


Code:
char buf[512], garbage[512];
if(fgets(buf, 512, fp)==NULL0
{
  fprintf(stderr, "Can't read first line\n");
  return(1);
}

if(sscanf(buf, "%d", &load) != 1)
{
  fprintf(stderr, "Can't get load value\n");
  return(1);
}

while(fgets(buf, 512, fp)!=NULL)
{
  if(sscanf(buf, "%s %d %d", garbage, &weight, &value) != 3)
  {
    fprintf(stderr, "Couldn't parse line '%s'\n", buf);
    continue;
  }

  do_stuff();
}

You'll get purists complaining about scanf and its dangers but its far preferable to building your own integer-parsing routines. Used in this fashion it is fairly safe, with no risk of buffer overflows and none of the strange half-a-line-eaten problems plain scanf is infamous for.

Last edited by Corona688; 03-11-2009 at 11:25 PM.. Reason: adding more code
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0