• Lunes 29 de Abril de 2024, 06:41

Autor Tema:  ruta ftp en ++  (Leído 1576 veces)

winnipu

  • Nuevo Miembro
  • *
  • Mensajes: 9
    • Ver Perfil
ruta ftp en ++
« en: Lunes 8 de Noviembre de 2010, 12:06 »
0
Hola a todos, es mi primer mensaje y quiero plantear la siguiente duda:

Vereis estoy haciendo un programa en c++ que se conecte a un ftp y descargue un archivo firewall.log en mi pc. Pero no quiero que lo descargue en cualquier carpeta sino en por ejemplo system32. Como la ruta cambia en cada pc, y en los distintos sistemas operativos, xp, win7, vista. He pensado que la mejor manera de hacerlo con una variable de entorno como %systemroot% si bien no se como implementarla porque por mas que lo intento meter en ftpgetfile, incluso con un getenv no logro resultados. Os pego el codigo que tengo a ver si alguien puede decirme como solucionarlo:

Código: C++
  1. #include <windows.h>
  2. #include <wininet.h>
  3.  
  4. #define RARCHIVO "firewall.log"
  5. #define LARCHIVO "%SYSTEMROOT%\system32\firewall.log"
  6.  
  7. int main()
  8. {
  9.   HINTERNET hInternet, hServer;
  10.   hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  11.   hServer = InternetConnect(hInternet, "ftp.usuarios.hispa.es", INTERNET_DEFAULT_FTP_PORT, "cosas", "xxxxxxxxx", INTERNET_SERVICE_FTP, 0, 0);
  12.   FtpGetFile(hServer, RARCHIVO, LARCHIVO, (int)NULL, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
  13.   InternetCloseHandle(hInternet);
  14.   InternetCloseHandle(hServer);
  15.  
  16. return 0;
  17. }
  18.  

m0skit0

  • Miembro de PLATA
  • *****
  • Mensajes: 2337
  • Nacionalidad: ma
    • Ver Perfil
    • http://fr33kk0mpu73r.blogspot.com/
Re: ruta ftp en ++
« Respuesta #1 en: Lunes 8 de Noviembre de 2010, 13:10 »
0
La mejor forma es pasárselo como parámetro al programa. Recuerda que el prototipo de main es

Código: C++
  1. int main(int argc, char* argv[])
  2.  
En mi opinión, esto deberías programarlo con un lenguaje de scripts en vez de C++, pero sólo es una opinión.

Saludos.

winnipu

  • Nuevo Miembro
  • *
  • Mensajes: 9
    • Ver Perfil
Re: ruta ftp en ++
« Respuesta #2 en: Lunes 8 de Noviembre de 2010, 13:31 »
0
Gracias M0skit0. Si, se que mejor con scripts, pero es que estoy aprendiendo c++, y quisiera resolver esa duda en c++.
Osea que dices dejar el programa asi?

Código: C++
  1.  
  2.     #include <windows.h>
  3.     #include <wininet.h>
  4.      
  5.     #define RARCHIVO "firewall.log"
  6.     #define LARCHIVO "%SYSTEMROOT%\system32\firewall.log"
  7.    
  8.      int main(int argc, char* argv[])
  9.    {
  10.       HINTERNET hInternet, hServer;
  11.      hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  12.      hServer = InternetConnect(hInternet, "ftp.usuarios.hispa.es", INTERNET_DEFAULT_FTP_PORT, "cosas", "xxxxxxxxx", INTERNET_SERVICE_FTP, 0, 0);
  13.      FtpGetFile(hServer, RARCHIVO, LARCHIVO, (int)NULL, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
  14.      InternetCloseHandle(hInternet);
  15.      InternetCloseHandle(hServer);
  16.    
  17.      return 0;
  18.    }
  19.  

alb

  • Nuevo Miembro
  • *
  • Mensajes: 24
    • Ver Perfil
Re: ruta ftp en ++
« Respuesta #3 en: Lunes 8 de Noviembre de 2010, 13:44 »
0
Holà,
Cita de: "m0skit0"
La mejor forma es pasárselo como parámetro al programa. Recuerda que el prototipo de main es

Código: C++
  1. int main(int argc, char* argv[])
  2.  
Código: C++
  1. int main()
  2.  
y
Código: C++
  1. int main(int argc, char* argv[])
  2.  
son los dos prototipos vàlidos en C++ (ISO/IEC 14882:2003, 3.6.1 Main function)

Código: C++
  1. int main(void)
  2.  
y
Código: C++
  1. int main(int argc, char *argv[])
  2.  
son los dos prototipos vàlidos en C (ISO/IEC 9899:1999,5.1.2.2.1 Program startup)

winnipu

  • Nuevo Miembro
  • *
  • Mensajes: 9
    • Ver Perfil
Re: ruta ftp en ++
« Respuesta #4 en: Lunes 8 de Noviembre de 2010, 14:08 »
0
Os referis a que deberia ser algo asi:

Código: C++
  1.  
  2.     #include <windows.h>
  3.     #include <wininet.h>
  4.      
  5.     #define RARCHIVO "firewall.log"
  6.    
  7.    
  8.      int main(int argc, char* argv[])
  9.    {
  10.      char *pPath = getenv("SystemRoot");
  11.       HINTERNET hInternet, hServer;
  12.      hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  13.      hServer = InternetConnect(hInternet, "ftp.usuarios.hispa.es", INTERNET_DEFAULT_FTP_PORT, "cosas", "xxxxxxxxx", INTERNET_SERVICE_FTP, 0, 0);
  14.      FtpGetFile(hServer, RARCHIVO, pPath, (int)NULL, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0);
  15.      InternetCloseHandle(hInternet);
  16.      InternetCloseHandle(hServer);
  17.    
  18.      return 0;
  19.    }
  20.  

winnipu

  • Nuevo Miembro
  • *
  • Mensajes: 9
    • Ver Perfil
Re: ruta ftp en ++
« Respuesta #5 en: Martes 9 de Noviembre de 2010, 16:16 »
0
Otra opcion que se me ha ocurrido pero que no me compila ni con dev ni con codeblocks porque no encuentra la libreria urlmon.h (no se si solo funciona en visual c++), es la siguiente:


Código: C++
  1. #include <urlmon.h>
  2.  
  3. int main(){
  4. URLDownloadToFile( 0 , "http:\usuarios.hispa.esindex.html" , "%SYSTEMROOT%\system32\index.html" ,  0 , 0)
  5. return 0;
  6. }
  7.  

m0skit0

  • Miembro de PLATA
  • *****
  • Mensajes: 2337
  • Nacionalidad: ma
    • Ver Perfil
    • http://fr33kk0mpu73r.blogspot.com/
Re: ruta ftp en ++
« Respuesta #6 en: Martes 9 de Noviembre de 2010, 16:25 »
0
No, lo que yo digo es que uses argc y argv para pasar la ruta del fichero. argc contiene cuántos argumentos se están pasando, y argv es un vector (array) con estos argumentos (el nombre del programa es el primer argumento siempre).

Por tanto si llamas a tu programa desde la línea de comandos tal que

Código: Text
  1. tuprograma.exe ruta_del_fichero
  2.  
puedes hacer desde tu main:

Código: C
  1. char *pPath[256];
  2. pPath[255] = '';
  3. strncpy(pPath, argv[1], 255);
  4.  
Te aconsejo que hagas una comprobación previa de si se está pasando el argumento, porque de lo contrario obtendrás rutas raras y/o violaciones de segmento.

Código: C
  1. if (argc < 2)
  2. {
  3.     printf("No se ha indicado la ruta del ficheron");
  4.     return EXIT_FAILURE;
  5. }
  6.  
También puedes usar la clase string de C++ en vez de char*, es mucho más cómoda.

De todas formas, no deberías sobrescribir la variable de entorno %SYSTEMROOT%.