Hack Forums
[C] is this the best way to upload a file to a FTP server using C++? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] is this the best way to upload a file to a FTP server using C++? (/showthread.php?tid=6105827)



is this the best way to upload a file to a FTP server using C++? - gurlhackerclub - Aug 28, 2020

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;
}



RE: is this the best way to upload a file to a FTP server using C++? - Lynx - Aug 28, 2020

I think actual FTP functions exist in c++;
What OS are you using?


RE: is this the best way to upload a file to a FTP server using C++? - gurlhackerclub - Aug 28, 2020

(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();
                }
            }
        }
    }



RE: is this the best way to upload a file to a FTP server using C++? - Lynx - Aug 28, 2020

(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.


RE: is this the best way to upload a file to a FTP server using C++? - tux1 - Aug 28, 2020

(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/35771120/c-uploading-file-to-ftp


RE: is this the best way to upload a file to a FTP server using C++? - gurlhackerclub - Aug 28, 2020

(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/35771120/c-uploading-file-to-ftp

Thanks Tux1!