File counter (to be run from t3h prompt)
Feb 1, 2009 at 6:37pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
if (argc == 0)
{
cout << "Expected argument. Terminating.\n" ;
return 1;
}
ifstream ftbc(argv[0]);
string thestr;
int counted = 0;
while ( !ftbc.eof() )
{
getline(ftbc, thestr);
counted++;
}
cout << counted << "lines in file\n" ;
}
When I run it, it just idles, doing nothing in particular. Any id-...er, what did I do wrong?
Feb 1, 2009 at 6:40pm UTC
argv[0] would refer to the name of the executable, not the parameter. Try argv[1]. I have only used getline on text files; I'm not sure but that might be the problem.
Feb 1, 2009 at 6:43pm UTC
Yay! Worked! Thanks.
Topic archived. No new replies allowed.