Beginner Trouble

I have an IP address stored in "example" and I cannot use it in the following, "HINTERNET hFtpSession = InternetConnect(hInternet, example, INTERNET_DEFAULT_FTP_PORT, L"test", L"test", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);"

My code is as follows:

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
#include <string>
#include <ws2tcpip.h>
#pragma comment(lib,"wininet.lib") /*This links wininet lib only works on Visual Studio VC++ */
#include<wininet.h>
#include <fstream>

int main() {

std::ifstream f("c:\\Windows\\Temp\\Test.txt");
std::string t;

	for (int i = 1; i <= 15; i++)
		std::getline(f, t);

	char* example = new char[t.size() + 1];
	std::copy(t.begin(), t.end(), example);
	example[t.size()] = '\0';

	printf("\n");
	printf(example);

	HINTERNET hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
	HINTERNET hFtpSession = InternetConnect(hInternet, example, INTERNET_DEFAULT_FTP_PORT, L"test", L"test", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
	FtpPutFile(hFtpSession, L"C:/test.txt", L"/test.txt", FTP_TRANSFER_TYPE_BINARY, 0);
	printf("File Uploaded!");
	InternetCloseHandle(hFtpSession);
	InternetCloseHandle(hInternet);

}


Sorry for the vague description, I am still learning. Any help would be greatly appreciated!

Many Thanks,

Googie.
Last edited on
I can replace the "example" in my script with L"127.0.0.1" and I can successfully upload the test.txt file to the FTP Server.
Last edited on
You might want to set your IDE to non-unicode mode and stop using the L"hello" string literals. Those all use wchar_t characters instead of char, which I believe are usually 16 bits on windows(?).

On the other hand, you can convert a regular string with chars to one with wchar_ts

1
2
3
4
5
6
7
wchar_t* example = new wchar_t[t.size() + 1];

size_t convertedChars = 0;
mbstowcs_s(&convertedChars, example, t.size() + 1, t.c_str(), _TRUNCATE);

// Display the string.
wcout << wcstring << '\n';


https://docs.microsoft.com/en-us/windows/win32/learnwin32/working-with-strings
Last edited on
Your code looks like you copied it from different sources.
It's not a good idea to mix C and C++, ascii chars with widechars.

Try this:
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
#include <iostream>
#include <fstream>
#include <string>
#include <ws2tcpip.h>
#include<wininet.h>

#pragma comment(lib,"wininet.lib") 

int main()
{
  std::ifstream f("c:\\Windows\\Temp\\Test.txt");
  std::string ip;

  for (int i = 1; i <= 15; i++)
    std::getline(f, ip);

  std::cout << ip << '\n';
  
  HINTERNET hInternet = InternetOpenA(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  HINTERNET hFtpSession = InternetConnectA(hInternet, ip.c_str(),  
                                          INTERNET_DEFAULT_FTP_PORT,  "test", "test",   
                                          INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
  FtpPutFileA(hFtpSession, "C:/test.txt", "/test.txt", FTP_TRANSFER_TYPE_BINARY, 0);
  std::cout << "\nFile Uploaded!\n";
  InternetCloseHandle(hFtpSession);
  InternetCloseHandle(hInternet);

}
This still dosnt work... :(
What doesn't work?
Any error messages?
Any output?
Topic archived. No new replies allowed.