Hi,
I'm triying to copy some information to clipboard but I have a problem when I want open the file "temporal.dat". The aplication never open the file. I have other application with the same code and everything run perfectly. Could you help me?
This is my code
Code:void CAdestView::CopiarVariables()
{
int sel=-1; // Nos indicara el elemento que esta seleccionado
CString aux_nomb; // Almacena el nombre para comparar
FILE *fichero; // Nombre del fichero temporal
int x=0; // Numero de elementos copiados
CString nombre_b; // Nombre temporal de la variable para copiar en el fichero
CString ausente_b; // Valor ausente temporal de la variable para copiar en el fichero
CString etiqueta_b; // Etiqueta temporal de la variable para copiar en el fichero
CString labels_b; // Labels char labels_b[30]; //
CString datostxt;
int t=0;
// Se abre el fichero para escritura
fichero= fopen("\\temporal.dat","wb");
// Se obtiene un puntero de la clase view
CAdestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(!fichero)
{
MessageBox(_T("No se pudo abrir el fichero"), _T("Error"), 0x10010);
return;
}
// Bucle para determinar el numero de elementos a copiar
do
{
sel = m_Lista.GetNextItem(sel,LVNI_SELECTED);
aux_nomb=m_Lista.GetItemText(sel,0);
if (sel!=-1)
x++;
}while (sel != -1);
// Abro el portapapeles
if(!OpenClipboard())
{
MessageBox(_T("No se pudo abrir el portapapeles"), _T("Error"), 0x10010);
return;
}
// Registro el formato Datos Adest
UINT cf= RegisterClipboardFormat(_T("Variables Adest"));
const char *text3 = "";
HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(text3)+1);
char *ptr = (char *)GlobalLock(hText);
strcpy(ptr, text3);
GlobalUnlock(hText);
SetClipboardData(cf, hText);
// Cierro el portapapeles
CloseClipboard();
// Se situa el puntero del fichero donde apunta el cursor
fseek(fichero,sizeof(int),SEEK_SET);
// Se escribe el numero de elementos a copiar
fwrite(&x,sizeof(int),1,fichero);
INT_PTR contador=0;
contador=pDoc->Variables.GetSize();
// Bucle para copiar todos los elementos seleccionados
do
{
sel = m_Lista.GetNextItem(sel,LVNI_SELECTED);
aux_nomb = m_Lista.GetItemText(sel,0);
for (int i=0;i<contador;i++)
{
CDatos* pDat = (CDatos*)pDoc->Variables.GetAt(i);
// El nombre debe coincidir
if (pDat->Nombre == aux_nomb)
{
// Se copia el nombre a la variable temporal del fichero
nombre_b=pDat->Nombre;
// Se escribe en el fichero el tamaƱo
fwrite(&pDat->Tam,sizeof(int),1,fichero);
// Se escribe en el fichero el nombre
fwrite(&nombre_b,sizeof(CString)*30,1,fichero);
// Se escribe en el fichero el tipo
fwrite(&pDat->Tipo,sizeof(int),1,fichero);
// Dependiendo del tipo de variable se escribe en el
// fichero unos u otros datos
if(pDat->Tipo==0)
{
for(t=0;t<pDat->Tam;t++)
//Se escriben los datos en el fichero
fwrite(&pDat->Datos[t],sizeof(double),1,fichero);
}
else
{
for(t=0;t<pDat->Tam;t++)
{
//Se escriben los datos en el fichero
datostxt=pDat->Dat_Txt[t];
fwrite(&datostxt,sizeof(CString)*20,1,fichero);
}
}
// Se copia el dato ausente a la variable temporal del fichero
ausente_b=pDat->Ausente;
// Se escribe en el fichero el dato ausente
fwrite(&ausente_b,sizeof(CString)*10,1,fichero);
// Se escribe en el fichero la escala
fwrite(&pDat->Escala,sizeof(int),1,fichero);
// Se copia la etiqueta a la variable temporal del fichero
etiqueta_b=pDat->Etiqueta;
// Se escribe en el fichero la etiqueta temporal
fwrite(&etiqueta_b,sizeof(CString)*100,1,fichero);
// Se copia las labels a la variable temporal del fichero
labels_b=pDat->Labels;
// Se escribe en el fichero las labels temporal
fwrite(&labels_b,sizeof(CString)*80,1,fichero);
// Se escribe en el fichero la precision
fwrite(&pDat->Precision ,sizeof(int),1,fichero);
}
}
}while (sel != -1);
// Se cierra el fichero
fclose(fichero);
}

