I was reading the command line argument tutorial and I realized that I didn't know what command line arguments are. Can someone quickly explain that to me. And if possible, post an example so I can more easily understand it.
Thanks in advance!
Printable View
I was reading the command line argument tutorial and I realized that I didn't know what command line arguments are. Can someone quickly explain that to me. And if possible, post an example so I can more easily understand it.
Thanks in advance!
If you run your program from a shell (in your case I am guessing a dos prompt is the easyest way to go) the command line arguments are what comes are the programms name followed by whatever you typed after its name
Compile this normally and name the resulting program lc.exe. Open a dos prompt and go to the directory where lc was created. type lc afile.txt and assuming afile.txt is a file in the current directory this will count the lines in that file. Running from the command line also lets you see the output without having windows close the window too quickly.Code:#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char *argv[]) {
if(argc==2) {
ifstream in(argv[1]);
char ch;
int lines=0;
while(in.get(ch)) if(ch == '\n') lines++;
cout << argv[1] << " has " << lines << " lines" << endl;
}
return 0;
}
Take a basic DOS command: copy.
'C:\x.exe' and 'C:\Folder\' are command line arguments. Its just another way of passing values to a program.Code:copy C:\x.exe C:\Folder\
Can you please explain the purpose of
Thanks!Code:using namespace std;
Its for the new standard headers. In the old <iostream.h> everything was in the global namespace. In the new header <iostream> its in the standard namespace. So you must either doQuote:
Originally posted by luckygold6
Can you please explain the purpose of
Thanks!Code:using namespace std;
Or...Code:#include <iostream>
int main( void )
{
std::cout << "Hi" << std::endl;
return 0;
}
Code:#include <iostream>
using namespace std;
int main( void )
{
cout << "Hi" << endl;
return 0;
}
@luckygold6: Have a read through the FAQ
Hum... that'd be a good idea...
Thanks!
I tried to replace
withCode:while(in.get(ch))
but the program is no longer counting the correct number of lines. Can you explain why?Code:while (in >> ch)
Thanks!
Nevermind, I figured it out.