Line Count Challenge Solution - Cprogramming.com







 



 



Google
 
Webcprogramming.com




An Affiliate of AIHorizon




Line Count Challenge Solution

#include <fstream>
#include <iostream>


using namespace std;

int main(int argc, char* argv[])
{
	if(argc!=2)
	{
		cout<<"Input should be of the form 'count filename.txt'";
		return 1;
	}
	else
	{
		ifstream input_file(argv[1]);
		if(!input_file)
		{
			cout<<"File "<<argv[1]<<" does not exist";
			return 0;
		}
		char c;
		int count = 1;
		while(input_file.get(c))
		{
			if(c == '\n')
			{
				count++;
			}
		}
		cout<<"Total lines in file: "<<count;
	}
	return 0;
}
Download Source

Many other solutions exist! If yours doesn't match the Cprogramming.com key, but it still works, let us know and we'll upload it as an alternative answer.



-----
Interested in advertising with us?
Please read our privacy policy.
Copyright © 1997-2006 Cprogramming.com. All rights reserved.