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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

unable to read file propely

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
kalakutta
Asked:
kalakutta
1 Solution
 
imladrisCommented:
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
 
ComTechCommented:
listening...

CS Admin
0
 
indira_shuklaCommented:
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.

 
tapasmondalCommented:
#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
 
rodchamberlinCommented:
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
 
cool_techieCommented:
are u sure u can write into file with "r" mode???
i guess problem is der.
see if is solved with r+
0
 
cool_techieCommented:
i am sorry, i missed some code, there is no problem with file perms.
sorry again
0
 
SpideyModCommented:
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

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now