Archived

This topic is now archived and is closed to further replies.

The End?

This topic is 5749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a C++ program that counts the letters, spaces, etc. in a text file. I want to implement how many end of line characters are used but am unsure of how to do this. Can someone please help me, it would greatly be appreciated.

Share this post


Link to post
Share on other sites
Hi there,
I think ifstream has a method called getline(). Which perhaps behaves the way you want, but i'm not sure. You know that on windows you have two characters describing the end of a line ?

Good luck,
NextS


[edited by - NextS on March 20, 2002 8:14:32 PM]

Share this post


Link to post
Share on other sites
Yes I have tried to show a character doing the end of a line like this:


   
if ((ch == 2) || (ch == 3))
{
endline++;
}


But this does not work. The 2 and 3 are for start of line and end of line. And the ch is for the incoming text.

[edited by - Kokiri Kid on March 20, 2002 8:20:19 PM]

[edited by - Kokiri Kid on March 20, 2002 8:20:59 PM]

Share this post


Link to post
Share on other sites
Hi there.
ahm. Dunno.

A simple test looks like this:


        
#include <fstream>

using namespace std;

int main ( int argc, char ** argv )
{
ifstream file ("dummy.txt" );

int linecount = 0;

while (!file.eof())
{
unsigned char ch;
file.get( ch );
if (!file.eof())
{
if (ch == '\n' || ch == '\r' )
linecount++;
cerr << ch;
}
}
cerr << "\n" << linecount << " "<<(int)'\n' << " " <<(int)'\r' << endl;
}

<SPAN CLASS=editedby>[edited by - NextS on March 20, 2002 9:05:36 PM]</SPAN>

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Enjoy:


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

int main()
{
std::ifstream inputStream("test.cpp");

int count = 0;

while (std::getline(inputStream, std::string(), ''\n''))
{
++count;
}

std::cout << "Number of lines in file: " << count << std::endl;

return 0;
}

Share this post


Link to post
Share on other sites
If you wish to count any character type, then you can make use of the STL count algorithm. Here''s an example that uses 4 statements to read in a text file and count the number of spaces:


  
#include <fstream>

#include <iterator>

#include <algorithm>

#include <vector>

#include <functional>


using namespace std;

typedef char elem;
typedef vector<elem> container;

int main()
{
container c;

ifstream input( "test.txt" );
if(input)
copy( istreambuf_iterator<elem>(input),
istreambuf_iterator<elem>(),
back_inserter(c));

int num = count( c.begin(), c.end(), '' '');
}


The third statement reads the entire file into the vector of characters, and the final statement counts all the elements of the vector which match the given pattern (space). I think you can guess how to count newlines, etc.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Copying the stream into a vector is redundant because you can just use istream_iterators when using std::count. If your worried about effeciency, you could supply a custom functor that counts based on the current character (i.e. to avoid you having to loop through everytime you want to count a new character).

Share this post


Link to post
Share on other sites