954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading a few chars from FileStream

I have a simple program that has retreived the first number of a 12 number set from a text file. I am trying to convert this character array into a double or float so I can work with it, but none of the conversions are working. Anyone have any suggestions how I can do this? My code is posted below.

String * strFilename = "c:/points";
	__wchar_t chrCoords __gc[] = new __wchar_t __gc[5];
	if (!File::Exists(strFilename))
	{
		Console::WriteLine(S"File not found.  Exiting...");
		return -1;
	}

	try
	{
		FileStream* fs = new FileStream(strFilename, FileMode::Open);
		StreamReader* sr = new StreamReader(fs);

		sr->ReadBlock(chrCoords, 1, 4);
		Console::WriteLine(chrCoords);
		float fltX = chrCoords;
		Console::WriteLine(fltX);
	}
	catch(System::Exception* ExErr)
	{
		Console::WriteLine(ExErr->ToString());
	}
Daywalker46410
Newbie Poster
6 posts since Sep 2003
Reputation Points: 10
Solved Threads: 0
 

um.. are you using C# or something? Anyways in C stdlib library there is a function that can do what you want.
sscanf() the name is.
An example of its use would be
sscanf (string,"%f",float); It will take the first float from the string provided on the left and put its value into the float variable provided on the right.

Valmian
Junior Poster in Training
82 posts since Sep 2003
Reputation Points: 13
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You