943,661 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1601
  • C++ RSS
Feb 25th, 2009
0

Re: Counting lines in a text file and further operations

Expand Post »
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??
Similar Threads
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Feb 25th, 2009
0

Re: Counting lines in a text file

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Feb 25th, 2009
0

Re: Counting lines in a text file

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!
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Feb 25th, 2009
0

Re: Counting lines in a text file

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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with heapsort
Next Thread in C++ Forum Timeline: Updating the time from a thread





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC