Jump to content
  • Advertisement
Sign in to follow this  
Zarathustra

C++ read floats from a file

This topic is 3909 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

atm I have this float* loadData(){ string line; ifstream fin ("sigmavalues.txt"); int nv; float* f; fin >> nv; f = new float[nv]; for(int i=0; i<nv; i++){ fin >> f; } fin.close(); return f; } void initData(){ float *dataBuffer = loadData(); But I don't get my data in that buffer >_< So what would be the correct way to do this ?

Share this post


Link to post
Share on other sites
Advertisement
const unsigned int buffersize = 32;
char buffer[ buffersize ];

fin.getline( buffer, buffersize );
float num = atof( buffer );

Share this post


Link to post
Share on other sites
You might also want to check to see if that the file being opened correctly -- using ifstream::is_open() method (in your case would be fin.is_open()). Also make sure that the file in question is in the working directory of the program.

EDIT:

int nv;
float* f;
fin >> nv;
f = new float[nv];





It would also seem that your program expects the file to have first an integer, specifying the number of entries in the text file.


5
0.363
1.316
0.361
1.382
0.3561

Share this post


Link to post
Share on other sites
std::vector< float > loadData()
{
ifstream fin("sigmavalues.txt");
return std::vector< float >(std::istream_iterator< float >(fin), std::istream_iterator< float >());
}

Σnigma

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
You might also want to check to see if that the file being opened correctly -- using ifstream::is_open() method (in your case would be fin.is_open(). Also make sure that the file in question is in the working directory of the program.

EDIT:
*** Source Snippet Removed ***

It would also seem that your program expects the file to have first an integer, specifying the number of entries in the text file.


5
0.363
1.316
0.361
1.382
0.3561


as long as it is only himself that are using it, it doesn't matter if it's only something temporary...

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall


It would also seem that your program expects the file to have first an integer, specifying the number of entries in the text file.


5
0.363
1.316
0.361
1.382
0.3561




.. you're absolutely right :/

I can be such an idiot sometimes. Thanks alot.

Share this post


Link to post
Share on other sites
Enigma, yeah sure, short solutions are fine, but he shouldn't use that unless he understands how it works and what's happening behind the curtains.

Share this post


Link to post
Share on other sites
Quote:
Original post by Molle85
Enigma, yeah sure, short solutions are fine, but he shouldn't use that unless he understands how it works and what's happening behind the curtains.
You'd prefer the version with potential memory leaks or the one with two potential sources of undefined behaviour? A quick google search for "istream_iterator tutorial" should be plenty to explain the basics.

Σnigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!