Line count program, how does this work?

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 2213 Views - Last Post: 01 March 2012 - 10:50 AM Rate Topic: -----

#1 Mongrel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-October 11

Line count program, how does this work?

Posted 28 February 2012 - 10:44 AM

Hi. I've been doing some exercises to get myself back into the swing of things with programming since it's been maybe a year or two since I've done any of it. I'm on file I/O now, and I found a programming challenge on the internet that asked you to input a .txt file, count the number of lines in it, then output that number. I couldn't remember how to search strings, so I peeked at the answer to see how they did it and got confused...I don't know how this code works, can you help me? I've included comments that indicate how I understand various parts of the code (the original solution had no comments in it). I even tried running it, but all I got was the first error message (Input should be of the form 'count filename.txt').

#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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Line count program, how does this work?

#2 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Line count program, how does this work?

Posted 28 February 2012 - 10:55 AM

What does the input file look like? I think that would help alot here.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Line count program, how does this work?

Posted 28 February 2012 - 10:56 AM

int main(int argc, char* argv[])
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

Was This Post Helpful? 2
  • +
  • -

#4 Mongrel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-October 11

Re: Line count program, how does this work?

Posted 28 February 2012 - 11:14 AM

@jdavi well that's the thing, there doesn't seem to be anything specified here. I was going to use a document called text.txt which I would have maybe four or five lines of text in, but I don't know where to input that 0.o

@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.
Was This Post Helpful? 0
  • +
  • -

#5 Mongrel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-October 11

Re: Line count program, how does this work?

Posted 28 February 2012 - 11:26 AM

whoops, looks like I posted while you were editing your post CTphp. And I have no idea how to edit mine o.o.

I'll try what you said.
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,231
  • Joined: 31-December 10

Re: Line count program, how does this work?

Posted 28 February 2012 - 11:33 AM

argc will always be at least 1, the first element of argv, arg[0], is the name of the program. If argc is greater than 1, then the program was passed command-line arguments and they are located in argv[1] through argv[argc - 1]. main is just a function, so whenever you open the program, the OS knows to call main(), but you can pass arguments to a function call so the OS passes to main the argument count and the array of argument strings. That's how those values are filled in.
Was This Post Helpful? 2
  • +
  • -

#7 Mongrel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-October 11

Re: Line count program, how does this work?

Posted 28 February 2012 - 11:56 AM

@CTphp I run Windows 7 and haven't used anything in DOS for a few years now. I can't seem to find out how to get this to work o.o;

@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?
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Line count program, how does this work?

Posted 28 February 2012 - 12:03 PM

Windows 7 Command Prompt Frequently Asked Questions

How do I... Accessing Command Line Arguments
Was This Post Helpful? 1
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Line count program, how does this work?

Posted 28 February 2012 - 12:58 PM

View PostMongrel, on 28 February 2012 - 02:56 PM, said:

@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?

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;
}


Was This Post Helpful? 1
  • +
  • -

#10 Mongrel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-October 11

Re: Line count program, how does this work?

Posted 28 February 2012 - 12:59 PM

I checked out both of those links, but I didn't find any information on how to call a program from the command line...I assumed it was the command cd (which is what CTphp used, though it may be different for Windows 7 than it is for Unix) followed by the path to the program itself, but keeps saying that it cannot find the path specified. This is what my command prompt looks like (test is the name of the program):

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.
Was This Post Helpful? 0
  • +
  • -

#11 Mongrel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-October 11

Re: Line count program, how does this work?

Posted 28 February 2012 - 01:10 PM

@CTphp okay, I understand how argv[] and argc work now, thanks.

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)?
Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,231
  • Joined: 31-December 10

Re: Line count program, how does this work?

Posted 28 February 2012 - 01:12 PM

The "cd" command is the "change directory" command. Windows use backslashes ('\') for it's file system whereas *nix systems use the forward-slashes ('/'). To begin, you need to move to the folder where your program is stored.
[code]
$> cd C:\My Documents\folder
$> nameOfProgram.exe arg1 arg2 arg3

This post has been edited by vividexstance: 28 February 2012 - 01:12 PM

Was This Post Helpful? 1
  • +
  • -

#13 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Line count program, how does this work?

Posted 28 February 2012 - 01:15 PM

I use XCode, and by default it uses a template to start a project:
#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.
Was This Post Helpful? 0
  • +
  • -

#14 Mongrel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 03-October 11

Re: Line count program, how does this work?

Posted 28 February 2012 - 01:37 PM

I put in this after the command prompt:

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.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Line count program, how does this work?

Posted 28 February 2012 - 01:56 PM

Why do you have the "cd" in your command line? The cd means change directory. You can not change directory to an exe file, only to a directory. If your program is indeed in the C:\Documents\folder1\folder2\ you should be able to cd C:\Documents\folder1\folder2 to go to that directory and then run your program with test.exe one two three four from that directory.


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
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2