How to read a file in a timer function in Visual C++ using .NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bmerlover
    New Member
    • Jul 2007
    • 49

    How to read a file in a timer function in Visual C++ using .NET

    How can you read a file in a timer function? I am currently using a do while statement to read a file which works fine. The implementation is in C but I've added the code to a C++ .NET framework GUI app. This is the implementation that works:

    Code:
    void findPacketHeaders(char *inputFile, char *outputFile)
    {
    	/* Open file */
    	fpInput = fopen(inputFile, "rb");
    	fpOutput = fopen(outputFile, "w");
    	
    	/* check if fpInput exists */
    	if (!fpInput)
    	{
    		fprintf(stderr, "Unable to open file %s", inputFile);
    		return;
    	}
    
    
    	/* Read fpInput contents into buffer */
    	do 
    	{
    
    		if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB)
    		{
    
    			getPacketHeaderSegments(fpInput, pktHeader, fpOutput);
    		}
    	
    	}while(!feof(fpInput));
    
    
    	fclose(fpInput);
    	fclose(fpOutput);
    }
    What I would like to do is implement this in a timer function so that the computer won't lag because the file I open is very large and when its reading the file with the do while, the CPU usage goes to 100% with no response although it does finish after a while. I have used timer functions in the passed to solve "while" issues but I can't seem to get it to read this file.

    FILE *fpInput, *fpOutput;
    packetHeader pktHeader;

    are global variables and they do work correctly.

    This is my implementation of the timer:

    Code:
    
    void findPacketHeaders(char *inputFile, char *outputFile)
    {
    	//FILE *fpInput, *fpOutput;
    	//packetHeader pktHeader;
    
    	pktHeader.packetSyncPattern = 0xEB25;
    
    	/* Open file */
    	fpInput = fopen(inputFile, "rb");
    	fpOutput = fopen(outputFile, "w");
    	
    	/* check if fpInput exists */
    	if (!fpInput)
    	{
    		fprintf(stderr, "Unable to open file %s", inputFile);
    		return;
    	}
    
    	processTimer->Interval = 10;
    	processTimer->Enabled = true;
    
    	fclose(fpInput);
    	fclose(fpOutput);
    
    }
    
    private: System::Void processTimer_Tick(System::Object^  sender, System::EventArgs^  e) 
    		 {
    			/* Read fpInput contents into buffer */
    			richTextBox_console->AppendText(fgetc(fpInput).ToString("X") + "\n");
    			
    			if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB)
    			{
    
    				getPacketHeaderSegments(fpInput, pktHeader, fpOutput);				
    			}
    
    			if(feof(fpInput))
    			{
    				/* break out of this */
    				processTimer->Enabled = false;
    			}
    
    		 }
    [\CODE]
  • madankarmukta
    Contributor
    • Apr 2008
    • 308

    #2
    Hi..

    Did you tried debugging this functions..?

    That may help you a lot..!!

    Thanks!

    Comment

    • bmerlover
      New Member
      • Jul 2007
      • 49

      #3
      Well, I tried printing what fgetc outputs which is -1 all the time or the hex representation is FFFFFFFF. It seems like the fgetc isn't working correctly inside the timer function.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Maybe you should read the file on a different thread and raise an event when it is done. Sure enough duct tape can fix anything, but patches on top of patches is never the way to go.

        Comment

        • bmerlover
          New Member
          • Jul 2007
          • 49

          #5
          I found out what the problem was, when the timer started, the file was already closed because of the fclose statements at the bottom of findPacketHeade r. So I rearranged the code to something similar to this. Thanks for replying, I appreciate the inputs.

          Code:
          void findPacketHeaders(char *inputFile, char *outputFile) 
          { 
              pktHeader.packetSyncPattern = 0xEB25; 
            
              /* Open file */ 
              fpInput = fopen(inputFile, "rb"); 
              fpOutput = fopen(outputFile, "w"); 
            
              /* check if fpInput exists */ 
              if (!fpInput) 
              { 
                  fprintf(stderr, "Unable to open file %s", inputFile); 
                  return; 
              } 
            
              processTimer->Interval = 10; 
              processTimer->Enabled = true; 
           
          } 
            
          private: System::Void processTimer_Tick(System::Object^  sender, System::EventArgs^  e)  
                   { 
                      /* Read fpInput contents into buffer */ 
          
                      if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB) 
                      { 
            
                          getPacketHeaderSegments(fpInput, pktHeader, fpOutput);                 
                      } 
            
                      if(feof(fpInput)) 
                      { 
                          /* break out of this */ 
                          processTimer->Enabled = false; 
          
                          fclose(fpInput); 
                          fclose(fpOutput); 
            
                      } 
            
                   }

          Comment

          Working...