Hi,
Need help to write the below mentioned program.


Create an employee struct with following fields
EMP_NAME
EMP_ID
DOJ (Date of joining)
Read in the values from a file and store them in the above structure. Print the employee details if an EMP_ID is given.


Regards,
Shaju

Recommended Answers

All 9 Replies

What do you need help with? Specifically?

What do you need help with? Specifically?

Below is my program, which is not working fine.. Its not writing the input properly to the file. Kindly help me out to solve this.

/* Q3. Program to get the employee details when the respective ID is given*/

#include<stdio.h>
#include<conio.h>
#include"FileFunc.h"

struct employee
{
    char name[25];
    char doj[25];
    int empid;
};

void main()
{
    struct employee e[3];
    FILE *fp;
    int i,num;
    int k=0;
    /*Read the details of three employees */
    fp=fopen("empfile.txt","w");
    for(i=0;i<3;i++)
    {
        printf("Enter the empid,name and doj");
        scanf("%d %s %s",&e[i].empid, e[i].name, e[i].doj);
    }
    //write all the three records in the file
    fwrite(&e,sizeof(struct employee),3,fp);

    fclose(fp);
    //CreateINP_File();

    fp=fopen("empfile.txt","r");
    //read all the three records from the file
    fread(&e,sizeof(struct employee),3,fp);
    printf("\nEnter the emp id to see the details\n");
    scanf("%d",&num);
    for(i=0;i<3;i++)
    {
        if(e[i].empid==num)
        {

        printf("Employee id = %d\nName = %s \nDate of joining = %s\n",e[i].empid,e[i].name,e[i].doj);
        }
    }
    fclose(fp);
    getch();

First of all some housekeeping:

1. In future, when posting code, please use code tags - see this link:
http://www.daniweb.com/forums/thread93280.html

2. void main() is bad - use either one of the following for the signature of main:
- int main()
- int main(void)
- int main(int argc, char *argv[])
- int main(int argc, char **argv)

Now on to your code. I compiled it and it worked OK. Your formatting of output could be improved and your use of scanf for input will give you grief if you decide to you use more than two words for a name - but other than that it worked for me.

When you say it doesn't write the input properly to the file, what exactly do you mean? In my test run, it produces a file with a size of 168 bytes = 56 bytes per record (not 54 - due to boundary alignment of the int)

First of all some housekeeping:

1. In future, when posting code, please use code tags - see this link:
http://www.daniweb.com/forums/thread93280.html

2. void main() is bad - use either one of the following for the signature of main:
- int main()
- int main(void)
- int main(int argc, char *argv[])
- int main(int argc, char **argv)

Now on to your code. I compiled it and it worked OK. Your formatting of output could be improved and your use of scanf for input will give you grief if you decide to you use more than two words for a name - but other than that it worked for me.

When you say it doesn't write the input properly to the file, what exactly do you mean? In my test run, it produces a file with a size of 168 bytes = 56 bytes per record (not 54 - due to boundary alignment of the int)

Hi,

when we write on to the file using structure, it does nt write the inputs to the file properly, it stores some unreadable characters.
Inputs given to the files:
1
xxxx
2009
2
yyyy
2008
3
zzzz
2007

File output:
xxxx ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ2009 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ yyyy ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ2008 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ zzzz ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ2007 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ


Hope you get where is the struck on this program...

Help me!!!!

I don't understand what your problem is - what you're observing in your file is expected based on the program you have written.

Let's take a look at the structure:

struct employee {
    char name[25];
    char doj[25];
    int empid;
};

You have allowed 25 characters for the name and doj members and an int for empid (most likely 4 bytes). So if you enter "John" for the name member, the first 5 bytes (including the '\0') will contain 'J', 'o', 'h', 'n', '\0' and the rest of the 20 bytes will contain garbage which I suspect is what you're seeing as unreadable characters. The same will occur for the doj member.

If you don't want the "unreadable" characters in your file, you will have to initialize the members of your structure prior to loading them with data and writing to your file.

If you don't want the "unreadable" characters in your file, you will have to initialize the members of your structure prior to loading them with data and writing to your file.

At any rate, since you are writing "raw" ints to your file, these fields will still appear as "unreadable" characters when viewing the file. For example, an empid = 100001 will be stored in the file as hex A1 86 01 00 (for a 4 byte int).

If you want all fields to be readable within the file, then you would have to store them as "strings".

I really don't see what the problem is really, since when you read the records from the file, the fields will either be interpreted as strings and the int will be converted on output (e.g. via printf).

If you want to verify that your program is writing data correctly to the file, use a hex browser/editor to view the contents of the file.

Just my 2.5 cents worth. ;)

