Two questions; how do command line inputs work and how do I start working in windows applications?

This probably sounds incredibly stupid, but how far along woudl I need to be to start being able to put code, display graphics and text and such in the windows application option for a new project in my compiler? When I open that option up it has tons of code already there to, I assume, actually make the window appear and lay the ground work. There are annotations in there which are pretty straight forward but how adept at the language do I have to be to start fiddling with that stuff and are there are good guidelines as to how to get started?

Also, can anybody tell me how giving command line instructions makes sense?

 
int main ( int argc, char *argv[] )


Apparently implies a whole lot more than I realize...isn't doing this simply declaring two variables, one an integer with no set value yet and another variable that is an array for chars pointing to another variable? Both of these with a scope only for the main function?

How is it then, that the argc is apparently always greater than 1 and what is argv pointing to and where as no other code is present? Sorry if this seems stupid I just have no idea where this stuff is coming from :s.

Thanks for any and all help :)!
Last edited on
There's no right answer to this because it depends what you want and how comfortable you are. You need to have a basic grasp of C++, sure. However, like anything else, GUI libraries need a bit or learning too. Knowing how to program well in a terminal doesn't mean than you'll automatically be able to program GUIs well.

If you're making some sort of form based application, you need a GUI framework of some sort. Qt seems to be the choice of many, but you can also use the native Windows API.

If you feel comfortable, just pick one of those up and start tinkering. It's often the best way to gauge how difficult you're going to find it.

As for command line arguments, they're pretty simple. The first, typically argc, stores the number of arguments passed in. This is always at least one, as your program name is passed implicitly as the first argument. The second, argv, is an array of string holding those passed in arguments.

