Tuesday, October 28, 2014

C FILE Program



Convert the Content of File to LowerCase

#include 
#include 
#include 
 
int to_lower_file(FILE *);
 
void main(int argc, char * argv[])
{
    int op = -1;
    char ch;
    FILE *fp;
    if (fp = fopen(argv[1], "r+"))
    {
        printf("FILE has been opened..!!!\n");
        op = to_lower_file(fp);
        printf(" %d \n", op);
        fclose(fp);
    }
    else
    {
        perror("Error Occured");
        printf(" %d\n ", op);
    }
}
 
int to_lower_file(FILE *f)
{
    int c;
    char ch;
    while ((ch = fgetc(f))! = EOF)
    {    
        c = (int)ch;
        if (c >= 65 && c <= 90)
        {
            ch = ch + 32;
            fseek(f, -1L, 1);
            fputc(ch, f);
        }
    }
    return 0;
}

Output
 
The function strerror returns a pointer to an error msg string whose contents are implementation defined.
the string is not modifiable and maybe overwritten by a subsequent call to the strerror function.
 
FILE has been opened..!!!
 0
 
the function strerror returns a pointer to an error msg string whose contents are implementation defined.
the string is not modifiable and maybe overwritten by a subsequent call to the strerror function.

Update Details of Employee using Files

#include 
#include 
#include 
#include 
struct emp
{
    int empid;
    char *name;
};
 
int count = 0;
void add_rec(char *a);
void display(char *a);
void update_rec(char *a);
 
void main(int argc, char *argv[])
{
    int choice;
    while (1)
    {
        printf("MENU:\n");
        printf("1.Add a record\n");
        printf("2.Display the file\n");
        printf("3.Update the record\n");
        printf("Enter your choice:");
        scanf("%d", &choice);
 
        switch(choice)
        {
        case 1:
            add_rec(argv[1]);
            break;
        case 2:
            display(argv[1]);
            break;
        case 3:
            update_rec(argv[1]);
            break;
        case 4:
            exit(0);
        default:
            printf("Wrong choice!!!\nEnter the correct choice\n");
        }
    }
}
 
void add_rec(char *a)
{
    FILE *fp;
    fp = fopen(a, "a+");
    struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
    temp->name = (char *)malloc(50*sizeof(char));
    if (fp == NULL)
        printf("Error!!!");
    else
    {
        printf("Enter the employee id\n");
        scanf("%d", &temp->empid);
        fwrite(&temp->empid, sizeof(int), 1, fp);
        printf("enter the employee name\n");
        scanf(" %[^\n]s", temp->name);
        fwrite(temp->name, 50, 1, fp);
        count++;
    }
    fclose(fp);
    free(temp);
    free(temp->name);
}
 
void display(char *a)
{
    FILE *fp;
    char ch;
    int rec = count;
    fp = fopen(a, "r");
    struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
    temp->name = (char *)malloc(50*sizeof(char));
    if (fp == NULL)
        printf("Error!!");
    else
    {
        while (rec)
        {
            fread(&temp->empid, sizeof(int), 1, fp);
            printf("%d", temp->empid);
            fread(temp->name, 50, 1, fp);
            printf(" %s\n", temp->name);
            rec--;
        }
    }
    fclose(fp);
    free(temp);
    free(temp->name);
}
 
void update_rec(char *a)
{
    FILE *fp;
    char ch, name[5];
    int rec, id, c;
    fp = fopen(a, "r+");
    struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
    temp->name = (char *)malloc(50*sizeof(char));
    printf("Enter the employee id to update:\n");
    scanf("%d", &id);
    fseek(fp, 0, 0);
    rec = count;
    while (rec)
    {
        fread(&temp->empid, sizeof(int), 1, fp);
        printf("%d", temp->empid);
        if (id == temp->empid)
        {
            printf("Enter the employee name to be updated");
            scanf(" %[^\n]s", name);
            c = fwrite(name, 50, 1, fp);
            break;
        }
        fread(temp->name, 50, 1, fp);
        rec--;
    }
    if (c == 1)
        printf("Record updated\n");
    else
        printf("Update not successful\n");
    fclose(fp);
    free(temp);
    free(temp->name);
}

Output
 
MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:1
Enter the employee id
1
enter the employee name
aaa
MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:1
Enter the employee id
2
enter the employee name
bbb
MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:3
Enter the employee id to update:
1
1Enter the employee name to be updated1bc
Record updated
MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:2
1 1bc
2 bbb
MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:4

No comments:

Post a Comment