At any rate, since you are writing "raw" ints to your file, these fields will still appear as "unreadable" characters when viewing the file. For example, an empid = 100001 will be stored in the file as hex A1 86 01 00 (for a 4 byte int).

If you want all fields to be readable within the file, then you would have to store them as "strings".

I really don't see what the problem is really, since when you read the records from the file, the fields will either be interpreted as strings and the int will be converted on output (e.g. via printf).

If you want to verify that your program is writing data correctly to the file, use a hex browser/editor to view the contents of the file.

Just my 2.5 cents worth. ;)

1. Do you say that my program is fine? If there is need of code changes in my program can you plz tell me the changes that need to be made to avoid unreadable character in file...

2. which is the better function to read the contents of the file and store them in structure if the file already exists? I mean fgets() or fread()???

3. I have another program, can u code it?
Create a struct named emp_details with the following fields
EMP_ID
ADDR1
ADDR2
PHONE
Read in the values from a file and store them in the above structure. Create a new structure with employee name, id and address details. Copy the values from structures created in question 3 and 4 to this new structure and print the result...

At any rate, since you are writing "raw" ints to your file, these fields will still appear as "unreadable" characters when viewing the file. For example, an empid = 100001 will be stored in the file as hex A1 86 01 00 (for a 4 byte int).

If you want all fields to be readable within the file, then you would have to store them as "strings".

I really don't see what the problem is really, since when you read the records from the file, the fields will either be interpreted as strings and the int will be converted on output (e.g. via printf).

If you want to verify that your program is writing data correctly to the file, use a hex browser/editor to view the contents of the file.

Just my 2.5 cents worth. ;)

1. Do you say that my program is fine? If there is need of code changes in my program can you plz tell me the changes that need to be made to avoid unreadable character in file...

2. which is the better function to read the contents of the file and store them in structure if the file already exists? I mean fgets() or fread()???

3. I have another program, can u code it?
Create a struct named emp_details with the following fields
EMP_ID
ADDR1
ADDR2
PHONE
Read in the values from a file and store them in the above structure. Create a new structure with employee name, id and address details. Copy the values from structures created in question 3 and 4 to this new structure and print the result...


4. Create a struct named emp_details with the following fields
EMP_ID
ADDR1
ADDR2
PHONE
Read in the values from a file and store them in the above structure. Create a new structure with employee name, id and address details. Copy the values from structures created in question 3(my very first program which we are discussing) and 4 to this new structure and print the result...

1. Do you say that my program is fine? If there is need of code changes in my program can you plz tell me the changes that need to be made to avoid unreadable character in file...

2. which is the better function to read the contents of the file and store them in structure if the file already exists? I mean fgets() or fread()???

3. I have another program, can u code it?
Create a struct named emp_details with the following fields
EMP_ID
ADDR1
ADDR2
PHONE
Read in the values from a file and store them in the above structure. Create a new structure with employee name, id and address details. Copy the values from structures created in question 3 and 4 to this new structure and print the result...

1. I said your program works OK for what you want to do. I already told you what you need to do if you don't want "unreadable" characters in your file. Read my previous posts again. I think you're getting too hung up on this "unreadable" characters - meh .. your problem.

2. fgets() or fread()? It really depends on what you need to do. Are you reading lines of text file delimited by CRLF or are you reading blocks of data?

3. Can I code it? Yes I can. Am I going to do it? Heck no - but members on this forum will help if you show some effort first. It's not that much different from what you're currently doing with the program you posted in this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.