Requirements:
Text file, having structure similar to like this.
Label-1
$A,1234,B56F,5.678;:
Label-2
$B,7890,C00E,5.678;:
........
Analysis:
The case under consideration is as:
Label-1
$A,1234,B56F,5.678;:
'Label-1' relates to data block "$A,1234,B56F,5.678;" (separated by ','). In my case data block format consist of three parameters is pre-decided:$A -> Header1234 -> Integer Value
B56F -> Hex Value
5.678 -> Float Value
; -> Footer
: -> represents the termination of data block
Upon retrieval of data block, the task is to extract and assign values to data structure parameters as per requirements.
Design & Development:
'tsDataBlock' is structure with four parameters as observed in analysis phase:
Design & Development:
'tsDataBlock' is structure with four parameters as observed in analysis phase:
typedef struct stDataBlock
{
int iParam1;
int iParam2;
float fParam3;
};
int _tmain(int argc, _TCHAR* argv[])
{
stDataBlock sDataBlock;
ifstream inFile;
char cLabel[10] = "Label-1"; // Label to search
bool bFlag = false;
char cDataBuffer[50];
char cParam1[10];
char cParam2[10];
char cParam3[10];
char *pch[5];
int i = 0;
inFile.getline(&cDataBuffer[0], 10); // Get line containing Label
// Get corresponding data block
{
// Check if Parameter 1 is integer as data type in data structure
// Finally print all values
printf("\nParam 1 %d", sDataBlock.iParam1);
printf("\nParam 2 %x", sDataBlock.iParam2);
printf("\nParam 3 %f", sDataBlock.fParam3);
cin.ignore();
return 0;
}
while (!bFlag && (inFile.eof() != 1))
{
if (strcmp(cDataBuffer, cLabel) == 0)
{
inFile.getline(&cDataBuffer[0], 50, ':');
cout<<"Corresponding Data Block: ";
// String tokenizer, excepts header, separator, and footer character
while (pch[i] != NULL)
{
i++;
pch[i] = strtok(NULL, "$,;");
}
strcpy(cParam1, pch[0]); // Parameter 1
strcpy(cParam2, pch[1]); // Parameter 2
strcpy(cParam3, pch[2]); // Parameter 3
printf("Param 1: %s, Param 2: %s, Param 3: %s",
cParam1, cParam2, cParam3);
cParam1, cParam2, cParam3);
if (i == 3)
{
bFlag = false;
// Check if Parameter 2 as hexadecimal data type
if(!sscanf(cParam2,"%x", &sDataBlock.iParam2))
bFlag = false;
// Check if Parameter 3 is float as data type
if(!sscanf(cParam3,"%f", &sDataBlock.fParam3))
bFlag = false;
}
// Finally print all values
printf("\nParam 1 %d", sDataBlock.iParam1);
printf("\nParam 2 %x", sDataBlock.iParam2);
printf("\nParam 3 %f", sDataBlock.fParam3);
}
}
}
cin.ignore();
return 0;
Very interesting work sir, congratulations. I'll be looking forward for your next post. Greetings from Chile.
ReplyDeleteMany thanks :)
ReplyDeleteGreat work done buddy........Hope it will help in future....:)
ReplyDeleteKeep it up :P