Solved

unable to read file

Posted on 2002-04-02
6
200 Views
Last Modified: 2010-04-02
i am suppose to read a file of munbers, ist row column is the time, 2nd column is the distance, program
issuppose to print out the  number of rows, maximum distance and minimum distance with their respective
times. i am suppost to use a sentinel of -1 . i am almost done with the codes, but it doesn't seem to
be reading propely.



#include<stdio.h>
#include<stdlib.h>
#include<io.h>
#define Sentinel -1

void main()
{
    int i,rows;
    double time,min_time,max_time;
    double distance,min_dist, max_dist;

    FILE *rocket;
    FILE *data;

    rocket = fopen("rocket.txt","r");
    if(rocket==NULL)
    {
         printf("file not found\n");
    }
    else
    {
    fscanf(rocket,"%f",&rows);
    printf("Number of rows:%f ", rows);
    }
    for(i=0;i<50;++i)
    {
         fscanf(rocket,"%d%d",&time,&distance);
              if(distance>max_dist)
              {
                   max_dist=distance;
                   max_time=time;
              }
if(distance<min_dist)
              {
                   min_dist=distance;
                   min_time=time;
              }
    }
fclose(rocket);

data=fopen("data.txt","w");
fprintf(data,"Minimum Distance = %4.1d (Time = %d)",min_dist,min_time);
fprintf(data,"Maximum Distance = %4.1d (Time = %d)",max_dist,max_time);
   
    fclose(data);
}


0.0     0.0
30.0     5000.0
60.6     25000.0
90.0     60000.0
100.0     75000.0
110.0     30000.0
140.0     10000.0
170.0     2500.0
200.0     0.0
-1.0     -1.0
0
Comment
Question by:kalakutta
6 Comments
 
LVL 4

Accepted Solution

by:
mblat earned 50 total points
Comment Utility
Couple things:

   fscanf(rocket,"%f",&rows);
   printf("Number of rows:%f ", rows);

rows defined as int, and that makes sense since it is "number of rows".  You are reading it as float and that doesn;t make sense.

second:


 if(distance>max_dist)
 {
 max_dist=distance;
                  max_time=time;
 }
if(distance<min_dist)
             {
                  min_dist=distance;
                  min_time=time;
             }
0
 
LVL 4

Expert Comment

by:mblat
Comment Utility
Couple things:

   fscanf(rocket,"%f",&rows);
   printf("Number of rows:%f ", rows);

rows defined as int, and that makes sense since it is "number of rows".  You are reading it as float and that doesn;t make sense.

second:


 if(distance>max_dist)
 {
 max_dist=distance;
                  max_time=time;
 }
if(distance<min_dist)
             {
                  min_dist=distance;
                  min_time=time;
             }
0
 
LVL 4

Expert Comment

by:mblat
Comment Utility
SORRY hit enter buttom by accident.  so
if(distance>max_dist)
{
max_dist=distance;
                 max_time=time;
}
if(distance<min_dist)
            {
                 min_dist=distance;
                 min_time=time;
            }

if I am not mistaken you are looking for MIN and MAX values here.

To make this algorithm work you need to initialize

max_dist to MINIMAL possible value, let's say 0 and
min_dist to MAXIMUM possible value, whatever it is.

Hope it helps
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:kir_ram_909
Comment Utility
Declaring rows as int and reading it using a float conversion character is not a proper that may result in error
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
Dear kalakutta

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "mblat"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now