Reputation: 139
I attempted to use this code to accept arguments from a console application. Unfortunately all I get are boolean values for some weird reason. I'm not sure what's happening. I think I might be mixing namespaces or something. Anyone?
#include "stdafx.h"
using namespace System;
using namespace System::Net;
using namespace System::IO;
using namespace System::Collections;
using namespace System::Text;
void uploadFile(String^ strFileName, String^ strServerName, String^ strUserName, String^ strPassword);
void prompt();
int main(int argc, char* argv[])
{
if (argc < 9)
{
prompt();
}
else if (argc == 9)
{
char* fileName;
char* serverName;
char* userName;
char* password;
for (int i = 1; i < argc; i++)
{
if (argv[i] == "-f")
fileName = argv[i + 1];
else if (argv[i] == "-s")
serverName = argv[i + 1];
else if (argv[i] == "-u")
userName = argv[i + 1];
else if (argv[i] = "-p")
password = argv[i + 1];
else
prompt();
Console::WriteLine(argv[i]);
}
String ^strFileName = gcnew String(fileName);
String ^strServerName = gcnew String(serverName);
String ^strUserName = gcnew String(userName);
String ^strPassword = gcnew String(password);
Console::WriteLine(argv[1]);
Console::WriteLine(strFileName);
uploadFile(strFileName, strServerName, strUserName, strPassword);
}
else
{
prompt();
}
}
void uploadFile(String^ strFileName, String^ strServerName, String^ strUserName, String^ strPassword)
{
if (!strServerName->StartsWith("ftp://"))
{
String^ host = "ftp://";
host += strServerName;
strServerName = host;
}
String^ path = Directory::GetCurrentDirectory();
WebClient^ wclient = gcnew WebClient();
wclient->Credentials = gcnew NetworkCredential(strUserName, strPassword);
String^ destFileName = path;
destFileName += strFileName;
Console::WriteLine(destFileName);
Console::WriteLine(strServerName);
/*array<Byte>^response = wclient->UploadFile(strServerName, destFileName);
if (response->Length > 0)
{
ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
String^ decoded = ascii->GetString(response);
Console::WriteLine("Response: {0}", decoded);
}*/
}
void prompt()
{
Console::WriteLine("Usage: ftpCLI.exe -f <filename> -s <server> -u <username> -p <password>\n");
Console::ReadLine();
Environment::Exit(1);
}
And for anyone interested here is the program with the resolution implemented and completed.
#include "stdafx.h"
#include <string.h>
using namespace System;
using namespace System::Net;
using namespace System::IO;
using namespace System::Collections;
using namespace System::Text;
void uploadFile(String^ strFileName, String^ strServerName, String^ strUserName, String^ strPassword);
void prompt();
int main(int argc, char* argv[])
{
if (argc < 9)
{
prompt();
}
else if (argc == 9)
{
char* fileName;
char* serverName;
char* userName;
char* password;
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-f") == 0)
fileName = argv[i + 1];
else if (strcmp(argv[i], "-s") == 0)
serverName = argv[i + 1];
else if (strcmp(argv[i], "-u") == 0)
userName = argv[i + 1];
else if (strcmp(argv[i], "-p") == 0)
password = argv[i + 1];
Console::WriteLine(argv[i]);
}
String ^strFileName = gcnew String(fileName);
String ^strServerName = gcnew String(serverName);
String ^strUserName = gcnew String(userName);
String ^strPassword = gcnew String(password);
Console::WriteLine(argv[1]);
uploadFile(strFileName, strServerName, strUserName, strPassword);
}
else
{
prompt();
}
}
void uploadFile(String^ strFileName, String^ strServerName, String^ strUserName, String^ strPassword)
{
if (!strServerName->StartsWith("ftp://"))
{
String^ host = "ftp://";
host += strServerName;
strServerName = host;
}
String^ path = Directory::GetCurrentDirectory();
WebClient^ wclient = gcnew WebClient();
wclient->Credentials = gcnew NetworkCredential(strUserName, strPassword);
String^ destFileName = strServerName;
destFileName += "/" + strFileName;
array<Byte>^response = wclient->UploadFile(destFileName, strFileName);
if (response->Length > 0)
{
ASCIIEncoding^ ascii = gcnew ASCIIEncoding;
String^ decoded = ascii->GetString(response);
Console::WriteLine("Response: {0}", decoded);
}
}
void prompt()
{
Console::WriteLine("Usage: ftpCLI.exe -f <filename> -s <server> -u <username> -p <password>\n");
Console::ReadLine();
Environment::Exit(1);
}
Upvotes: 0
Views: 522
Reputation: 409166
This loop is troublesome:
for (int i = 1; i < argc; i++)
{
if (argv[i] == "-f")
fileName = argv[i + 1];
else if (argv[i] == "-s")
serverName = argv[i + 1];
else if (argv[i] == "-u")
userName = argv[i + 1];
else if (argv[i] = "-p")
password = argv[i + 1];
else
prompt();
Console::WriteLine(argv[i]);
}
Lets say that the fist argument is -f
with the filename following that. The first run through the loop you get the -f
and extract the filename. The next iteration you however argv[i]
will be the filename you extracted in the first iteration.
When you get an argument with a parameter (like -f
) you need to increment i
once extra time.
Edit: As noted by heavyd, the actual problem you have is probably the comparison of the string. You can not use the equality operator for raw C-style strings, you have to use e.g. strcmp
.
Like this:
if (strcmp(argv[i], "-f") == 0) { ... }
Upvotes: 1
Reputation: 6914
In CLR console your main should be like:
int main(array<System::String ^> ^args)
Upvotes: 1