By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,492 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,492 IT Pros & Developers. It's quick & easy.

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

P: 50
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:

Expand|Select|Wrap|Line Numbers
  1. void findPacketHeaders(char *inputFile, char *outputFile)
  2. {
  3.     /* Open file */
  4.     fpInput = fopen(inputFile, "rb");
  5.     fpOutput = fopen(outputFile, "w");
  6.  
  7.     /* check if fpInput exists */
  8.     if (!fpInput)
  9.     {
  10.         fprintf(stderr, "Unable to open file %s", inputFile);
  11.         return;
  12.     }
  13.  
  14.  
  15.     /* Read fpInput contents into buffer */
  16.     do 
  17.     {
  18.  
  19.         if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB)
  20.         {
  21.  
  22.             getPacketHeaderSegments(fpInput, pktHeader, fpOutput);
  23.         }
  24.  
  25.     }while(!feof(fpInput));
  26.  
  27.  
  28.     fclose(fpInput);
  29.     fclose(fpOutput);
  30. }
  31.  
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. void findPacketHeaders(char *inputFile, char *outputFile)
  4. {
  5.     //FILE *fpInput, *fpOutput;
  6.     //packetHeader pktHeader;
  7.  
  8.     pktHeader.packetSyncPattern = 0xEB25;
  9.  
  10.     /* Open file */
  11.     fpInput = fopen(inputFile, "rb");
  12.     fpOutput = fopen(outputFile, "w");
  13.  
  14.     /* check if fpInput exists */
  15.     if (!fpInput)
  16.     {
  17.         fprintf(stderr, "Unable to open file %s", inputFile);
  18.         return;
  19.     }
  20.  
  21.     processTimer->Interval = 10;
  22.     processTimer->Enabled = true;
  23.  
  24.     fclose(fpInput);
  25.     fclose(fpOutput);
  26.  
  27. }
  28.  
  29. private: System::Void processTimer_Tick(System::Object^  sender, System::EventArgs^  e) 
  30.          {
  31.             /* Read fpInput contents into buffer */
  32.             richTextBox_console->AppendText(fgetc(fpInput).ToString("X") + "\n");
  33.  
  34.             if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB)
  35.             {
  36.  
  37.                 getPacketHeaderSegments(fpInput, pktHeader, fpOutput);                
  38.             }
  39.  
  40.             if(feof(fpInput))
  41.             {
  42.                 /* break out of this */
  43.                 processTimer->Enabled = false;
  44.             }
  45.  
  46.          }
  47.  
  48.  
[\CODE]
Mar 13 '09 #1
Share this Question
Share on Google+
4 Replies


100+
P: 293
Hi..

Did you tried debugging this functions..?

That may help you a lot..!!

Thanks!
Mar 13 '09 #2

P: 50
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.
Mar 13 '09 #3

tlhintoq
Expert 2.5K+
P: 3,525
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.
Mar 13 '09 #4

P: 50
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 findPacketHeader. So I rearranged the code to something similar to this. Thanks for replying, I appreciate the inputs.

Expand|Select|Wrap|Line Numbers
  1.  
  2. void findPacketHeaders(char *inputFile, char *outputFile) 
  3.     pktHeader.packetSyncPattern = 0xEB25; 
  4.  
  5.     /* Open file */ 
  6.     fpInput = fopen(inputFile, "rb"); 
  7.     fpOutput = fopen(outputFile, "w"); 
  8.  
  9.     /* check if fpInput exists */ 
  10.     if (!fpInput) 
  11.     { 
  12.         fprintf(stderr, "Unable to open file %s", inputFile); 
  13.         return; 
  14.     } 
  15.  
  16.     processTimer->Interval = 10; 
  17.     processTimer->Enabled = true; 
  18.  
  19.  
  20. private: System::Void processTimer_Tick(System::Object^  sender, System::EventArgs^  e)  
  21.          { 
  22.             /* Read fpInput contents into buffer */ 
  23.  
  24.             if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB) 
  25.             { 
  26.  
  27.                 getPacketHeaderSegments(fpInput, pktHeader, fpOutput);                 
  28.             } 
  29.  
  30.             if(feof(fpInput)) 
  31.             { 
  32.                 /* break out of this */ 
  33.                 processTimer->Enabled = false; 
  34.  
  35.                 fclose(fpInput); 
  36.                 fclose(fpOutput); 
  37.  
  38.             } 
  39.  
  40.          }
  41.  
  42.  
Mar 14 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.