I have just written small program that reads txt file and populates vector of strings (STL). Everything was ok. when I worked with small files but when I incresed size of file after 13988 lines program looks like frozen :-( Could someone put on this issue some light ? I tried to change vector to different structure i.e. list or stack but without success - stil the same behavior. Does anyone know where is problem ?
Code:
#include <string>
#include <iostream>
#include <ctime>
#include <fstream>
#include <vector>
#include <stdexcept>
using namespace std;
int main(void)
{
clock_t start,
end,
time;
char logLine[512];
int nOfLine = 0,
nOfOmitedLine = 0;
long int i = 0;
vector<string> serverLog;
start = clock(); // start clock
system ("cls"); /* Or system ("clear"); for Unix */
//
// Open the file and check for errors
//
ifstream logFile("srv.log");
if (!logFile) {
throw invalid_argument("Unable to open file\n");
}
//
// read log lines from server.log to vector
//
while(!logFile.eof()) // read log lines from server.log to vector
{
logFile.getline(logLine,512);
nOfLine++;
if (strncmp(logLine, "T\t", 2) == 0) {
serverLog.push_back(logLine);
cout << serverLog.size() << endl;
} else {
nOfOmitedLine++;
}
}
logFile.close();
end = clock(); // stop clock
time = ( end - start ) / CLOCKS_PER_SEC; //compute time of execution
cout << "Vector size: " << serverLog.size() << endl;
cout << "Vector capacity: " << serverLog.capacity() << endl;
cout << "Number of read lines: " << nOfLine << endl;
cout << "Number of omited lines: " << nOfOmitedLine << endl;
if (time < 1) {
cout << "Time (sec): < 1"<< endl;
} else {
cout << "Time (sec): " << time << endl;
}
return(0);
}