hey,

This might be naive, but how come this bit of code isn't working???

int lc=0;

read.open(filename_user.c_str(), ios::in);
while(getline(read, linecount, '\n'))
{
                     ++lc;
                     }

Have looked through the internet, and this code seems to be fine, so what is it that makes it not work??

the output is always 0, no matter what file I try to open.

William Hemsworth commented: I really hope that "void main" in your sig is a joke :) +3

Recommended Answers

All 13 Replies

You should check that the file actually opened.

read.open(filename_user.c_str(), ios::in);
if( ! read )
{
  //error handler here
}
else
     while(getline(read, linecount, '\n'))
     {
          ++lc;
     }

Are you sure the data file is in the place where your program is looking for it?

Actually, it does check whether the file is open and it does also show the contents of the file...The only thing that doesn't work is the line count I made. But I see what you mean: Below is the whole code:

unsigned int lc=0;
     reDo:
     cout << "\n\n\n\n\tUser, please Enter the filename (with directory) below:\n\n\t\t::==:: ";
     getline(cin, filename_user); 
     
     ifstream read(filename_user.c_str());
     
     if(read.fail())
     {
                    cout << "\n\n\t";
                    error_slashes();
                    cerr << "\n\n\t   Sorry, but this program was unable to locate/read\n\n\t\t\tthe given file!\n\n\t";
                    error_slashes();
                    
                    cin.get();
                    cout << "\n\n\t\tDo you wish to open a new file? (y/n[quit]) ";
                    cin >> choice;
                    
                    if(choice=='y')
                    {
                                   goto reDo;
                                   }
                                   else
                                   {
                                       exit(1);
                                       }
                    }
     read.open(filename_user.c_str(), ios::in);
     while(getline(read, linecount, '\n'))
     {
                         ++lc;
                         }
                         read.clear();//clear memory
                         read.close();
                         read.open(filename_user.c_str(), ios::in);
                         while(getline(read, lines, '\n'))
                         {
                                             cout << lines << "\n\n\n";
                                             }
                         cout << "\n\n\t\t" << lc;

In the code, it checks for the existence of the file before opening...and it does output the file contents...Only thing not working is the line count.

Is the method correct?

Member Avatar for iamthwee

Why don't you just create a simple linecount program to see where you're going wrong?

read.open(filename_user.c_str(), ios::in);
    while(getline(read, linecount, '\n'))
    {
        ++lc;
    }
    read.clear();//clear memory
    read.close();
    read.open(filename_user.c_str(), ios::in);
    while(getline(read, lines, '\n'))
    {
        cout << lines << "\n\n\n";
    }
    cout << "\n\n\t\t" << lc;

If this code were to work at all, it would display the cout line as many times as there are newlines in the file. I suppose that wasn't your intent?

There are a few other things wrong with your program, but I guess this isn't all of it?

Anyway, here's a small example for linecounting.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string buff;
    int count = 0;
    ifstream input("c:\\input.txt"); //or something
    if (!input.is_open()) return 1; // error handling goes here
    while (getline(input, buff, '\n')) count++;
    cout << "file has " << count << " lines";
    return 0;
}

Just out of curiosity: You do know that void main() doesn't exist right? I'm referring to your signature...

Also: Using goto is considered bad coding-practice. 99.9% of the cases (including yours) the better answer would be to use a loop. See attachment

ps. You could also have a look at indenting code

commented: Thanks for giving a reasonable answer, plus some advice! +1

Just out of curiosity: You do know that void main() doesn't exist right? I'm referring to your signature...

Good... Even signature is checked! :twisted:

Just out of curiosity: You do know that void main() doesn't exist right? I'm referring to your signature...

Good... Even signature is checked! :twisted:

Yes, I am aware that void main doesn't exist as a standard, although this does work in older c++ compilers, like the ones I used to use...

If this code were to work at all, it would display the cout line as many times as there are newlines in the file. I suppose that wasn't your intent?
There are a few other things wrong with your program, but I guess this isn't all of it?

Well, the code does do this, and the only reason this exists is to check that the program can actually render the file properly...and it does, it keeps printing the line as many times as it encounter '\n' delimiter.
And you're correct, This is just a part of a bigger program. I'll try your code out and will try and modify my own...

Thanks for all the replies...

[EDIT]Thanks for your code and advice, 'niek_e', it helped such a lot! The code's finally working!!

Why even bother reading the signature?(although you got to admit, some are pretty funny)

Why even bother reading the signature?(although you got to admit, some are pretty funny)

I quite agree to that myself....

Why even bother reading the signature?(although you got to admit, some are pretty funny)

I quite agree to that myself....

I'm trying to help people with C++. void main() isn't standard C++, so every time I encouter it, I mention it to the poster. That way, when/if the poster gets a job in the programming-field, he/she won't get laughed out of the building by using void main() .

What difference does it make if I see it in the post or in the signature?

Hmm, just a thought .. how about changing one line of the signature to:

cout << "Hi there!" << "I'm a non-standard-compliant C++ coder!";


;)

I'm trying to help people with C++. void main() isn't standard C++, so every time I encouter it, I mention it to the poster. That way, when/if the poster gets a job in the programming-field, he/she won't get laughed out of the building by using void main() .

What difference does it make if I see it in the post or in the signature?

And you're correct as well, but I never use void main() now-a-days, although I have some really old compilers on my computer that accept void main() , so I used it when I was learning basic c++. And the only reason its like that in my signature is because you're allowed only 5 lines, and a return statement will make 6...

Hmm, just a thought .. how about changing one line of the signature to:

cout << "Hi there!" << "I'm a non-standard-compliant C++ coder!";

Haha :D

and a return statement will make 6...

I'll give you the benefit of the doubt ;)

Haha :D


I'll give you the benefit of the doubt ;)

lol...okay!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.