Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:58:22+00:00 2026-06-13T12:58:22+00:00

I attempted to use this code to accept arguments from a console application. Unfortunately

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:58:23+00:00Added an answer on June 13, 2026 at 12:58 pm

    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) { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the below code is the JSON response i recieve from an API. unfortunately i
I have tried to use this code in VS2008 (and may have included too
First attempt to use this cool site - after searching for 2 hours: So
I'm attempted to use deferred and hitch in order to provide a callback for
What is the best way to pause in JavaScript. I have attempted to use
I attempt to use webservice return POCO class generated from entity data model as
I have the following sample code and noticed that if I attempt to use
The following code is from a tutorial ( http://net.tutsplus.com/php/creating-a-php5-framework-part-1/ ), not mine. I have
I am trying to use boilerpipe java library, to extract news articles from a
I follow this sample http://trac.osgeo.org/proj/wiki/ProjAPI but if I try to import this code in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.