| | |
Counting lines in a text file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
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??
--
Index of mp3
Index of mp3
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.
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.
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
•
•
•
•
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.
--
Index of mp3
Index of mp3
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)
char** array = 0; // 2d array of strings int arraysize = 0; // current number of rows in the array int elmsused = 0; // number of rows actually used const int BLOCKSIZE = 10; string line; ifstream in("file.txt"); while( getline(in, line) ) { if( elmsused == arraysize) { array = (char **)realloc(array, arraysize+BLOCKSIZE); arraysize += BLOCKSIZE); } array[elmsused] = (char *)malloc(line.size()+1); strcpy(array[elmsused], line.c_str()); elmsused++; }
}
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.
![]() |
Similar Threads
- Counting lines in a text file and further operations (C++)
- easist way to find out # of lines in a text? (C++)
- how to count paragraphs from a text file. (C)
Other Threads in the C++ Forum
- Previous Thread: Problem with heapsort
- Next Thread: Updating the time from a thread
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






