Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here is the file sample.

########################################### Configuration File for Employee Information ##########################
#################################
  ID = 101
  Name = Anjum ####### She works for Innobox 
  sal= 30000# per month
  Experience = 5.4 
#################
ID = 102
  Name = Satya      ####### He works for Innobox 
  sal= 60000# per month
  Experience = 7.4 
####################




	ID = 103
  Name =     Ramana ####### 
  sal= 70000# per month
  Experience = 8.4
####################


	ID = 104
  Name = Trinadh ####### ## He works for Innobox
  sal     =        60000# per month
  Experience = 7.0
######################



i need to parse it and store all data in a structure. and print.

structure members are-
1.ID
2.name
3.salary
4.experience

Any claue?
Posted
Updated 28-Oct-14 11:33am
v4
Comments
OriginalGriff 28-Oct-14 15:46pm    
And?
What have you tried?
Where are you stuck?
What help do you need?
XSCHB_000 28-Oct-14 16:18pm    
I am required to create a parser for parsing this file format. After parsing the input file and creating records, I am required to implement querying on these records.

I need to display the output based on the command line option.
options you can have are "-a" to display all records.
"-i number" to display a record of specific id.

code what i wrote:

#include
#include
#include
#include
#include

#define EMPLOYE_FILE_NAME "/home/supratik/Empconfig.txt"
#define ID_PARSE_DELIMITER "ID"
#define NAME_PARSE_DELIMITER "Name"
#define SALARY_PARSE_DELIMITER "sal"
#define EXPERIENCE_PARSE_DELIMITER "Experience"
#define MAX_STR_SIZE 1024
#define MAX_ID_LENGTH 3
#define MAX_NAME_LENGTH 8
#define MAX_SALARY_LENGTH 6
#define MAX_EXPERIENCE_LENGTH 5

#define MAX_LINE_READ 100

#define MAX_EMPLOYEE_RECORD 4

//static int parse_string( char *buffer );
static int Parse_file( void );
static int parse_ID( char *buffer );
static int parse_Name( char *buffer );
static int parse_Exp( char *buff);
static int parse_salary( char *buff);
static void Read_file_for_ID( FILE *fp,char *str);
static void Read_file_for_Name( FILE *fp,char *str);
static void Read_file_for_Salary( FILE *fp,char *str);
static void Read_file_for_Experience( FILE *fp,char *str);

//employee structure

struct employee
{
int id; //ID of employee
char name[30]; //Name of employee
int sal; // Salary of employee
float exp; //Experience of employee
};

struct employee record[ MAX_EMPLOYEE_RECORD -1 ];
int record_index = 0 ;

void print_record()
{
printf("Here are all records\n");
for(; record_index <=MAX_EMPLOYEE_RECORD-1; record_index++)
{
printf("ID of employee=%d\t",record[record_index].id);
printf("NAME of employee=%s\t",record[record_index].name);
printf("Salary of employee=%d\t",record[record_index].sal);
printf("experience of employee=%.1f\n",record[record_index].exp);
}
return;
}

void print_individual_record(int index)
{
printf("Here are the record\n");
printf("ID of employee=%d\t",record[index-1].id);
printf("NAME of employee=%s\t",record[index-1].name);
printf("Salary of employee=%d\t",record[index-1].sal);
printf("experience of employee=%.1f\n",record[index-1].exp);
return;
}