I could for example, check if a certain number of arguments has been passed in and, if so, store one in a variable. (There are obviously better uses of CL arguments than this - it's purely for example).

args.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main( int argc, char* argv[] )
{
    std::string my_arg;

    // First argument is always the name of your program
    std::cout << argv[0] << std::endl;

    if( argc == 2 )
    {
        // Something has been passed in
        my_arg = argv[1];
        std::cout << "Argument passed in is " << my_arg << std::endl;
    }

    return 0;
}


I compile my program, calling it 'args'
g++ args.cpp -o args


Then run it. First with no arguments, then with one; foo.
> args
args

> args foo
args
Argument passed in is foo
Last edited on
Thanks for the quick reply :D! How would you recommend I start tinkering with the Windows API? What is it, exactly, anyway? I looked into it and it lists a lot of stuff that sounds familiar to me when I took a course in Visual Basic. I could drag n' drop objects onto the pane but I assume the functionality for C++ is incredibly different or is it O_o? Stuff like buttons, combo boxes, etc...

And two, I still can't seem to comprehend one thing; where does argv get the file name from, and how do I pass other file types into it? Unless including argc and *argv[] is some kind of pre-built thing that automatically assigns that but why does it do so, or is that too complicated to explain XD fine if it is I'm just curious if it isn't, because I assumed argv was a pointer. Also how can argv be a char I thought char was used to store single values of the 256 pre-determined ASCII characters? How would an array with the char datatype be able to hold a file name?

Also, how would I store another file type in argv[x]? I assume it's a little more complicated than just saying *argv[x] = <filename>, right?

Sorry if these questions are weird O_o
Windows API is exactly that - an application programming interface for Windows. It has all of the necessary components to make Windows applications. There are form builders and I think Visual Studio C++ has one.

http://msdn.microsoft.com/en-us/library/bb384845(v=vs.100).aspx

This is probably a good place to try if you want to go that route. Bear in mind, you're working with a Windows specific API there. If you want cross-platform development, you'd need an alternative framework.

Look closely again at the code for the argv variable. It's not a char, it's an array of C-strings (you could write it as char **argv if you prefer). They are picked up from whatever you pass in when you run the program from the command line (or, in some IDEs, you can set command line argument through some build settings).

So, if I had:
> args hello world


Then argc would be three. Remember, the first argument is always the program name. The second and third would be "hello" and "world" respectively. And since we start are indices at zero, those three arguments are stored in argv[0], argv[1] and argv[2].

In essence, that's all they are - strings. You can pass whatever file type you like, provided you've coded the adequate solution to read it in your program.

Here's a basic example. Let's assume we have a text file, my_file.txt, in the same directory as our program. It looks like this:

my_file.txt
Line One
Line Two
Line Three


And then we have our program. We're going to take the name of the file from the argument passed in and, providing we can open it, print its contents to the screen.

args.cpp
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 <string>
#include <fstream>

int main( int argc, char* argv[] )
{
    std::string line;

    if( argc < 2 )
    {
        std::cout << "No argument passed to program\n";
        std::cout << "Usage: " << argv[0] << " <filename>\n";
        return 0;
    }

    std::ifstream is( argv[1] );

    if( is.is_open() )
    {
        while( getline( is, line ) )
            std::cout << line << std::endl;

        is.close();
    }
    else
        std::cout << "Could not open file " << argv[1] << std::endl;

    return 0;
}


So, if I compile the program, name it 'args' and run it.
> args my_file.txt
Line One
Line Two
Line Three


Does that clear anything up?
Last edited on
Win32 is an application programming interface, a set of functions and data structures that helps you to create a graphical user interface

Win32 is harder than visual basic since it's procedural, you have no classes to work with, you have to implement by text how you will create a window, or what clicking a button does in a low level way, etc...

As a clarification, Win32 is the implementation of Windows API for 32-bit platforms. I want to little bit more strongly here implicate after all the talk of Windows API that it is a very bad choice for you. It is written in C, so really low level API and that's why really hard to use: just do not do this. For example Qt is multi-platform, actively developed ( written in C++ ) with much better library design and yet with more functionality than you will need.
WOW Thanks for all the information :D I really appreciate this much help, you guys are awesome. Thanks for explaining about the windows API and Qt and about it's limitations I am going to download Qt now and start playing around so thanks a ton :) thanks for the tip on how it works differently from what I did before, I assume the code however would be similar as it was in VB? Properties like on_click or such?

Also, thanks for the extrapolation on command lines, if I could, could anybody give me a specific example or explanation of how I would pass a different file name into a different part of the array of argv? I tried argv[1] = "(F:/Test/Project1.exe)" for example, and it seemingly didn't increment argc nor open the file...should I try this with a .txt file? Just an example, really, where this was shown would be incredibly helpful so I can link everything up in my head :X

Thanks again for all the help!
Eek, I seem to have run into a problem trying to figure out how to install and run Qt...I got the 30 month free trial, but I can't seem to set up a compiler to work with it despite me having used VS Express 2013 for desktops to compile my C++ prior. I see the compiler listed under tools->options->compilers, so I have no idea why it wouldn't work =\

I tried looking for answers but I can't make heads or tails. Places are telling me to DL mingw, which I did, and got an archive which I extracted and have no clue what to do with, and then there's something about getting personal build directories from another source because the 64 bit version of mingw doesn't come with them...

Basically if anybody could simplify this problem I'd be extremely appreciative DX
Anybody >.<?
Sorry, I was away (I only really tend to knock about the forums between quiet periods at work!).

I don't know about the Qt issue as I've not really used it aside from dabbling with the odd small program. Even then, that was probably about two years ago.

As for the arguments, passing a filename in shouldn't differ based on the path or type; it's just a string at the end of the day.

So, if you want argv[1] to be the path above, you'd run your program like so:
> args "F:/Test/Project1.exe"


Maybe this program might clear things up a bit:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main( int argc, char* argv[] )
{

    for( int i=0; i < argc; ++i )
        std::cout << "Arg " << i << " is " << argv[i] << std::endl;
    
    return 0;
}


Try running it pass different things in.

For example:
> args my_argument "F:/SomeFile.exe" 12345


This would produce:
Arg 0 is args
Arg 1 is my_argument
Arg 2 is F:/SomeFile.exe
Arg 3 is 12345


The think of it in the same way as any other function. When you pass arguments, it's a means of getting variables into the scope of that subroutine. It's much the same here, only you're passing variables from the command line for use in your program.

Topic archived. No new replies allowed.