Solved

unable to read file

Posted on 2002-04-02
6
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 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