0

このコードを使用して、コンソール アプリケーションから引数を受け入れようとしました。残念ながら、奇妙な理由でブール値しか取得できません。何が起こっているのかわかりません。名前空間か何かを混在させているのではないかと思います。誰?

#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);
}
4

2 に答える 2

1

このループは面倒です:

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]では、最初の反復で抽出したファイル名になります。

パラメータ( など)で引数を取得すると、余分な時間を1回-fインクリメントする必要があります。i

編集: Heavyd が指摘したように、実際の問題はおそらく文字列の比較です。生のCスタイルの文字列には等値演算子を使用できません。たとえば、を使用する必要がありますstrcmp

このような:

if (strcmp(argv[i], "-f") == 0) { ... }
于 2012-10-30T13:57:31.507 に答える
1

CLR コンソールでは、メインは次のようになります。

int main(array<System::String ^> ^args)
于 2012-10-30T13:54:26.487 に答える