[英]Accepting arguments in a console application with managed code in C++
我試圖使用此代碼接受來自控制台應用程序的參數。 不幸的是,我得到的是一些奇怪的原因的布爾值。 我不確定發生了什么。 我想我可能會混合命名空間或其他東西。 任何人?
#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);
}
對於任何有興趣的人來說,該計划的實施和完成決議。
#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);
}
在CLR控制台中,您的主要應該是:
int main(array<System::String ^> ^args)
這個循環很麻煩:
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]);
}
讓我們說第一個參數是-f
,后面跟着文件名。 第一次循環運行你得到-f
並提取文件名。 然而, argv[i]
的下一次迭代將是您在第一次迭代中提取的文件名。
當你得到一個參數與參數(如-f
),你需要增加i
一次額外的時間。
編輯:正如heavyd所說,你遇到的實際問題可能是字符串的比較。 您不能對原始C樣式字符串使用相等運算符,您必須使用例如strcmp
。
像這樣:
if (strcmp(argv[i], "-f") == 0) { ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.