Skip to content
MPGHThe Dark Arts
/
RegisterLog in
Forum
Community
What's NewLatest posts across the boardTrendingHottest threads right nowSubscribedThreads you follow
Discussion
GeneralIntroductionsEntertainmentDebate FortFlaming & Rage
Board
News & AnnouncementsMPGH TimesSuggestions & HelpGiveaways
More Sections
Art & Graphic DesignProgrammingHackingCryptocurrency
Hacks & Cheats
Games
ValorantCS2 / CS:GOCall of Duty / WarzoneFortniteApex LegendsEscape From Tarkov
+14 moreLeague of LegendsGTA VMinecraftRustROTMGBattlefieldTroveBattleOnCombat ArmsCrossFireBlackshotRuneScapeDayZDead by Daylight
Resources
Game Hacking TutorialsReverse EngineeringGeneral Game HackingAnti-CheatConsole Game Hacking
Tools
Game Hacking ToolsTrainers & CheatsHack/Release NewsNew
Submit a release →Share your cheat, tool, or config with the community.
AINEW
AI Tools
General & DiscussionPrompt EngineeringLLM JailbreaksHotAI Agents & AutomationLocal / Open Models
AI × Gaming
AI Aimbots & VisionML Anti-CheatGame Bots & Automation
Create
AI Coding / Vibe CodingAI Art & MediaAI Voice & TTS
The AI frontier →Where game hacking meets modern machine learning. Jump in.
Marketplace
Buy & Sell
SellingBuyingTradingUser Services
Trust & Safety
Middleman LoungeMarketplace TalkVouch Copy Profiles
Money
Cryptocurrency TalkCurrency ExchangeWork & Job Offers
Start selling →List accounts, services, and goods. Use the middleman to trade safe.
MPGH The Dark Arts

A community for offensive security research, reverse engineering, and AI.

Community

ForumMarketplaceSearch

Account

RegisterLog in

Legal

Privacy PolicyForum RulesHelp & FAQ
© 2026 MPGH · All rights reserved.Built by the community, for the community. For educational purposes onlyContent is shared for security research and education — we don't condone illegal use. You're responsible for complying with applicable laws. Use at your own risk.
Home › Forum › Programming › C++/C Programming › [Tutorial] Reading from the CMD line

[Tutorial] Reading from the CMD line

Posts 1–8 of 8 · Page 1 of 1
SH
shercipher
[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.
#1 · 20y ago
RC
RCEisForMe
*
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.
#2 · 20y ago
SH
shercipher
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).
#3 · 20y ago
RC
RCEisForMe
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.
#4 · 20y ago
[G
[gen]
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.
#5 · edited 20y ago · 20y ago
RC
RCEisForMe
umm, thanks for reiterating.
#6 · 20y ago
iverson954360
[MPGH]iverson954360
hey rceisforme... mpgh was down for pretty long so they set it back to and earilier time b4 it went down
#7 · 20y ago
BA
BaGGy
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.
#8 · 20y ago
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

  • Reading from an INI fileBy Credzis in C++/C Programming
    0Last post 18y ago
  • Reading from a memory addressBy isaacboy in Visual Basic Programming
    0Last post 17y ago
  • [Tutorial] - Reading Text & *Extra*By guza44_44 in Visual Basic Programming
    3Last post 16y ago
  • [Request] Reading From WebPageBy CoderNever in C++/C Programming
    4Last post 16y ago
  • [TUTORIAL] Calling from a .dll using Java and JNIBy kronus980 in Java
    0Last post 16y ago

Tags for this Thread

None