Solved

unable to read file

Posted on 2002-04-02
6
201 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
ID: 6913556
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
ID: 6913557
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
ID: 6913563
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:kir_ram_909
ID: 6915063
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
ID: 6957191
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
ID: 6979183
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

867 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

22 Experts available now in Live!

Get 1:1 Help Now