Click here to Skip to main content
15,893,381 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: copy bytes from receive buffer pointer Pin
André Kraak21-Oct-11 13:15
André Kraak21-Oct-11 13:15 
AnswerRe: copy bytes from receive buffer pointer Pin
jkirkerx21-Oct-11 13:18
professionaljkirkerx21-Oct-11 13:18 
AnswerRe: copy bytes from receive buffer pointer Pin
André Kraak21-Oct-11 13:31
André Kraak21-Oct-11 13:31 
QuestionRe: copy bytes from receive buffer pointer Pin
André Kraak21-Oct-11 13:32
André Kraak21-Oct-11 13:32 
AnswerRe: copy bytes from receive buffer pointer Pin
jkirkerx21-Oct-11 13:36
professionaljkirkerx21-Oct-11 13:36 
AnswerRe: copy bytes from receive buffer pointer Pin
André Kraak21-Oct-11 13:45
André Kraak21-Oct-11 13:45 
AnswerRe: copy bytes from receive buffer pointer Pin
Luc Pattyn21-Oct-11 16:05
sitebuilderLuc Pattyn21-Oct-11 16:05 
QuestionProblem with read in CFile class Pin
antonio34321-Oct-11 6:41
antonio34321-Oct-11 6:41 
I have two function to save/load information from/in a file.

My problem is that

for(int p=0;p<tamano_b;p++)
			{
				// Leemos del fichero los datos de la variable
				f.Read(&dato_b,sizeof(dato_b));
				datos_b[p]=dato_b;
			}

This line dont read well, only read the first date, and after read rubbish.

f.ReadString(ausente_b);

In this line read a empty CString

and so on..

Overcoat fail when read CString variable

This is my function:

int CAdestView::PegarVariables(void)
{
	//FILE *fichero;			// Puntero a un fichero
	int tamano_b;			// tamano temporal
	int x=0;				// elementos copiados
	CString nombre_b;		// nombre temporal
	double dato_b=0;		// datos temporal para introducirlo en un vector
	double *datos_b;		// vector de datos si la variable no es de texto
	CString *datostxt_b;	// vector de Cstring si la variable es de texto
	CString datotxt_b;		// datos temporal de una variable de texto
	CString ausente_b;		// dato ausente temporal
	int escala_b=0;			// escala temporal
	CString etiqueta_b;	// etiqueta temporal
	CString labels_b;		// labels temporal
	int precision_b=0;		// precision temporal
	int tipo_b=0;			// tipo temporal

	CString error;			// Cadena para mostrar error 
	INT_PTR tam_vars;
	int n = 1;				// Entero para formar el nuevo nombre de la variable
	CString aux_nombre;		// Cadena que almacena los nombres que formamos como posibles
	CString aux_nombre_lista;	// Cadena que almacena los nombres leídos del control lista
	bool salir = false;		// Nos indica si ya hemos encontrado un nuevo nombre válido
	bool encontrado = false;	// Nos indica si hemso encontrado una variable con el mismo nombre
	CStdioFile f;
	// Se obtiene un puntero de la clase view
	CAdestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// Se abre el fichero temporal para lectura
	CString path= GetUserHomeDir() + _T("\\temporal.dat");

	f.Open( path, CFile::modeReadWrite | CFile::typeBinary);

	if (f == NULL)
	{
		MessageBox (_T("fallo"));
		return 0;
	}
	// Situamos el puntero del fichero
	//fseek(fichero,sizeof(int),SEEK_SET);
	f.SeekToBegin();
	tam_vars = pDoc->Variables.GetSize ();
	// Vemos si existe algun dato a pegar, el primero es el numero
	// de datos a pegar
	// Si no hay datos devolmemos 0 y salimos
	
		if(f.Read(&x,sizeof(x))<0)
		return 0;

	// Bucle que recorre todos los elementos a pegar
	for(int h=0;h<x;h++)
	{
		// Leemos del fichero el tipo de la variable
		f.Read(&tipo_b,sizeof(tipo_b));
		// Leemos del fichero el tamaño del primer elemento pegado
		f.Read(&tamano_b,sizeof(tamano_b));
			// Dependiendo del tipo de variable rellenamos un vector u
		// otro
		if(tipo_b==0)
		{
			// Reservamos memoria para el vector de la variable
			datos_b=new double[tamano_b];
			datostxt_b = NULL;
			for(int p=0;p<tamano_b;p++)
			{
				// Leemos del fichero los datos de la variable
				f.Read(&dato_b,sizeof(dato_b));
				datos_b[p]=dato_b;
			}
		}
		else
		{
			// Reservamos memoria para los vectores de las variables
			datostxt_b=new CString[tamano_b];
		
			for(int p=0;p<tamano_b;p++)
			{
				// Leemos del fichero los datos de la variable
				f.ReadString(datotxt_b);
				datostxt_b[p]=datotxt_b;
			}
		}

		// Leemos del fichero el nombre de la variable
		
		f.ReadString(nombre_b);
			
		
		// Leemos del fichero el dato ausente de la variable
		f.ReadString(ausente_b);
		// Leemos del fichero la escala de la variable
		f.Read(&escala_b,sizeof(escala_b));
		// Leemos del fichero la etiqueta de la variable
		f.ReadString(etiqueta_b);
		// Leemos del fichero la label de la variable
		f.ReadString(labels_b);
		// Leemos del fichero la precision de la variable
		f.Read(&precision_b,sizeof(precision_b));
		// obtenemos el numero de variables del espacio de trabajo
		// Si no hay ninguna, no comprobamos si el nombre esta repetido
		if(tam_vars == 0)
		{
			// Llamamos al constructor de la clase Variables para introducir
			// los datos
			GetDocument()->Variables.Add(new CDatos(tipo_b,tamano_b,escala_b,nombre_b, datos_b,datostxt_b,etiqueta_b,labels_b, ausente_b,precision_b));
			// y los introducimos en la lista
			m_Lista.InsertItem(n+x,nombre_b);
			// Actualizamos el número de variables en el espacio
			//GetDocument()->Variables.GetSize () = n+x;
		}
		else
		{
			// Hay variables en el espacio de trabajo
			// Debo comprobar que no inserto con un nombre ya existente
			salir = false;
			// Primero vamos a ver si el nombre original de la variable no está ya presente
			for (int i = 0; i < (tam_vars +1); i++)
			{
				aux_nombre_lista = m_Lista.GetItemText (i, 0);
				if (nombre_b == aux_nombre_lista)
				{
					encontrado = true;
					break;
				}
			}
			if (!encontrado)
			{
				salir = true;
				aux_nombre = nombre_b;
			}

			// El nombre original ya existe, le creo uno nuevo
			// Escribo un nombre del tipo Var1, Var2, ...
			while (!salir)
			{
				aux_nombre.Format (TEXT("%s%d"),nombre_b, n);
				// Compruebo si ya existe en el espacio de trabajo
				for (int i = 0; i < (tam_vars+1); i++)
				{
					aux_nombre_lista = m_Lista.GetItemText (i, 0);
					if (aux_nombre_lista == aux_nombre)
					{
						encontrado = true;
						n++;
						break;
					}
				}
				if (!encontrado)
					salir = true;
				else
					encontrado = false;
			}

			// Llamamos al constructor de la clase Variables para introducir
			// los datos
			pDoc->Variables.Add (new CDatos(tipo_b,tamano_b,escala_b,aux_nombre,datos_b,datostxt_b,etiqueta_b,labels_b,ausente_b,precision_b));

			// Actualizamos el número de variables en el espacio
			//GetDocument()->Variables.GetSize () = n+x;
		}
	}		

	// Se cierra el fichero temporal	
	f.Close();
	bool borrado=FALSE;
	// Devuelvo 1 indicando que la función terminó correctamente
	return 1;
}


