Line Count Programming Challenge

Pages: 12
I have a problem of figuring out what I am asked to do and how to start. The problem is this:

Here's a simple help free challenge to get you started: write a program that takes a file as an argument and counts the total number of lines. Lines are defined as ending with a newline character. Program usage should be

count filename.txt
and the output should be the line count.

How do you take a file as an argument?

Thank you.
Hi,
> How do you take a file as an argument?

For example :
1
2
3
4
void readCountFile(std::string filename)
{
   // Your code...
}


And in your function main(), you call that function :
readCountFile("input.txt");
Does that help? :)
Thank you, but I still remain confused.
write a program that takes a file as an argument


If you mean taking a file name as a command line argument, then that would be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(int argc, char* argv[])
{
    if(argc < 2) // checking to see if 2 arguments were passed from command line
    {
        cout << "TOO FEW ARGUMENTS\n";
        return 0;
    }

    ifstream file(argv[1]); // delcare and open file

    if(!file) // check if file opened
    {
        cout << "FILE NOT OPENED";
        return 0;
    }


When you run this on the command line, you'll have to pass in the file name as a command line argument. This will result in a total of 2 arguments passed in as the name of the program will automatically be inserted as the first argument (hence why we check for 2 arguments and argv[1] is the file name, because argv[0] is the program name).
Last edited on
@closed account 5a8Ym39o6 (300)
> Does that help? :)

No, you missed the point.

@aurimas13
Program usage should be

count filename.txt


Your teacher means this:

int main(int argc, char *argv[]) {

http://en.cppreference.com/w/cpp/language/main_function

argc is the count of how arguments there were - you can use this to check for correct usage.

argv[0] is the program name - "count"

argv[1] is the first argument - the "filename.txt" Note that this is a char array, not a std::string

Good Luck !!

closed account (48T7M4Gy)
A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method's parameters.


http://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter
Last edited on
Thank you guys. I'm still a bit confused. Ok, so I have int main(int argc, char *argv[], but what should I do with this? I'm a novice. so can please anyone explain in simple terms what take a file as an argument mean and what steps I need to do? I can't grasp how to take a file as an argument and then later count the total number of lines. Does this assignment include ifstream?
Start out with Arslan's code.

Did you understand this part is from the command line?

count filename.txt

have you heard of the function std::getline ?

http://www.cplusplus.com/reference/istream/basic_istream/getline/

You can put that in a loop.

Alternatively, for a less desirable C approach, count the number of newlines.
Did you understand this part is from the command line?


what is command line? I'm confused.

have you heard of the function std::getline ?


Yes.

You can put that in a loop.


Put getline in a loop? What that supposed to mean?
> What is command line? I'm confused.
If all your answers are "I am confused", I would recommend you take this easier approach. And I am sure your professor won't scold badly at you I think :)

> Hi,
>> How do you take a file as an argument?

> For example :
1
2
3
4
void readCountFile(std::string filename)
{
   // Your code...
}


> And in your function main(), you call that function :
readCountFile("input.txt");

Give me several minutes. I will give you some details later.
what is command line? I'm confused.


Once you have your program compiled, type this in the DOS prompt:

count filename.txt

Put getline in a loop? What that supposed to mean?


Inside the body (between the braces { } ) of a while loop.

http://www.cplusplus.com/doc/tutorial/
Thank you @closed account. I will look forward to you response.
By the way what you mean take an easier approach to this assignment? Thanks.
@aurimas13
Before that, can you prepare an input file with some random text (about several lines)? Then show the content of the file (even its file name) to us.
@aurimas13

closed account 5a8Ym39o6 (300) wrote:
If all your answers are "I am confused", I would recommend you take this easier approach. And I am sure your professor won't scold badly at you I think :)


I think you will loose plenty of points for not doing what the assignment says.
@TheIdeasMan
A student should be at least aware of all small concepts before an assignment. Why would a professor force a student to solve such an assignment whereas the student doesn't even know (or has ever learned) what a "command line" is?
Last edited on
@closed account

To prepare an input file, should I simply use text editor, input words in the file and save as a .txt file?
> To prepare an input file, should I simply use text editor, input words in the file and save as a .txt file?
Yes. You don't have to do this anymore :)
Here is an example for you to get started. Try compling it && run it :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void readCountFile(string filename)
{
    ifstream inFile(filename);

    if(!inFile.is_open())
    {
        cout << "Error : File " << filename << " not found" << endl;
        return;
    }

    string t;
    int lineCount = 0;
    while(getline(inFile, t)) lineCount += 1;

    cout << "The file '" << filename << "' has " << lineCount << " line(s)." << endl;
    inFile.close();
}

int main()
{
    readCountFile("input.txt");
    return 0;
}


And here is the content of the file input.txt (example) :
hello
this is my
david beccam is a daddy
mother mommy is mythy self
great
say bye
good nine :)
Last edited on
Does that help? :)
Pages: 12