Automotive and Embedded Networks

cancel
Showing results for 
Search instead for 
Did you mean: 

How to search the words in text file using CAPL

I have text file say test.txt and I have to search some 10 words from that text file. Could someone please help how can we search words from that text file using CAPL

0 Kudos
Message 1 of 3
(7,378 Views)

Check out fileGetString. Below is what is an example code in the CAPL help menu.

variables
{
msTimer writeTimer;
long glbPeriod = 250;

dword glbHandle = 0;
long glbValue;
}

on Start
{
char buffer[64];
long ret;

//
// Opens the file in ASCII mode for read access.
//
// To determine the absolute path, the search procedure will be used.
// The file must be located in the directory of the databases or the
// configuration directory.
//
glbHandle = OpenFileRead ("Data.Txt",0);

if ( glbHandle!=0 )
{
//
// got to end of file ...
//
while ( fileGetString(buffer,elcount(buffer),glbHandle)!=0 ) {};
//
// Get the last parameters
// (saved on disk after the end the last measurement)
//
glbValue = atol (buffer);
write ("Last value %d.",glbValue);
fileClose (glbHandle);
}
else
{
write ("File 'Data.Txt' was not opened for read access.");
}
//
// Open the file in ASCII mode for write access.
//
// The write path was not set using the function setWritePath(), so
// the configuration directory will be used instead. This is the
// default behavior.
//
glbHandle = OpenFileWrite ("Data.Txt",2);

if ( glbHandle!=0 )
{
setTimer (writeTimer,glbPeriod);
}
else
{
write ("File 'Data.Txt' was not opened for write access.");
}
}
on timer writeTimer
{
long randomValue;
char buffer [64];

if ( glbHandle!=0 )
{
randomValue = random (32767);
snprintf (buffer,elcount(buffer)," %d \n",randomValue);
filePutString (buffer, elcount(buffer),glbHandle);
setTimer (writeTimer,glbPeriod);
}
else
{
write ("Error, invalid file handle.");
}
}

on StopMeasurement
{
fileClose (glbHandle);
}
 
0 Kudos
Message 2 of 3
(7,221 Views)

Hello,

 

I tried the example given by CAPL and it works. But, in this instance, numbers are written line by line. If i write in the same line 5 numbers, how do yo do to get the 3rd number ?

 

Sincerely,

 

Karbure

0 Kudos
Message 3 of 3
(5,743 Views)