CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Aug 2011
    Posts
    69

    Copy to clipboard

    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);	
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Copy to clipboard

    Quote Originally Posted by a343 View Post
    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.
    ...
    This is my code

    Code:
    void CAdestView::CopiarVariables()
    {
    	...
    	// Se abre el fichero para escritura
    	fichero= fopen("\\temporal.dat","wb");
    	...
    	// Se cierra el fichero
    	fclose(fichero);	
    }
    You write you "want to open the file "temporal.dat". But in your code you are trying to open the "\\temporal.dat" file!
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2011
    Posts
    69

    Re: Copy to clipboard

    I meant "\\temporal.dat". I'm sorry.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Copy to clipboard

    Quote Originally Posted by a343 View Post
    I meant "\\temporal.dat". I'm sorry.
    But does the file \temporal.dat exist?
    Try to open (just copy/paste this name) \temporal.dat in Windows explorer or in cmd console...
    Victor Nijegorodov

  5. #5
    Join Date
    Aug 2011
    Posts
    69

    Re: Copy to clipboard

    I can't find the file how "\\temporal.dat" or "\temporal.dat" but I look in windows explorer how "temporal.dat" and I find the file in C:\Users\Antonio\AppData\Local\VirtualStore

    But in other aplicattion I have the same code with "\\temporal.dat" and everything run perfectly.
    What happen?
    How I must to edit my code?
    Last edited by a343; August 31st, 2011 at 05:17 AM.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Copy to clipboard

    Why are you putting the slash in front of the file name?

  7. #7
    Join Date
    Aug 2011
    Posts
    69

    Re: Copy to clipboard

    Becouse is the direction of this file. That mean that the file is in the unit C: or the unit where is the windows file

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Copy to clipboard

    Are you sure it is correct?
    Where did you read any info about such a meaning of backslash?
    Victor Nijegorodov

  9. #9
    Join Date
    Aug 2011
    Posts
    69

    Re: Copy to clipboard

    I'm not sure, I had this code from other person who did my project before.
    If you know other way to do this function, tell me

    Maybe it is not like this, but I saw in this web something like that

    http://copstone.com/2009/12/abrir-ap...ernas-desde-c/
    Last edited by a343; August 31st, 2011 at 11:26 AM.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Copy to clipboard

    Putting a (forward) / before a path indicates it is an absolute path on Linux/Unix. I don't know whether or not \ has a similar meaning on Windows.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Copy to clipboard

    Quote Originally Posted by a343;2031377Maybe it is not like this, but I saw in this web something like that

    [url
    http://copstone.com/2009/12/abrir-aplicaciones-externas-desde-c/[/url]
    I don't see anything similar to the file name beginning with backslash in the site you referred to.
    If you see - please, post the quote.
    Victor Nijegorodov

  12. #12
    Join Date
    Aug 2011
    Posts
    69

    Re: Copy to clipboard

    Now, I put my path, where is the file, "C:\\Users\Antonio\AppData\Local\VirtualStore", but it can't open the file. Why???

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Copy to clipboard

    Quote Originally Posted by a343 View Post
    Now, I put my path, where is the file, "C:\\Users\Antonio\AppData\Local\VirtualStore", but it can't open the file. Why???
    This path is wrong.
    Please, show the exact code snippet where you pass in the path and file name to open the file.
    Victor Nijegorodov

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Copy to clipboard

    Quote Originally Posted by a343 View Post
    Now, I put my path, where is the file, "C:\\Users\Antonio\AppData\Local\VirtualStore", but it can't open the file. Why???
    How would we know?? From MSDN "Always check the return value to see if the pointer is NULL before performing any further operations on the file. If an error occurs, the global variableerrno is set and may be used to get specific error information. For further information, see errno."

    I think Victor is alluding to the possibility that you're not using escape characters correctly in your string literal.

    What does this problem have to do with your thread title?

  15. #15
    Join Date
    Aug 2011
    Posts
    69

    Re: Copy to clipboard

    Please read the code of the function, it copies informations to clipboard.

    Victor, you are right. But my aplication will run in several computer, and I need some way to put the path in general way not only my path

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured