help with conversion from char vector to float

Reading numbers from a txt file and converting them into a float;

Page 1 of 1

3 Replies - 1708 Views - Last Post: 15 April 2009 - 07:30 AM Rate Topic: -----

#1 jarami82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-April 09

help with conversion from char vector to float

Posted 15 April 2009 - 06:17 AM

I have a proble programing a function that reads from a .txt file

This is part of my code



#include <windows.h>

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>

#include <math.h>
#include <cstdlib>
#include <sstream>


#include <vcclr.h>


namespace prueba {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace std;

	using namespace System::Runtime::InteropServices;
	//Decalaracion variables globales.
	char *filenameaux; 
	char *filename1; 
	char *filename2; 
	char *filename3 = "C:/Proyecto/temp/datos1.txt";
	char *filename4 = "C:/Proyecto/temp/datos2.txt";
	char* Path1Mod;
	char* Path2Mod;

	float matrizIni[450][80];
	float matriz[450][80];
	float matrizNormIni[450][80];
	float matrizNorm[450][80];
	float media[80];
	float ucl[80],lcl[80];
	int maximo;
	int punto,coordenada;
	
	
	//Decalaracion de metodos


//-----------INICIO LEER ARCHIVO---------------------------------------

int leerArchivo(char *filename) //throw (std::exception)
{
	
	ifstream fichero1(filename); // abrir archivo para lectura

	/*string s_num ="0";
	char c_num = '\0';
	int i,j,k,contador_total;
	float valor;
	const char *cad;	
	float varianza[80];
	float sigma[80];   
	float xpcg[450],ypcg[450],zpcg[450];
   */
	char s_num[7];
	char c_num = '\0';
	char c_num2 = '\0';
	char *cad;
	int i,j,k,contador_total;
	double valor;	
	float varianza[80];
	float sigma[80];   
	float xpcg[450],ypcg[450],zpcg[450];

		
	// verificar la apertura del archivo
	if ( fichero1.bad() ) {
		//cout << "Error al tratar de abrir archivo";
		//cin.get();
		return 1;
		//label6->Text="Error al tratar de abrir archivo";
		
	} 

	// lectura de datos
	j = 0; 

   while( ! fichero1.eof()) {
			
					 		  
			  k=0;			  
			  while(k<79)
			  {   
				  
				  i=0;
				  fichero1.get(c_num);  
				  if(c_num != char(32))
				   {
					if(c_num != char(10))
					 {
							  
				  s_num[i] = c_num;
						   i++;		
		c_num2=c_num;
						  
				while((i<7) && (c_num2 != char(32)) && (c_num2 != char(10)))
				{
							
									 fichero1.get(c_num);
									 s_num[i] = c_num;
									 i++;
																	 
								  } 

							cad=s_num;
						   
							   
						 // valor = strtod(s_num, NULL);
												   //  cad = s_num.c_str();

						   //istringstream in(s_num);
						   //s_num >> valor;
						  valor=atof((const char*)cad);


						   /*cad = s_num.c_str();
						   valor=atof(cad);
						   matriz[j][k] = valor;
						   k++;*/
					  }
					 }
			  }
		  j++; 
	}
	maximo = j;
 
   //copia de la matriz inicial
   for(j=0;j<maximo;j++)  
	{
	   for(k=0;k<79;k++)
	   {									 
		matrizIni[j][k]=matriz[j][k];  
	   }				 
	 }  
							
	fichero1.close();

return maximo;

}





This function is suposed to read float numbers separated with spaces froam a txt file.

I put each digit of the float number in a vector(i also tried with a string)
and then tried to convert them into a float

As you can see in this part of the code i have tried many ways to solve the problem



						 cad=s_num;
			 valor=atof((const char*)cad);			   
							   
				// valor = strtod(s_num, NULL);
						 //  cad = s_num.c_str();

			 //istringstream in(s_num);
			  //s_num >> valor;
			 
			 /*cad = s_num.c_str();
						   valor=atof(cad);
						   matriz[j][k] = valor;
						   k++;*/




And all of them compile fine.
But when i run it (in visual studio 2008) it doest work an the function throws an exception.


I think that there is some problem in this part of thecode, with the variable s_num(something about the exceed range)


