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:
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]
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);
}
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;
}
}
Comment