Solved

unable to read file

Posted on 2002-04-02
6
206 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

636 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