#include <fstream> //allows files to be inputted, outputted, and manipulate
#include <iostream> // allows basic user interface
using namespace std;
int main(int argc, char* argv[]) /* the start of the function. This also declares two variables: the interger argc and a pointer to an array of characters called argv. I know that argc is commonly used to refer to the number of arguments passed to the program from the command line, and that argv[] is often the listing of all of the arguments, but I don't know how the program knows this */
{
if(argc!=2) /* if argc is not equal to 2. What does this have to do with anything though? There doesn't seem to be any place where argc is defined... */
{
cout<<"Input should be of the form 'count filename.txt'"; /* this is the error given if the file input is incorrect...but I don't understand where this input even comes from. Given that, I have no idea how I can put the input in the form 'count filename.txt' as there is apparently no user input in this program */
return 1;
}
else
{
ifstream input_file(argv[1]); /* this inputs the file that argv[1] points to...though I don't see anywhere that actually assigns argv[1] or any other part of the argv array a value, so where does this inputted file come from? */
if(!input_file) // if the file fails to open
{
cout<<"File "<<argv[1]<<" does not exist"; //error message
return 0;
}
char c;
int count = 0;
while(input_file.get(c)) // this while loop counts the lines in the program, I understand how it works
{
if(c == '\n')
{
count++;
}
}
cout<<"Total lines in file: "<<count;
}
return 0;
}
25 Replies - 1747 Views - Last Post: 01 March 2012 - 10:50 AM
#1
Line count program, how does this work?
Posted 28 February 2012 - 10:44 AM
Replies To: Line count program, how does this work?
#2
Re: Line count program, how does this work?
Posted 28 February 2012 - 10:55 AM
#3
Re: Line count program, how does this work?
Posted 28 February 2012 - 10:56 AM
argv is an array containing the arguments passed to the application.
argc is the number of elements in argv.
Try building this:
#include <iostream>
int main (int argc, char * const argv[]) {
for (int i = 0; i < argc; i++) {
std::cout <<i << "\t" << argv[i] << std::endl;
}
return 0;
}
Then from a command line, type (Unix, your OS may be different) this:
cd /path/to/object/code
./code_name one two three four
It should output something like this:
0 ./args 1 one 2 two 3 three 4 four
where ./args is the compiled object code, or your executable.
This post has been edited by CTphpnwb: 28 February 2012 - 11:12 AM
#4
Re: Line count program, how does this work?
Posted 28 February 2012 - 11:14 AM
@CTphp so...are "int argc" and "char* argv[]" special cases for declaring variables? I assumed that it would work just like declaring a normal variable...i.e. that "int argc" would be essentially identical to "int argumentnum" or something like that; a blank variable that has no meaning until one is assigned to it. Am I wrong here? That is to say, I'm confused as to why "char* argv[]" contains the arguments passed to the application and "int argc" is the number of elements in argv. I can't identify the place in the code where that is specified.
#5
Re: Line count program, how does this work?
Posted 28 February 2012 - 11:26 AM
I'll try what you said.
#6
Re: Line count program, how does this work?
Posted 28 February 2012 - 11:33 AM
#7
Re: Line count program, how does this work?
Posted 28 February 2012 - 11:56 AM
@vividex okay, so then argv[] and argc are special cases? Or maybe are they only special cases when they are defined in main itself rather than later? If they're not then how does the program know to assign those specific values to those variables?
#8
Re: Line count program, how does this work?
Posted 28 February 2012 - 12:03 PM
#9
Re: Line count program, how does this work?
Posted 28 February 2012 - 12:58 PM
Mongrel, on 28 February 2012 - 02:56 PM, said:
No, they're arguments to a function like any other. It's just that in this case main() is the function. If you try my code above, but without defining them in main then it will fail:
#include <iostream>
int main (){
for (int i = 0; i < argc; i++) {
std::cout <<i << "\t" << argv[i] << std::endl;
}
return 0;
}
#10
Re: Line count program, how does this work?
Posted 28 February 2012 - 12:59 PM
C:\Users\computer name>cd/documents/folder1/folder2/test
I have also tried ending the command prompt with "test.cbp," "test one two three four" and "test.cbp one two three four," but all of them yielded the same result.
#11
Re: Line count program, how does this work?
Posted 28 February 2012 - 01:10 PM
By the way, am I correct in assuming that a program that starts with int main(int argc, char* argv[]) cannot be run from the compiler and must be run from the command prompt (or by another program)?
#12
Re: Line count program, how does this work?
Posted 28 February 2012 - 01:12 PM
[code]
$> cd C:\My Documents\folder
$> nameOfProgram.exe arg1 arg2 arg3
This post has been edited by vividexstance: 28 February 2012 - 01:12 PM
#13
Re: Line count program, how does this work?
Posted 28 February 2012 - 01:15 PM
#include <iostream>
int main (int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I'm forgetful, so needless to say, I often leave those arguments in place but don't use them. The compiler has no problem running that code.
#14
Re: Line count program, how does this work?
Posted 28 February 2012 - 01:37 PM
cd C:\Documents\folder1\folder2\test.exe one two three four
and got the same error message. I also tried it with "My Documents" instead of "Documents" and with "test.cbp" instead of "test.exe" (since that is apparently the file type of my program; there doesn't seem to be a .exe file) as well as with "Users\computer name\" after C:\. Still getting the same message.
Also, in response to CTphp, the program will run fine (that is, with no errors) from my compiler as well (I use Code::Blocks btw), but all it does is give me the "Input should be of the form 'count filename.txt'" error and allow me to press any key to end the program. So it doesn't seem to actually do what I want it to when run from there, nor does it seem to give me any way to manipulate it into doing what I want it to.
#15
Re: Line count program, how does this work?
Posted 28 February 2012 - 01:56 PM
Since you are using Code::Blocks to compile this program you should be able to add the command line parameters to your project by selecting Project Set programs' arguments from the menu. Then typing in the correct arguments.
Jim
|
|

New Topic/Question
Reply



MultiQuote






|