Counting lines in a text file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Counting lines in a text file and further operations

 
0
  #1
Feb 25th, 2009
I have a very similar assignment in which we have been specified that we are NOT allowed to use vectors. Is there any other way in this case to count the number of lines of data within a text file??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Counting lines in a text file

 
0
  #2
Feb 25th, 2009
If you are not allowed to use vector then you will have to read the entire file, counting the line numbers as the loop progresses. There is no other way to do it.

Why do you need the line count? Maybe there are alternative ways to achieve what you want to do.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Counting lines in a text file

 
0
  #3
Feb 25th, 2009
Originally Posted by Ancient Dragon View Post
If you are not allowed to use vector then you will have to read the entire file, counting the line numbers as the loop progresses. There is no other way to do it.

Why do you need the line count? Maybe there are alternative ways to achieve what you want to do.
I have to use a dynamic array structure to store the contents of a an input text file. Firstly I do not know how long the input text file will be and thus need to work this out first. The use of a dynamic array is also necessary as we are required to be able to insert and delete elements from the array. Also I am having problems with storing strings into my array as I only know how to store integers. Could you help with this at all? Thanks!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Counting lines in a text file

 
0
  #4
Feb 25th, 2009
You could use malloc() and realloc() to allocate space for the array. One way to do it is to allocate one row in the array each time a new line is read from the file. But that is slow and inefficient. I prefer to allocate a block of rows and when they get filled up inreeast the array by another block of rows.
  1. char** array = 0; // 2d array of strings
  2. int arraysize = 0; // current number of rows in the array
  3. int elmsused = 0; // number of rows actually used
  4. const int BLOCKSIZE = 10;
  5.  
  6. string line;
  7. ifstream in("file.txt");
  8. while( getline(in, line) )
  9. {
  10. if( elmsused == arraysize)
  11. {
  12. array = (char **)realloc(array, arraysize+BLOCKSIZE);
  13. arraysize += BLOCKSIZE);
  14. }
  15. array[elmsused] = (char *)malloc(line.size()+1);
  16. strcpy(array[elmsused], line.c_str());
  17. elmsused++;
  18. }

}
Last edited by Ancient Dragon; Feb 25th, 2009 at 8:08 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC