C++ Line Counter

Hello and welcome to my first post!! :)

I have just started C++ programming and my first project is a program that will read a text file, print it in the console, then tell me the number of lines. Here is the code I have so far:

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
29
30
31
#include <cstdlib>
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
using namespace std;
#include <fstream>
using std::ifstream;

int main(int argc, char *argv[])
{
    ifstream inFile;
    string url;
    inFile.open("links.txt");
   if(!inFile) { 
      cout << "Error: file could not be opened" << endl;
      system("PAUSE");
    return EXIT_SUCCESS;
      exit(1);
   }
int count = 0;
   while (inFile >> url) {   
   cout << url << endl;
        if ( url == '\n') // This is where I'm having problems.
        count++;
   }
   inFile.close();
   cout << "End-of-file reached..\n\n" << "links: " << count << "\n\n\n" << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


The problem is that "url == '\n'" won't compile. It's probably really simple, and I'm really pushing the limits of what I can do with programming in any language with this, but I have a feeling I'm close. Please tell me where I'm going wrong.

Thanks :) .
Hello again.

I just figured it out! Took away the line causing problems, and it works.

Thanks to anyone opening this thread and thinking about helping :D
url is a string so you need double quotes: "\n"
BTW >> operator discards newline characters
^^^ Nope.

When I run it, it only tells me that there are 0 lines. Maybe because it is taking the "\n" literally? Maybe the >> operator has something to do with that, but I don't know what to put instead.

Anyway, I got it working and it counts lines just fine ;)

Thanks a lot.
You should actually use getline
^^^
Could you give a code example please? I'm sorry I just don't know how to use it.

And how would you make it read only lines with specific words? So that it would sort it out? I'm using it for sorting a list of URLs if that helps.

Thanks
Here is a way of using getline to count lines:
1
2
3
4
5
6
string line;
while ( inFile )
{
    getline ( inFile, line );
    count++;
}

And how would you make it read only lines with specific words?
You can't read only lines with a specific word as you don't know that they contain it before you read them.
You can read each line and then check whether it contains that word and act accordingly.
Can you give me an example of how you are trying to sort those URLs so I can be more specific?
Thanks, to be more specific, here is what I am thinking:

I have a small self-made Perl Webcrawler that gives me a long list of URLs that it has visited. I want to extract the links from a specific domain so that instead of listing every single one of the URLs, as it does now, it will give me only the ones from the domain that I ask for.

If that sounds to complicated, then maybe I should consider limiting the webcrawler to a specific domain, but I wanted to focus on C++ and I am therefore making an attempt at it from this angle instead.
Is not complicated.
So:
1) you get the url in the string
2) you check if it is from that domain
3) if it is, you display it
Parts 1 and 3 are trivial, for part 2 you have tons of functions, here are some:
http://www.cplusplus.com/reference/string/string/compare/ if ( !url.compare( 0, domain.length(), domain ) )
http://www.cplusplus.com/reference/string/string/substr/ if ( url.substr( 0, domain.length() ) == domain )
http://www.cplusplus.com/reference/string/string/find/ if ( url.find( domain ) == 0 )
If the domain string doesn't contain www. replace 0 with 4
Last edited on
Thanks so much for your help. It works perfectly now, pulling out the correct domains.
Topic archived. No new replies allowed.