Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

is this the best way to upload a file to a FTP server using C++?

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;
}
I think actual FTP functions exist in c++;
What OS are you using?
(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();
                }
            }
        }
    }
(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

[Image: FtwuQgr.gif]
(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!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by TerminallyIllNov 11, 2019 10:30 AM 6 281 Nov 11, 2019 10:30 AM
Last Post: HFDone
[.NET] by anonCiceroFeb 25, 2018 06:53 AM 9 269 Feb 25, 2018 06:53 AM
Last Post: Ninja
[.NET] by DaRk LoRd FlArENov 21, 2016 04:11 AM 2 196 Nov 21, 2016 04:11 AM
Last Post: DaRk LoRd FlArE