Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

unable to read file

Posted on 2002-04-02
6
Medium Priority
?
208 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

927 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