#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h>
#include <WinInet.h>
#pragma comment ( lib, "Wininet.lib" )
#define IsInvalidDir(dir) ( ( ( dir [ 0 ] == '.' ) && ( dir [ 1 ] == 0 ) ) || ( ( dir [ 0 ] == '.' ) && ( dir [ 1 ] == '.' ) && ( dir [ 2 ] == 0 ) ) )
using namespace std;
HINTERNET
Internet,
Ftp;
string
kelias;
void ScanDirectory ( const char *dirname, string t_path = "\0" );
bool FtpConnect ( string host, string user, string password );
bool FtpUploadFile ( const char *file );
int main ( )
{
string
hostas,
vartotojas,
slaptazodis;
ifstream
failas ( "info.txt" );
cout << "Surenkama informacija is failo 'info.txt'\n" << endl;
if ( failas.is_open ( ) )
{
while ( failas.good ( ) )
{
getline ( failas, hostas );
getline ( failas, vartotojas );
getline ( failas, slaptazodis );
getline ( failas, kelias );
break;
}
cout << ">> Hostas: " << hostas << endl;
cout << ">> Vartotojas: " << vartotojas << endl;
cout << ">> Slaptazodis: " << slaptazodis << endl << endl;
}
else
{
ofstream
failas2;
cout << "Nepavyko atverti failo." << endl;
cout << "Failas kuriamas..." << endl;
failas2.open ( "info.txt" );
failas2 << "ftp_hostas\n";
failas2 << "vartotojo_vardas\n";
failas2 << "slaptazodis\n";
failas2 << "katalogas\n";
cout << "Failas sukurtas. Surasykite reikiamus duomenis y faila ir paleiskite programa is naujo." << endl;
failas2.close ( );
system ( "PAUSE" );
return 1;
}
failas.close ( );
if ( !FtpConnect ( hostas, vartotojas, slaptazodis ) )
{
cout << "Nepavyko prisijungi prie Ftp serverio." << endl;
InternetCloseHandle ( Internet );
InternetCloseHandle ( Ftp );
system ( "PAUSE" );
return 1;
}
ScanDirectory ( "." );
system ( "PAUSE" );
return 1;
}
void ScanDirectory ( const char *dirname, string t_path )
{
DIR
*katalogas;
dirent
*katalogo_info;
if ( !IsInvalidDir ( dirname ) )
{
t_path.append ( dirname );
t_path.append ( "/" );
}
if ( t_path[0] == 0 )
katalogas = opendir ( dirname );
else
katalogas = opendir ( t_path.c_str ( ) );
while ( katalogo_info = readdir ( katalogas ) )
{
if ( katalogo_info->d_type == DT_DIR )
{
if ( !IsInvalidDir ( katalogo_info->d_name ) )
{
ScanDirectory ( katalogo_info->d_name, t_path );
}
}
else
{
}
cout << t_path << " " << dirname << "/" << katalogo_info->d_name << endl;
}
closedir ( katalogas );
size_t
pos;
pos = t_path.find ( dirname );
if ( pos != string::npos )
t_path.erase ( pos );
}
bool FtpConnect ( string host, string user, string password )
{
Internet = InternetOpen ( NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
if ( Internet == NULL )
{
InternetCloseHandle ( Internet );
cout << "infail" << endl;
return false;
}
WCHAR
whost [ 50 ],
wuser [ 50 ],
wpassword [ 50 ];
char
temp [ 50 ];
strcpy ( temp, host.c_str ( ) );
MultiByteToWideChar ( 0, 0, temp, -1, whost, 50 );
strcpy ( temp, user.c_str ( ) );
MultiByteToWideChar ( 0, 0, temp, -1, wuser, 50 );
strcpy ( temp, password.c_str ( ) );
MultiByteToWideChar ( 0, 0, temp, -1, wpassword, 50 );
Ftp = InternetConnect ( Internet, whost, INTERNET_DEFAULT_FTP_PORT, wuser, wpassword, INTERNET_SERVICE_FTP, 0, 0 );
if ( Ftp == NULL )
{
InternetCloseHandle ( Internet );
InternetCloseHandle ( Ftp );
cout << whost << " " << wuser << " " << wpassword << endl;
return false;
}
return true;
}
bool FtpUploadFile ( const char *file )
{
char
dest [ 200 ],
ckelias [ 50 ];
strcpy ( ckelias, kelias.c_str ( ) );
sprintf ( dest, "%s/%s", ckelias, file );
WCHAR
wfile [ 50 ],
wkelias [ 200 ];
MultiByteToWideChar ( 0, 0, file, -1, wfile, 100 );
MultiByteToWideChar ( 0, 0, dest, -1, wkelias, 200 );
if ( !FtpPutFile ( Ftp, ( LPCWSTR )wfile, ( LPCWSTR )wkelias, FTP_TRANSFER_TYPE_BINARY, 0 ) )
{
return false;
}
return true;
}
Function ScanDirectory gets all files in current directory, and subdirectories, but if there is "CurrentDir/SecondDir/ThirdDir", program doesn't reach ThirdDir. How can I solve this?

New Topic/Question
Reply



MultiQuote





|