?
Solved

unable to read file propely

Posted on 2002-04-02
8
Medium Priority
?
198 Views
Last Modified: 2010-04-15
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
8 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 6913152
Your data consists of two "columns" of numbers.

However, your program opens the file, then reads the first float number into rows;

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

(I would expect it to pick up the first 0.0 here)

Then it goes into a loop to read pairs of time and distance:

for(i=0;i<50;++i)
{
    fscanf(rocket,"%d%d",&time,&distance);

time and distance are float variables, but they are being read with an integer format (%d).

In summary, I would think it should work a lot better, if you drop the first read for rows, then scan the pairs with:

    fscanf(rocket,"%f %f",&time,&distance);

0
 

Expert Comment

by:ComTech
ID: 6915505
listening...

CS Admin
0
 

Expert Comment

by:indira_shukla
ID: 6941690
I think following are the pobs in your program:
1) rows is declared integer but you are reading it as a floating point (although I know that it will type convert it).
2) when number of rows is in "rows" then why r u running the loop 50 times.. Is the file contain 50 rows?????

3)What is the initial value of distance,max_distance and min_distane?? so, the it will take the garbage and will not run properly.
4) you have taken max_distance and distance as floating point number and reading them as integers...This may be the source of improper working of the code..


 Just check these out...
Indira.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:tapasmondal
ID: 7000447
#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);/* YOU HAVE DEFINED ROWS AS INT TYPE. SO U HAVE TO REPLACE %f BY %d */
    printf("Number of rows:%f ", rows);
    }
    for(i=0;i<50;++i)
    {
         fscanf(rocket,"%d%d",&time,&distance);

/* YOU HAVE DEFINED TIME AND DISTANCE AS DOUBLE TYPE. SO U HAVE TO REPLACE %d BY %f */
              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



I THINK IT WILL WORK. IT WILL BE FINE IF U SUPPLY SOME DATA OF UR INPUT FILE.
0
 

Expert Comment

by:rodchamberlin
ID: 7123447
Actually,

since you have declared "rows", "time" and "disatance" as double you will need format specifier "%lf". "%f" would be for a "float" variable.

When you do the output (fprintf), you need to use "%f" or "%lf" (it doesn't actually matter which, since the 'C' language specification causes a float to be passed as a double when you are passing through a variable length argument list).

Hope this helps.
0
 

Expert Comment

by:cool_techie
ID: 7819753
are u sure u can write into file with "r" mode???
i guess problem is der.
see if is solved with r+
0
 

Expert Comment

by:cool_techie
ID: 7819762
i am sorry, i missed some code, there is no problem with file perms.
sorry again
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8279754
All,
I am unlocking this question in preparation for cleanup.  I will return in 7 days to finalize this question.  Please leave any recommendations for the final state of this question, I will take all recommendations into consideration.  Failing any feedback, I may decide in 7 days to delete or PAQ this question with no refund.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

719 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