Originally Posted by
amrith92
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 Last edited by niek_e; Sep 4th, 2008 at 5:44 am.