is this the best way to FTP a file to a server using C++?
Feel like I am cheating by calling the system command.
Code #include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <cstdlib>
int main()
{
int i;
printf("Checking if processor is available...");
if (system(NULL)) puts("Ok");
else exit(EXIT_FAILURE);
printf("Executing FTP...\n");
std::string cmdstring;
cmdstring = "curl -T my-local-file.txt ftp://ftp.example.com --user user:secret ";
const char* command = cmdstring.c_str();
i = system(command);
printf("output: %d.\n", i);
return 0;
}
DO NOT DEAL WITH THIS MEMBER
I think actual FTP functions exist in c++;
What OS are you using?
Aug 28, 2020 01:27 PM
(This post was last modified: Aug 28, 2020 01:28 PM by gurlhackerclub.)
(Aug 28, 2020 01:20 PM)Lynx Wrote: I think actual FTP functions exist in c++;
What OS are you using?
Windows.
I found an old example using wininet. But I was have problems with it.
InternetOpen, InternetConnect, and !FtpPutFile were not found.
Probably something dumb, Like my compiler settings.
Code #include <wininet.h>
#include <iostream>
#include <windows.h>
#include <Wininet.h>
#include <stdlib.h>
#include <winsock.h>
#include <string.h>
#include <stdio.h>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "wininet.lib")
void FileSubmit()
{
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hInternet == NULL)
{
cout << "Error: " << GetLastError();
}
else
{
hFtpSession = InternetConnect(hInternet, "server", INTERNET_DEFAULT_FTP_PORT, "user", "pass", INTERNET_SERVICE_FTP, 0, 0);
if (hFtpSession == NULL)
{
cout << "Error: " << GetLastError();
}
else
{
if (!FtpPutFile(hFtpSession, "C://file.txt", "/file.txt", FTP_TRANSFER_TYPE_BINARY, 0))
{
cout << "Error: " << GetLastError();
}
}
}
}
DO NOT DEAL WITH THIS MEMBER
(Aug 28, 2020 01:27 PM)gurlhackerclub Wrote: Windows.
I found an old example using wininet. But I was have problems with it.
InternetOpen, InternetConnect, and !FtpPutFile were not found.
Probably something dumb, Like my compiler settings.
Code #include <wininet.h>
#include <iostream>
#include <windows.h>
#include <Wininet.h>
#include <stdlib.h>
#include <winsock.h>
#include <string.h>
#include <stdio.h>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "wininet.lib")
void FileSubmit()
{
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hInternet == NULL)
{
cout << "_error_: " << GetLastError();
}
else
{
hFtpSession = InternetConnect(hInternet, "server", INTERNET_DEFAULT_FTP_PORT, "user", "pass", INTERNET_SERVICE_FTP, 0, 0);
if (hFtpSession == NULL)
{
cout << "_error_: " << GetLastError();
}
else
{
if (!FtpPutFile(hFtpSession, "C://file.txt", "/file.txt", FTP_TRANSFER_TYPE_BINARY, 0))
{
cout << "_error_: " << GetLastError();
}
}
}
}
just google the errors man, you'll figure it out.
(Aug 28, 2020 01:27 PM)gurlhackerclub Wrote: Windows.
I found an old example using wininet. But I was have problems with it.
InternetOpen, InternetConnect, and !FtpPutFile were not found.
Probably something dumb, Like my compiler settings.
Code #include <wininet.h>
#include <iostream>
#include <windows.h>
#include <Wininet.h>
#include <stdlib.h>
#include <winsock.h>
#include <string.h>
#include <stdio.h>
using namespace std;
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "wininet.lib")
void FileSubmit()
{
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hInternet == NULL)
{
cout << "_error_: " << GetLastError();
}
else
{
hFtpSession = InternetConnect(hInternet, "server", INTERNET_DEFAULT_FTP_PORT, "user", "pass", INTERNET_SERVICE_FTP, 0, 0);
if (hFtpSession == NULL)
{
cout << "_error_: " << GetLastError();
}
else
{
if (!FtpPutFile(hFtpSession, "C://file.txt", "/file.txt", FTP_TRANSFER_TYPE_BINARY, 0))
{
cout << "_error_: " << GetLastError();
}
}
}
}
That should work, it sounds like you haven't linked the wininet.lib correctly with your compiler.
A simpler example: https://stackoverflow.com/questions/3577...ile-to-ftp
Hey there,
If I have helped you or if you just enjoy my posts, please consider donating bitcoin to me 1DpL2kcxuidEsjt2MMN2AYufcVoXbi3v2X
(Aug 28, 2020 03:40 PM)tux1 Wrote: That should work, it sounds like you haven't linked the wininet.lib correctly with your compiler.
A simpler example: https://stackoverflow.com/questions/3577...ile-to-ftp
Thanks Tux1!
|