GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Oct-2004, 07:15
wc3promet wc3promet is offline
New Member
 
Join Date: Oct 2004
Posts: 7
wc3promet is on a distinguished road

Problems in Counting Lines in Text File


Problems in Counting Lines in Text File

A Portion of my file read code to count lines:
CPP / C++ / C Code:
string temp;
.
.
.
  while(!in.eof())
    {
    	if (!(in>>temp)) break;
      	else
      	{
      	 	if(temp=="\n")
          	{
          	 linecount++;
          	}
                cout<<"Line No: "<<linecount<<"\n"<<endl;
        }

The linecount stays at a permanent 0
  #2  
Old 22-Oct-2004, 12:57
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
You should be looking for '\n' as a character and not as a "\n" which is string notation.

change your inner if condition to

CPP / C++ / C Code:
if(temp=='\n')
            { .......
  #3  
Old 22-Oct-2004, 20:12
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by nkhambal
You should be looking for '\n' as a character and not as a "\n" which is string notation.

change your inner if condition to

CPP / C++ / C Code:
if(temp=='\n')
            { .......

... and in.eof() will 'read' one extra character or line.

Another option is to read the file using getline() and count how many times it read rather than char by char testing each one.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.