Solved

reading file data

Posted on 2004-10-02
11
200 Views
Last Modified: 2010-04-15
I’m having problem with my text file. Not too sure on which function to use in this program.

My text file contains integer numbers and float. It has many lines and 1 line contain more than 20 numbers. There can be a mix of number type within that line.

Here is the pseudo code of it.

Open text file
Read number position 1-15 (regardless of the line)
For every 4th position
Read and compare it with a threshold value
If the 4th position < threshold value
Add 1 to sum
End Loop
Print the total sum

I would really appreciate if anyone could guide me step by step in doing it.
Thanks in advance.
0
Comment
Question by:nor_miranda
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 12210423
 int i;
  float f[15];
  for( i=0; i<15; i++ ){
    scanf(" %f",&f[i]);
  }
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12210461
Use fopen() to open the text file.

FILE *fp;
fp=fopen("filename.txt","r");

As ozo said,use fscanf() to read the 15 float numbers(note fscanf is for reading from file,and you read all your numbers ,int or float, into float variables.)

 int i;
 float f[15];
 for( i=0; i<15; i++ ){
   fscanf(fp," %f",&f[i]);
 }

Once you have the 15 numbers in the array,you easily compare every 4th position,just simple array indexing.
0
 

Author Comment

by:nor_miranda
ID: 12210632
Thanks for the help!
This is what I have done for the opening of the file.

#include <stdio.h>

int main()
{
  FILE *data;
  data = fopen("wind_data.txt", "r");

return(0);
}

Actually, I don't quite understad the for loop.

Here is an example of the data...

98 5 31 17.62 19.95 6.31 2.66 4.44 12.55 1.66 2.99 18.30 12.60 14.32 5.99 7.69 8.99 2.33 97 5 20 6.93 2.66 12.44 11.03 18.23 14.17 12.27 11.20 10.39 5.64 8.69 7.56 98 12 8 1.36 7.96 12.34 19.83

In this case... my program should captured the num "17.62", "6.93", "1.36".
Will the code above works?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12211646
Basically,you needed to read 15 numbers from the file,right?

fscanf(data," %f",&f[i]);

reads a float value from the file into an element of the array f.
Which element?the one indexed by the subscript i.

so instead of 15 fscanf statements,we use a for loop to execute the fscanf statement 15 times and store the corresponding values into an array.

So,after the for loop,you can print every 4th element of the array.
float thresh=9.9;
for(i=3;i<15;i+=4)
{
  if(f[i]>thresh)
    printf("%f\n",f[i]);

}
0
 

Author Comment

by:nor_miranda
ID: 12212722
Well actually, I need to read more than 15 numbers. In the text file it contains thousand over numbers.

For every 0-15 number position the program should compare the 3rd position with the threshold value say (6.89). This loop will continue till all the numbers has been compared.

It means that,

[98 5 31 17.62 19.95 6.31 2.66 4.44 12.55 1.66 2.99 18.30 12.60 14.32 5.99]
[97 9 20 6.93 2.66 12.44 11.03 18.23 14.17 12.27 11.20 10.39 5.64 8.69 7.56]
[93 12 8 1.36 7.96 12.34 19.83 12.68 11.02 4.36 7.62 10.03 7.69 8.99 2.33]...

Therefore, the program should compare the number "17.62", "6.93", "1.36" with the threshold value...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:ankuratvb
ID: 12212768
Put the for loop in another loop which will read from file till EOF

FILE *fp;
int i;
float f[15];
fp=fopen("filename.txt","r");
while(feof(fp)==0)
{
 for( i=0; i<15; i++ ){
   fscanf(fp," %f",&f[i]);
 }
 printf("%f\n",f[3]);  //Print every 4th element from each set of 15
}
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12214796
After looking at your data, I have come to this conclusion.
The first 3 numbers are of type integer. The next 12 will be of the type float. This pattern repeats.

So, you will have to use 2 loops like this:

#define THRESHOLD 6.89

FILE *fp;
int i;
int int_nums[3];
float float_nums[12];
fp = fopen("data.txt","r");
while (!feof(fp))
{
    for(i = 0; i < 3; i++)
    {
        fscanf(fp,"%d", &int_nums[i]);
    }
    for(i = 0; i < 12; i++)
    {
        fscanf(fp,"%f", &float_nums[i]);
    }

    if (float_nums[0] == THRESHOLD)
    {
         // THRESHOLD matches.
    }
    else
    {
         // THRESHOLD doesn't match.
    }
}

Hope this helps.....

-ssnkumar
0
 

Author Comment

by:nor_miranda
ID: 12223915
Thanks kumar for your help!

Anyway, this is what I've added to the code that was suggested earlier.

However, I still have a liltle problem with counting the total of number data that is below the thrashold value.

#include <stdio.h>

int main(void)
{
      int i,sum;
      float avg=5.828;
      float f[15];

      FILE *data;
      data = fopen("avg_data.txt","r");

     while(feof(data)==0)
     {
        for( i=0; i<15; i++ )
           {
                 fscanf(data," %f",&f[i]);
            }
            printf("%.2f\n",f[3]);
      }
      if(f[3]< avg)
      sum++;
      printf("Number of data that is below average is %d\n",sum);

 return 0;
}


0
 
LVL 8

Accepted Solution

by:
ssnkumar earned 125 total points
ID: 12224418
Some corrections are needed to your code.
Change this:
>     while(feof(data)==0)
To:
      while(!feof(data))

In the following lines:
>        for( i=0; i<15; i++ )
>           {
>                 fscanf(data," %f",&f[i]);
>            }
Still you are reading all the 15 numbers as float.
But, in your data first 3 are integers.
And in the statement:
> if(f[3]< avg)
>      sum++;
This statement has to be inside the while loop.
And before the while loop, sum has to be initialized to 0.

One query regarding your data file:
Are all the numbers are on the same line? OR each 15 set is on a different line?

And if you are reading from a text file,  there can be a new line character at the end of the last line.
If so, then iteration will go to one extra loop and you will get wrong answer.
To take care of this, you will have to read using fgetc() at the end of each 15 numbers. If this char is a '\n', then read another char and check if you have reached end of file. If not, then do ungetc() and go back to loop.
If the character read is not 'n' and if you have not reached eof, then also you will have to do ungetc().

Please let us know if you still have problems.

-ssnkumar
0
 

Author Comment

by:nor_miranda
ID: 12224606
In one line there will be more than 15 numbers.

However, the program should so called blocked any 15 numbers in order for the program to meet all the conditions.

There is no new line in the text file. It is a continuous flow separated by a space.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12224741
So, what is the status of your problem?
Did you try out my suggestions?
Give your feedback.

-ssnkumar
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now