Wednesday, June 29, 2011

Extracting specific data block, from text file, associated withcorresponding label and store data in structure variables of respectivedata type by using C/C++ environment

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:

typedef struct stDataBlock
{
          int iParam1;
          int iParam2;
          float fParam3;
};
int _tmain(int argc, _TCHAR* argv[])
{
          stDataBlock sDataBlock;
          ifstream inFile;
          inFile.open("configFile.txt"); // Text File containing data
          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
                              pch[0] = strtok(cDataBuffer, "$A,;");
                              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);

                              if (i == 3)          
                              {
                                        // Check if Parameter 1 as integer data type
                                        if(!sscanf(cParam1,"%d", &sDataBlock.iParam1))
                                        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;
}

Integration of SQLite3 and Netbeans C/C++ IDE

Few days back, I wanted to use SQLite database for one of my project. I spend couple of hours to find a way to integrate with Netbeans. Mayb...