Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read floats, from a file and multiply/divide each float by a float, in the file. Varying the floats in a file proportionately and limit them between two numbers. essentially I have a data file from a sending program ( a coulomb of floats) that I want reed by another program but the data varies in value up/down more then the reading program can interpret. I'd like to do this in C language. Please help I have nothing.
Posted

Assumeing that you file contains the floats as text rather than a binary format:
1.201
137.889
...

You will have to open the file for read-write access, read it into you program (using fscanf) do you calculations, then reset the file stream and write it all back out again. This is because floats do not fit neatly into the file: 1.201 takes up less space in characters than 137.889, so you cannot just write the changed bits back into the file, or you will overwrite the data.

There is some of the detail you will need here: http://www.go4expert.com/forums/showthread.php?t=2977[^]
 
Share this answer
 
Comments
Espen Harlinn 30-Oct-11 11:59am    
Sounds like measurement data, so I hope he is getting them in binary IEEE 754 format, anyway my 5 :)
Richard C. Andrews 30-Oct-11 14:32pm    
IEEE 745 sound like a good format to use. I'll look into that, thanks.
OriginalGriff 30-Oct-11 12:14pm    
You specified floats in the question...
If you can do the file handling, where is the problem?
Richard C. Andrews 30-Oct-11 14:59pm    
File handling is the opening and closing of files of which was not my question.
Richard C. Andrews 30-Oct-11 12:52pm    
Yes the data is in text and in scientific notation... so I think they are longs not floats but thats not a diff I can't handle. I am aware of how to open, read and write a file.
While (if) you have access to the input interface of the first program
you can build the "translation law" between the two known limit points (linear case)
or through a multipoint set (other case).
Then you can use the law :)
 
Share this answer
 
Comments
Richard C. Andrews 31-Oct-11 13:31pm    
The first prog has an output interface with no need for an input interface. The only input to the first prog is the data stream to it. Oh wait, would you care to elaborate? Oh, yes the few funtions to squeeze my data into the second prog, yes that's all I need but can't wrap my head around it. I have nearly finish all the code but this last bit of it that goes in the middle somewere and maybe when I realize how to impliment the "translation law" with C, I will see further as to what I could do to extenuate my prog.:]
Richard C. Andrews 6-Nov-11 12:48pm    
First I realize that in order to fit my data file into the reading prog, I must know the limits that it can handle.
If that be +1 to -1 for 32 bit reading, then I can have a min of 4.6566e−10 for positive values and a min of −4.6566e−10 for negative values.
Next find the highest and lowest positive value, the highest and lowest negative value, in the data file to be sure that linear modification of the file will not result in lost data.
Using the value of the highest positive value, divide every positive value by it. Then using the highest negative value, divide every negative value by that.

So the program would go like this:

1. Take file names to be read and written at the command line.
2. Read line by line to find number of values.
3. Using a dynamic array for the number of values input them as floats to the precision needed.
4. Output the highest positive and negative numbers.
5. Write them as constants.
6. Output the lowest positive and negative numbers.
7. Write them as constants.
8. Using printf and scanf input to what limits are needed.
9. Write the divisor to a constant.
10. Output the divisor.
11. Divide the positive numbers of the file by the highest positive number.
12. Divide the negative numbers of the file by the highest negative number. (I suppose 11 and 12 be done with one complex function.)
13. Output the new values line by line to the new file.
Eugen Podsypalnikov 7-Nov-11 2:29am    
I would see the labor task (research only, probably your 1 up to 10 positions) to find out the k and d for the linear law: y = k*x + d ...where y is needed interpetation by the second program of the values x from the first program. Once they are found (in a labor(research) workaround), they could be used in the hard code of the second program (probably your 11 up to 13 positions) :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900