Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

csolar

Reading files in C++

This topic is 5718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is my exact situation: I am making a 3D game, a little one, and a small world editor. The game is in C++, and the World Editor in VB. Your supposed to make a new world in the world editor, then the game is supposed to open that saved file, read the contents, and place the 3drooms respectivly. So far, I have gotten as far as being able to open the file. I''ve tried stdio, ifstream, etc. Looked through the MSDEV files for hours, and surfed the internet for answers. Nothing I find will work for my needs. Most of them load the file by lines, or chars, into a big list. That okay if I want to display the data, but I want to convert it into a float, so I can do something mathmatically with it. But lists do not convert into regular floats. So if someone knows either, how to convert char* lists into regular floats, or they know of an eisier way, then please tell me! I''m pulling out my hair.

Share this post


Link to post
Share on other sites
Advertisement
well if you have a string like

"92.50"

you can use the atof function to convert a string to a float



Raymond Jacobs,
Profesional Web Applications Developer,
Weekend Game Developer,
www.EtherealDarkness.com

Share this post


Link to post
Share on other sites
You could use fstream read.


    
#include <fstream>

using namespace std;

ifstream infile("filename.bin", ios::binary);
float value;
infile.read(reinterpret_cast<char*>(&value), sizeof(float));


or you could read the whole file into an array.


#include <fstream>

using namespace std;

ifstream infile("filename.bin", ios::binary);
infile.seekg(0, ios::end);
int size=infile.tellg();
infile.seekg(0, ios::beg);
char* buffer=new char[size];

infile.read(buffer, size);

int pos=0;
float value=*reinterpret_cast<float*>(&buffer[pos]);
pos+=sizeof(value);
int anotherValue=*reinterpret_cast<int*>(&buffer[pos]);
pos+=sizeof(anotherValue);

//finally delete the buffer

delete [] buffer;




or you could make a binary buffer class
I have an uncommented binary buffer class, that works, so if you need it I can post it here.



extended the second example a bit, and removed a memory leak..


[edited by - fredizzimo on January 23, 2003 10:21:46 AM]

Share this post


Link to post
Share on other sites
Wow, only 10 mins. I don''t know why I dont come here first. Anyway, thanks for the quick reply, I''m going to try both of those solutions.

Share this post


Link to post
Share on other sites
You could also use fscanf to read the floats directly from the file, instead of converting to floats after it''s read.

Say you have a file like this..

1.2 1.5 1.7
1.1 2.8 10.0

You would just


  
FILE *in;
float OurFloat;
in = fopen("FileName","rb");
while (!feof(in)) //While not at the end of the file

{
fscanf(in,"%f ",&OurFloat); //Read in our float.

//Process it here..

}
fclose(in);


If you need more help than that, you can find me online at CrazyGuy4Eva (aol), Ready4Dis_1@hotmail.com (msn), or email me at Ready4Dis@aol.com

Share this post


Link to post
Share on other sites
quote:
Original post by csolar
I''m going to definently ditch MSDN.
Don''t! I mean, always try to look for answers yourself first, then come to ask.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!