Sign in to follow this  
the_cyberlord

Reading floats from a file in C++

Recommended Posts

Quote:
Original post by the_cyberlord
How can you read for example: float values from a file and assign them to a float?

// It's quite easy in ascii mode.
float ReadAsciiFloat(FILE *File) {
float Value;

fscanf(File, "%f", &Value);

return Value;
}

// And almost as easy in binary mode.
// It might get you into portability problems though. Like different byte orders and non IEEE floating point formats.
float ReadBinaryFloat(FILE *File) {
float Value;

fread(&Value, sizeof(Value), 1, File);

return Value;
}


Share this post


Link to post
Share on other sites
Well first of all, have you read the file? Lets presume you have first.

You first need to filter the value to a seperate character-array (this works a bit better, since less can go wrong with the conversion)

If you have filtered the value then you convert it to a float:



float fX = (float)cFilter;




It should work with this, but I make errors too. When you convert something to an other type of variable, then you use the ' type variable-name2 = (type to convert to)variable-name1; ' statement.


Share this post


Link to post
Share on other sites
In case you want to be more oop [smile], you can use streams. To read from text file:


#include <fstream>

using namespace std;

...

fstream f_in;
float f;

f_in.open("file.txt", ios_base::in);

f_in >> f;

f_in.close();

Share this post


Link to post
Share on other sites
whoot!
I'm not very advanced you know, can somebody write a piece of code that only reads all the all the values in in a file (ASCII), in the order that they are in the file, seperated by spaces, and converts them to float values and then puts them in a vector?
( and all using the C++ standard libs preferably with <string> and <iostream> )
many thanks!

Share this post


Link to post
Share on other sites
Since none mentionned these two points :

A) One thing you have to remember when you read atomic types in files is :
endianness (Google)

B) One thing you have to remember about reading structures is :
structure alignment (Google)


If you don't want to have bugs or create complications if someone ports your softare from PC to Mac for intance, remember A).

If you don't want to spend hours figuring out why your favorite importer crashes, just to discover it's because you changed some compiler settings, learn what B) is all about.

Share this post


Link to post
Share on other sites
Simple app that will read all values from file.txt and store them in vector. No error checking is done.


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(void)
{
ifstream f_in;
vector<float> values;

// open file
f_in.open("file.txt", ios_base::in);

// go through whole file
while (!f_in.eof())
{
float f;

// read value, you don't need to care about spaces, tabs or newlines
f_in >> f;

// add value to vector
values.push_back(f);
}

// close file
f_in.close();

// write values to output
for (vector<float>::iterator it = values.begin(); it != values.end(); ++it)
{
cout << *it << endl;
}

return 0;
}




And version without streams:


#include <stdio.h>
#include <vector>

using namespace std;

int main(void)
{
FILE *f_in;
vector<float> values;

f_in = fopen("file.txt", "r");

while (!feof(f_in))
{
float f;

fscanf(f_in, "%f", &f);

values.push_back(f);
}

fclose(f_in);

// write values to output
for (vector<float>::iterator it = values.begin(); it != values.end(); ++it)
{
printf("%f\n", *it);
}

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by b2b3
Simple app that will read all values from file.txt and store them in vector. No error checking is done.


its actually more simpler than that assuming its just consecutive floats separated by white-space e.g.:


#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <vector>
#include <iostream>

int main(int argc, char* argv[]) {

if(argc == 1)
return EXIT_FAILURE;

std::vector<float> vfs;

std::ifstream ifs(argv[1]);

std::copy(std::istream_iterator<float>(ifs),
std::istream_iterator<float>(),
std::back_inserter(vfs));

ifs.close();

std::copy(vfs.begin(), vfs.end(),
std::ostream_iterator<float>(std::cout, ", "));

return EXIT_SUCCESS;

}


but with-out the actual file format that just a stab in the dark.

Quote:
Original post by the_cyberlord
you mean, reading ascii numbers from a file and putting them in a numerical data type automatically converts them?


doesn't convert, the I/O streams parse text into appropriate type in a type safe manner (when used correctly), they do the parsing for you.

Share this post


Link to post
Share on other sites
Why does this give an endless loop?

GLint loader(vector<GLfloat> &model )
{
ifstream f_in;
// open file
f_in.open("rawtri.raw", ios_base::in);

// go through whole file
while (!f_in.eof())
{
GLfloat f;

// read value, you don't need to care about spaces, tabs or newlines
f_in >> f;

// add value to vector
model.push_back(f);
cout << "Added coordinate";
}

// close file
f_in.close();

// write values to output
for (vector<GLfloat>::iterator it = model.begin(); it != model.end(); ++it)
{
cout << *it << endl;
}

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by the_cyberlord
Why does this give an endless loop?

GLint loader(vector<GLfloat> &model )
{
ifstream f_in;
// open file
f_in.open("rawtri.raw", ios_base::in);

// go through whole file
while (!f_in.eof())
{
GLfloat f;

// read value, you don't need to care about spaces, tabs or newlines
f_in >> f;

// add value to vector
model.push_back(f);
cout << "Added coordinate";
}

// close file
f_in.close();

// write values to output
for (vector<GLfloat>::iterator it = model.begin(); it != model.end(); ++it)
{
cout << *it << endl;
}

return 0;
}



GLint loader(vector<GLfloat>& model ) {

std::ifstream ifs("rawtri.raw");

std::copy(std::istream_iterator<float>(ifs),
std::istream_iterator<float>(),
std::back_inserter(model));

ifs.close();

std::copy(model.begin(), model.end(),
std::ostream_iterator<float>(std::cout, ", "));

return 0;
}


are you sure your reading a text file?

Share this post


Link to post
Share on other sites
if I save it as a txt file, it still acts like there's no end at the file
and this is the console output of my program
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
Added coordinate
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
-1.07374e+008
Press any key to continue
as you see, it doesn't read the right values, and it only reads the first value.
I limited it to read just 18 values, because if 'file end' is used, he falls in that endless loop

Share this post


Link to post
Share on other sites
well i just tested both versions of code and they both work with this input file:


5.511850 5.511848 0.000000 -5.511847 5.511851 0.000000 -5.511850 -5.511850 0.000000
-5.511850 -5.511850 0.000000 5.511850 -5.511851 0.000000 5.511850 5.511848 0.000000


maybe you should tells us what compiler your using & post all your code in source tags in here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this