int main(int argc, char *argv)
{
int rc;
int value;
opterr = 0;
int index;

if(argc < 2){
printf("%s():[%d]:Invalid command, need to give more argument\n",__func__,__LINE__);
return -1;
}
while( 1)
{
//printf("We are Here\n");
static struct option long_options[] =
{
/* These options don’t set a flag.
We distinguish them by their indices. */
{"index", required_argument, 0, 'i'},
{"display", no_argument, 0, 'a'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
value = getopt_long (argc, argv, "ai:",
long_options, &option_index);
/* Detect the end of the options. */
if (value == -1)
break;
// printf("%d we are here\n",__LINE__);
switch (value)
{
case 'a':
// printf ("option -a\n");
rc = Parse_file();
if(!rc){
print_record();
}
break;

case 'i':
// printf ("option -d with value `%s'\n", optarg);
index = atoi(optarg);
// printf("%d\n",index);
rc = Parse_file();
if(!rc){
print_individual_record(index);
}
break;
default:
printf("%s():[%d]: OOPS!!bad argument, please enter valid argument\t -a\n",__func__,__LINE__);
}
}

}


static void Read_file_for_ID( FILE *fp, char *str)
{
char temp_str[ MAX_STR_SIZE ];
int line_num = 0;

memset(temp_str, 0, sizeof(temp_str));
while ((fgets(temp_str,1024,fp))!= NULL)
{
if((strstr(temp_str,str)) != NULL){
// printf("A match found on line: %d\n", line_num);
// printf("%s():%d: DEBUG: string is:%s\n",__func__,__LINE__,temp_str);
// record_index++;
parse_ID( temp_str );
}
line_num++;
}
return;
}

static void Read_file_for_Name( FILE *fp, char *str)
{
char temp_str[MAX_STR_SIZE];
int line_num = 0;
memset( temp_str, 0 , sizeof(temp_str));
while ((fgets(temp_str,1024,fp))!= NULL)
{
if((strstr(temp_str,str)) != NULL){
// printf("A match found on line: %d\n", line_num);
// printf("%s():%d: DEBUG: string is:%s\n",__fu
Sergey Alexandrovich Kryukov 28-Oct-14 15:47pm    
Please stop re-posting it, better write a proper question. I already saw this "question".
The question is unclear. Just one example is not the description of input format, set aside output (parse to what?)
What do you mean "how"? By doing appropriate software development work.
What have you tried so far?
—SA
XSCHB_000 28-Oct-14 16:17pm    
I am required to create a parser for parsing this file format. After parsing the input file and creating records, I am required to implement querying on these records.

I need to display the output based on the command line option.
options you can have are "-a" to display all records.
"-i number" to display a record of specific id.

code what i wrote:

#include
#include
#include
#include
#include

#define EMPLOYE_FILE_NAME "/home/supratik/Empconfig.txt"
#define ID_PARSE_DELIMITER "ID"
#define NAME_PARSE_DELIMITER "Name"
#define SALARY_PARSE_DELIMITER "sal"
#define EXPERIENCE_PARSE_DELIMITER "Experience"
#define MAX_STR_SIZE 1024
#define MAX_ID_LENGTH 3
#define MAX_NAME_LENGTH 8
#define MAX_SALARY_LENGTH 6
#define MAX_EXPERIENCE_LENGTH 5

#define MAX_LINE_READ 100

#define MAX_EMPLOYEE_RECORD 4

//static int parse_string( char *buffer );
static int Parse_file( void );
static int parse_ID( char *buffer );
static int parse_Name( char *buffer );
static int parse_Exp( char *buff);
static int parse_salary( char *buff);
static void Read_file_for_ID( FILE *fp,char *str);
static void Read_file_for_Name( FILE *fp,char *str);
static void Read_file_for_Salary( FILE *fp,char *str);
static void Read_file_for_Experience( FILE *fp,char *str);

//employee structure

struct employee
{
int id; //ID of employee
char name[30]; //Name of employee
int sal; // Salary of employee
float exp; //Experience of employee
};

struct employee record[ MAX_EMPLOYEE_RECORD -1 ];
int record_index = 0 ;

void print_record()
{
printf("Here are all records\n");
for(; record_index <=MAX_EMPLOYEE_RECORD-1; record_index++)
{
printf("ID of employee=%d\t",record[record_index].id);
printf("NAME of employee=%s\t",record[record_index].name);
printf("Salary of employee=%d\t",record[record_index].sal);
printf("experience of employee=%.1f\n",record[record_index].exp);
}
return;
}

void print_individual_record(int index)
{
printf("Here are the record\n");
printf("ID of employee=%d\t",record[index-1].id);
printf("NAME of employee=%s\t",record[index-1].name);
printf("Salary of employee=%d\t",record[index-1].sal);
printf("experience of employee=%.1f\n",record[index-1].exp);
return;
}

int main(int argc, char *argv)
{
int rc;
int value;
opterr = 0;
int index;

if(argc < 2){
printf("%s():[%d]:Invalid command, need to give more argument\n",__func__,__LINE__);
return -1;
}
while( 1)
{
//printf("We are Here\n");
static struct option long_options[] =
{
/* These options don’t set a flag.
We distinguish them by their indices. */
{"index", required_argument, 0, 'i'},
{"display", no_argument, 0, 'a'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
value = getopt_long (argc, argv, "ai:",
long_options, &option_index);
/* Detect the end of the options. */
if (value == -1)
break;
// printf("%d we are here\n",__LINE__);
switch (value)
{
case 'a':
// printf ("option -a\n");
rc = Parse_file();
if(!rc){
print_record();
}
break;

case 'i':
// printf ("option -d with value `%s'\n", optarg);
index = atoi(optarg);
// printf("%d\n",index);
rc = Parse_file();
if(!rc){
print_individual_record(index);
}
break;
default:
printf("%s():[%d]: OOPS!!bad argument, please enter valid argument\t -a\n",__func__,__LINE__);
}
}

}


static void Read_file_for_ID( FILE *fp, char *str)
{
char temp_str[ MAX_STR_SIZE ];
int line_num = 0;

memset(temp_str, 0, sizeof(temp_str));
while ((fgets(temp_str,1024,fp))!= NULL)
{
if((strstr(temp_str,str)) != NULL){
// printf("A match found on line: %d\n", line_num);
// printf("%s():%d: DEBUG: string is:%s\n",__func__,__LINE__,temp_str);
// record_index++;
parse_ID( temp_str );
}
line_num++;
}
return;
}

static void Read_file_for_Name( FILE *fp, char *str)
{
char temp_str[MAX_STR_SIZE];
int line_num = 0;
memset( temp_str, 0 , sizeof(temp_str));
while ((fgets(temp_str,1024,fp))!= NULL)
{
if((strstr(temp_str,str)) != NULL){
// printf("A match found on line: %d\n", line_num);
// printf("%s():%d: DEBUG: string is:%s\n",__fu
PIEBALDconsult 28-Oct-14 15:53pm    
There's a decent change that a Regular Expression can do most of that.

No I don't like to dive trough all the code in your comment.

But this should give you a first idea:
a.) Read line by line
b.) Delete for each line from first occurence of "#" to the end of line, becuase "#" seems to start comment.
c.) In case remaing line is not empty and contains a "=" then evaluate remaining "a = b"

Not a solution which can be used imediatelly, but maybe some hints about the logic.

See solution #3 for implementation hints.
 
Share this answer
 
v3
Comments
Maciej Los 28-Oct-14 17:37pm    
Very good tutorial ;)
+5!
[no name] 28-Oct-14 17:39pm    
Really not kidding? If no then thank you Maciej! Regards, Bruno
Maciej Los 28-Oct-14 17:41pm    
Check the Reputation to find out i'm kidding or not ;)
Cheers,
Maciej
[no name] 28-Oct-14 17:51pm    
Thanks, Bruno
Andreas Gieriet 28-Oct-14 19:20pm    
My 5!
Cheers
Andi
Your language might be defined in "pseudo" EBNF like
txt
File       = { Skip } { Record } .
Record     = Field<ID> Field<Name> Field<Sal> Field<Experience> .
Field<Key> = { WS } Key { WS } "=" { WS } Text { Skip } .
Skip       = { WS } [HASH { ANY-CHAR-EXCEPT-NL } ] NL .
Text       = ANY-CHAR-EXCEPT-HASH-WS { ANY-CHAR-EXCEPT-HASH-WS } .

Follow the steps as decribed by Bruno Sprecher in Solution #2.
You might use the C-functions

  • strcspn
  • strtok
  • strncpy

Cheers
Andi
 
Share this answer
 
Comments
[no name] 28-Oct-14 19:28pm    
My 5 and a link to your solution and a sorry for And"y" instead of Andi. Regards, Bruno
Andreas Gieriet 29-Oct-14 12:18pm    
Thanks for your 5!
Cheers
Andi
PS: No problem with the "wrong" naming ;-)
Sergey Alexandrovich Kryukov 29-Oct-14 0:08am    
5ed. Well, "pseudo" EBNF is a bit too much. :-) There is no "E" in this kind of BNF. Not sure OP can understand it...
—SA
Andreas Gieriet 29-Oct-14 12:22pm    
Thanks for your 5, Sergey!
"pseudo" due to the "generic" production (Field<key>).
*E*BNF since to my understanding, repetitions ({ ... }) do not exist on BNF - they are only available as recursion in BNF.
Cheers
Andi
Sergey Alexandrovich Kryukov 29-Oct-14 13:46pm    
Well, maybe. It's that my impression was more like pseudo-BNF. You made it up, anyway. :-)
Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900