reading in a file

Started by
3 comments, last by Hand_Of_Destruction 18 years ago
i would like to read some values in a text file and use these values as object coordinate components in my application. the function that reads the file returns a char table. for instance : if a file contains a value of -1.0f the reading function will return a char table organised like that: table[0]=- table[1]=1 table[2]=. table[3]=0 table[4]=f As you can see, i can't use these values like that. I'm looking for a method which could allow me to transform theses char and recover the real value ?
Advertisement
What language do you use?

For C, there's the strtol()/strtof() and the (f)scanf functions.
Writing a function or method to do this yourself is trivial and would be a good excercise. Unfortunately I can't offer any direct solution though as I have always used my own functions for this.

I am unsure whether or not the strtol()/strtof() functions are part of the standard and may be undefined by the standard.
ToDoList.GrowthRate = WorkRate*2, it's more efficient to not work.

1) open file
2) copy whole contents into one string container

hope this helps:

#include <fstream>//copy file to existing_stringstd::ifstream config( para_configPath.c_str() );if(!config)	print::Instance().Error( "no_file", para_configPath );else{	std::ifstream file(filename);	existing_string.assign(std::istreambuf_iterator< char >(file), std::istreambuf_iterator< char >());config.close();


or

use your i/o class and convert it. (probably will be slower than the above)for( int i=0; i<Table.length(); i++ )	somestring+=Table;std::cout << somestring;


hope that was helpful.

Hmm... I think you are making some model file format or something. Sorry but why can't you just:

[SOURCE]float x;ifstream fin("c:\\blablabla.txt");while (fin.good())      {         fin >> x;     }[/SOURCE]

This topic is closed to new replies.

Advertisement