what happen??
AnswerRe: Problem with read in CFile class Pin
Richard MacCutchan21-Oct-11 9:26
mveRichard MacCutchan21-Oct-11 9:26 
GeneralRe: Problem with read in CFile class Pin
David Crow21-Oct-11 10:24
David Crow21-Oct-11 10:24 
GeneralRe: Problem with read in CFile class Pin
antonio34321-Oct-11 22:26
antonio34321-Oct-11 22:26 
GeneralRe: Problem with read in CFile class Pin
Richard MacCutchan22-Oct-11 0:07
mveRichard MacCutchan22-Oct-11 0:07 
GeneralRe: Problem with read in CFile class Pin
antonio34322-Oct-11 0:33
antonio34322-Oct-11 0:33 
GeneralRe: Problem with read in CFile class Pin
Richard MacCutchan22-Oct-11 0:56
mveRichard MacCutchan22-Oct-11 0:56 
GeneralRe: Problem with read in CFile class Pin
antonio34322-Oct-11 1:09
antonio34322-Oct-11 1:09 
GeneralRe: Problem with read in CFile class Pin
Richard MacCutchan22-Oct-11 1:44
mveRichard MacCutchan22-Oct-11 1:44 
GeneralRe: Problem with read in CFile class Pin
antonio34322-Oct-11 8:21
antonio34322-Oct-11 8:21 
GeneralRe: Problem with read in CFile class Pin
Chuck O'Toole22-Oct-11 9:44
Chuck O'Toole22-Oct-11 9:44 
GeneralRe: Problem with read in CFile class Pin
Richard MacCutchan22-Oct-11 21:33
mveRichard MacCutchan22-Oct-11 21:33 
GeneralRe: Problem with read in CFile class Pin
antonio34323-Oct-11 0:43
antonio34323-Oct-11 0:43 
QuestionMFC dialog + ResizableLib + splitter Pin
Dialecticus21-Oct-11 5:05
Dialecticus21-Oct-11 5:05 
AnswerRe: MFC dialog + ResizableLib + splitter Pin
Richard MacCutchan21-Oct-11 6:26
mveRichard MacCutchan21-Oct-11 6:26 
AnswerRe: MFC dialog + ResizableLib + splitter Pin
TheGreatAndPowerfulOz21-Oct-11 9:17
TheGreatAndPowerfulOz21-Oct-11 9:17 
QuestionProblem with MPR.lib/MFC90.DLL Pin
tasumisra20-Oct-11 17:41
tasumisra20-Oct-11 17:41 
AnswerRe: Problem with MPR.lib/MFC90.DLL Pin
Richard MacCutchan20-Oct-11 21:41
mveRichard MacCutchan20-Oct-11 21:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.