Results 1 to 8 of 8
  1. #1
    shercipher's Avatar
    Join Date
    Dec 2005
    Posts
    108
    Reputation
    10
    Thanks
    14

    [Tutorial] Reading from the CMD line

    This comes in two nice little packages. C++ and C.

    C++:
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    //this is a variable to hold what the person typed
    char mystring[255];
    cout<<"Tell me something important: ";
    cin>>mystring;
    cout<<"You said "<<mystring<<"!"<<endl;
    return 0;
    }
    C:
    Code:
    #include <stdio.h>
    
    int main() {
    /*this is the variable holding what the person typed*/
    char mystring[255];
    printf("Tell me something important: ");
    scanf("%s", &mystring); /*no, that ampersand is not an accident*/
    printf("You said %s!\n");
    return 0;
    }
    EXPLAINATION -

    C++:
    First, I included the iostream file, which provides the basic set of input and output instructions for C++.
    Then I included the namespace, std. Think of C++ as a binder. When you open this binder, you go to the "iostream" tab, and then to the "std" tab under "iostream". std contains the instructions for manipulating i/o streams.

    I then initiate function main, the entry point of every program. It is an integer function, which means when it terminates, it returns an integer value to whatever called it (in this case DOS).
    The variable "mystring" is created. Its type is character, and it is an array of 256 cells. That means that for every cell in this array, the type is character, and therefore when I have a bunch of characters lined up in an array, I form a string.
    I then use the cout stream and shift the bytes (<<) to instruct it to output my question to the user's command prompt.
    * I then use the cin stream, which is the input stream, and shift the bytes in the other direction (>>) indicating that I want the user to input something. The program will wait for the user to type something in and press enter, and will store whatever the user typed into mystring.
    Then the cout stream again is shifted to tell the user what they input. Whatever happened to be input into the mystring variable is reguritated to the user.
    I return 0, meaning that whatever program that handled this program (DOS) will be told "0". DOS doesn't do anything with this number, but it is good practice to include it.
    I end the function main.

    C:
    I include the stdio.h file, which includes all the functions needed for input and output via the command line in C.
    I initiate function main, which is the entry point of every program (all programs begin reading instructions at main).
    Just like in the C++ program, I create an array of type character, 256 cells large (computers count from 0...so 255 is 255 counting from 1, what you are used to, but to the computer, 255 is 256 counting from 0).
    When characters are placed into this array in order, they form a string, or a sentence, which means its a bunch of characters in order.
    I use the output function printf() to ask the user for some input.
    * I use the function scanf, which accepts two arguements.

    THIS IS A REAL STICKY POINT - WHY DOES SCANF ACT THE WAY IT DOES?
    scanf stands for scan formatted. It is used to read FORMATTED input, which means input written in a certain way.
    When I call scanf, I must first tell it HOW I want it to interpret input, then where to store the input. The ampersand is important, because it is a pointer to the location of your variable - you can't just pass the variable directly.
    So when I say
    Code:
    scanf("%s", &mystring);
    I am really telling the computer that I want to read a raw string (%s stands for string) and store it at the memory location (&) of mystring.
    What is formatting, you might say? Consider this...
    Code:
    scanf("FORMAT: %d %d %d %c", &myint1, &myint2, &myint3, &mychar1);
    When you respond to this program, you must type into the command line (where 99 represents any number and c any character) exactly:

    "FORMAT: 99 99 99 c"

    And it will be read into the appropriate variables. It can easily be seen why this is useful (reading formatted files, for example).

    In this case, I told scanf that I expect a string, and I want it to be stored in mystring.

    Then I tell the computer to output a message using printf() again. Note the %s. You guessed it, %s, %d (a number), and %c (a single character) are shared between both scanf and printf. Note there is no & in front of my variable this time. This is because I am talking about the variable, not its address (in memory). The second thing I passed to the function was what variable I wanted printf() to replace the %s opcode with. Finally, \n is an escape sequence which means "newline", or simulate the pressing of enter on a computer (move down a line).

    Finally, I return 0 to the handling program, and end the function main.

    I know this is hard to understand...I'm not so good at explaining these concepts. If you didn't understand this, you should read some much better tutorials you can find floating on google and if you don't understand those, use this as a supplement.

    It would be nice if someone rewrote this in a more -- newbie friendly way.

  2. #2
    RCEisForMe's Avatar
    Join Date
    Feb 2006
    Posts
    39
    Reputation
    9
    Thanks
    0

    *

    That's a nice tutorial, shercipher.
    I always thought of the command prompt (cmd.exe in winnt derivatives) as the command-line, and reading input from that was reading command-line arguments. Then again, I've never done any professional programming.
    (Then again, it seems that like this guy and this guy have the same idea as I.)
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
    	while(argc>0)
    			{
    				argc--;
    			 	printf("%s\n", *argv++);
    			}
    	return 0;
    }
    The above code reads all command line arguments and prints them to the screen (including the filename of the EXE.) Notice how the commands are accessed (via a pointer to a string) and see how if you change the '0' in "while (argc>0)" to a '-10' your program (may) spew out some random crap (not much in all likeliness) and then crash:
    Code:
    C:\FUBAR>argcv.exe hellofdfd sdsd
    argcv.exe
    hellofdfd
    sdsd
    (null)
    &#175; &#175;**☻►
    Actually I'm a little suprised it even got the chance to spit out that random string before windows shot it down.

    Anyhow, that's just my take on it.

  3. #3
    shercipher's Avatar
    Join Date
    Dec 2005
    Posts
    108
    Reputation
    10
    Thanks
    14
    Lol.

    You are accessing memory space that doesn't belong to your program. Windows generally doesn't shoot programs down for that (they fux0r up themselves most of the time).

  4. #4
    RCEisForMe's Avatar
    Join Date
    Feb 2006
    Posts
    39
    Reputation
    9
    Thanks
    0
    Why have so many of my posts been deleted? Did MPGH restore from an old backup or something?
    Whatever the case, I basically said that this caused a paging/segmentation fault as the program tries to access memory outside of it's allocated space (for example, it may try and access 0xABABABAB), and since the program hasn't got an exception handler for it it gets terminated.
    This is generally why most programs crash.
    Originally I cited a few MS KnowlegeBase articles, but I can't be bothered right now.

  5. #5
    [gen]'s Avatar
    Join Date
    Jan 2006
    Posts
    11
    Reputation
    10
    Thanks
    0
    Actually, shericifer is confused about the meaning of the term 'reading input from commandline', what hes deminstrating is reading just input from the console.. the commandline arguments are passed in a program usually when you want the user to input some data that will help determine the flow of the program.

    thats the use of the int main( int argc, char **argv).
    you can get integer input,or a char input, usually the char input youd want to hold in a char array to hold the data// and something like if(argv[0] == "i") you put i as a commandline argument.. and the way its used by the program is from CMD. Say your program is located in C:\blah.exe
    You would go "C:\blah.exe i" <--The program will look for this commandline argument, if of course your program handles any.
    Last edited by [gen]; 03-27-2006 at 05:31 PM.

  6. #6
    RCEisForMe's Avatar
    Join Date
    Feb 2006
    Posts
    39
    Reputation
    9
    Thanks
    0
    umm, thanks for reiterating.

  7. #7
    iverson954360's Avatar
    Join Date
    Jan 2006
    Gender
    male
    Posts
    10,280
    Reputation
    940
    Thanks
    1,656
    My Mood
    Dead
    hey rceisforme... mpgh was down for pretty long so they set it back to and earilier time b4 it went down
    Hip Hop thread Part 1 (No Longer Updated): https://www.mpgh.net/forum/316-music-...-hop-list.html
    Part 2 (No Longer Updated): https://www.mpgh.net/forum/2-general/...ad-pt-2-a.html
    Part 3: COMING SOON











  8. #8
    BaGGy's Avatar
    Join Date
    Dec 2005
    Gender
    female
    Posts
    14
    Reputation
    10
    Thanks
    0
    I am sorry this tutorial is a 1.5/5 in my opinion.

    You really don't explain return types very well, and well the way you explained namespacing was very misleading since you can do a lot more with it than what you said. And yes you do claim to be very poor at explaining things, as I have that problem as well. But I really suggest that to those reading this tutorial, to not use it as any resource of information, the only thing that really is somewhat useful is the code.

    Other than that you would be better off taking a class, or buying a book on the subject.

Similar Threads

  1. Replies: 0
    Last Post: 04-23-2010, 03:39 PM
  2. [Request] Reading From WebPage
    By CoderNever in forum C++/C Programming
    Replies: 4
    Last Post: 03-25-2010, 12:42 AM
  3. [Tutorial] - Reading Text & *Extra*
    By guza44_44 in forum Visual Basic Programming
    Replies: 3
    Last Post: 11-27-2009, 12:21 AM
  4. Reading from a memory address
    By isaacboy in forum Visual Basic Programming
    Replies: 0
    Last Post: 03-26-2009, 03:28 AM
  5. Reading from an INI file
    By Credzis in forum C++/C Programming
    Replies: 0
    Last Post: 11-28-2007, 02:18 PM