 while(k<79)
			  {   
				  
				  i=0;
				  fichero1.get(c_num);  
				  if(c_num != char(32))
				   {
					if(c_num != char(10))
					 {
							  
				  s_num[i] = c_num;
						   i++;		
		c_num2=c_num;
						  
				while((i<7) && (c_num2 != char(32)) && (c_num2 != char(10)))
				{
							
									 fichero1.get(c_num);
									 s_num[i] = c_num;
									 i++;
																	 
								  } 

							cad=s_num;
						   
							   
						 // valor = strtod(s_num, NULL);
												   //  cad = s_num.c_str();

						   //istringstream in(s_num);
						   //s_num >> valor;
						  valor=atof((const char*)cad);


						   /*cad = s_num.c_str();
						   valor=atof(cad);
						   matriz[j][k] = valor;
						   k++;*/
					  }
					 }
			  }
		  j++; 
	}




I've tried to do it not only with a char vector , but also with strings.
But when i run it, it says something like:
Assertion failure:string subscripts out of range

This is the code , when i use a string


int leerArchivo2(char *filename)
{
	ifstream fichero1(filename); // abrir archivo para lectura
	string cadena = "";
	string s_num ="";
	char c_num = '\0';
	int i,j,k,maximo,contador_total;
	float valor,multiplicador;
	const char *cad;	
	float varianza[80];
	float sigma[80];   
	float xpcg[450],ypcg[450],zpcg[450];
		
	// verificar la apertura del archivo
	if ( fichero1.bad() ) {
	 
		return 1;
	} 

	// lectura de datos
	j = 0; 
	while ( ! fichero1.eof()) {
		  
			  k=0;			  
			  while(k<79)
			  {	 
				  s_num="";
				  c_num = '\0';

				  i=0;
				  fichero1.get(c_num);  
				  if(c_num != char(32))
				   {
					if(c_num != char(10))
					 {
							  
						   s_num[i] = c_num;
						   i++;						 
						   while(i<7)
						   {		
									 fichero1.get(c_num);
									 s_num[i] = c_num;
									 i++;
						   }		  
													  
						   cad = s_num.c_str();
						   valor=atof(cad);
						   matriz[j][k] = valor;
						   k++;
					  }
					 }
			  }
		  j++; 
	}
	maximo = j;
	
   //copia de la matriz inicial
   for(j=0;j<maximo;j++)  
	{
	   for(k=0;k<79;k++)
	   {									 
		matrizIni[j][k]=matriz[j][k];  
	   }				 
	 }  
							
	fichero1.close();




So i would like to know what is the problem with the variable "s_num"

I used to run the same function with dev c++,and it used to work fine.
Thank you for your time.

This post has been edited by jarami82: 15 April 2009 - 06:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: help with conversion from char vector to float

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: help with conversion from char vector to float

Posted 15 April 2009 - 06:30 AM

Please :code:

Note: When closing a code block --- code != /code

You are going to need to ask a specific question that you want help with or describe a specific error/problem.
Was This Post Helpful? 1

#3 jarami82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-April 09

Re: help with conversion from char vector to float

Posted 15 April 2009 - 06:58 AM

View Postjanotte, on 15 Apr, 2009 - 05:30 AM, said:

Please :code:

Note: When closing a code block --- code != /code

You are going to need to ask a specific question that you want help with or describe a specific error/problem.




I've copied and pasted, the code.
So see if you can help me.
I have to finish this program before next week, but im stock with this error
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: help with conversion from char vector to float

Posted 15 April 2009 - 07:30 AM

I really can't get a grasp on what the design and flow of your code is so I am having trouble with it.

I am really concerned about all the 'magic number' uses of 79 in the code.

What leads you to believe the strings will always be 80 characters long?

It might be safer to use length()
http://www.cplusplus.../string/length/
but since I can't see what you are trying to do it's hard to know if that is useful advice.

You also have a lot of incrementing of variables ( e.g. i ) inside while() loops that you then use to access string indexes without any defensive actions to ensure that you are not going out of bounds.
This is a dangerous practice and it seems it may have bitten you on the tender parts somewhere but I can't immediately see where because it is happening all over the place.

Sorry if this sounds like I am criticising you.
I don't mean it that way.
I just don't "get" what is happening here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1