1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#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;
}
|