Thread: Small program big problem :-(

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Small program big problem :-(

    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);
    }

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I actually forgot when it started working and when it didn't, but the one thing that I did do was take the eof() out of the loop expression, replaced it with std::getline instead of istream::getline (this means, string's isntead of char[]'s), and then some other minor changes (substr instead of strncmp, added a catch() statement, took out self explanitory comments, whatever!)

    Code:
    #include <string> 
    #include <iostream> 
    #include <ctime>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    int main(void)
    {
    	clock_t start = clock(), end, time;
    	int nOfLine = 0, nOfOmitedLine = 0; 
    	long int i = 0; 
    
    	vector<string> serverLog;
    	string logLine;
        
    	try
    	{
    		ifstream logFile("C:\\a.txt");
    		if (!logFile)
    			throw string("Unable to open file\n");
    
    		//
    		// read log lines from server.log to vector
    		//
    		while(getline(logFile, logLine))
    		{
    			nOfLine++;
    			if(logLine.substr(0, 2) == "T\t") 
    			{
    				serverLog.push_back(logLine);
    				cout << serverLog.size() << endl;
    			} 
    			else 
    				nOfOmitedLine++;
    		}
    
    		logFile.close();
    
    		end = clock();
    		time = ( end - start ) / CLOCKS_PER_SEC;
    
    		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;
    	}
    	catch(string& e)
    	{
    		cout << e;
    	}
    	return 0;
    }
    Anyways, I think it works now that I mutilated it.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    It works !!!

    Thank You !!!

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Glad to hear it

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    How you think ? Why it worked with somll files and did not work with big files ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. Replies: 2
    Last Post: 04-25-2005, 11:59 AM