Tuesday, October 28, 2014

C File Pro



Create a File & Store Information
 
#include 
#include  
void main()
{
    FILE *fptr;
    char name[20];
    int age;
    float salary;
 
    fptr = fopen("emp.rec", "w");
 
    if (fptr == NULL)
    {
        printf("File does not exists \n");
        return;
    }
    printf("Enter the name \n");
    scanf("%s", name);
    fprintf(fptr, "Name    = %s\n", name);
    printf("Enter the age\n");
    scanf("%d", &age);
    fprintf(fptr, "Age     = %d\n", age);
    printf("Enter the salary\n");
    scanf("%f", &salary);
    fprintf(fptr, "Salary  = %.2f\n", salary);
    fclose(fptr);
}
 
Output
 
Enter the name
raj
Enter the age
40
Enter the salary
4000000
 
Illustrate Reading of Data from a File
 
#include 
#include 
#include 
 
void main()
{
    FILE *fptr;
    char filename[15];
    char ch;
 
    printf("Enter the filename to be opened \n");
    scanf("%s", filename);
    fptr = fopen(filename, "r");
    if (fptr == NULL)
    {
        printf("Cannot open file \n");
        exit(0);
    }
    ch = fgetc(fptr);
    while (ch != EOF)
    {
        printf ("%c", ch);
        ch = fgetc(fptr);
    }
    fclose(fptr);
}
 
Output
 
Enter the filename to be opened
pgm95.c
 
#include 
#include 
void main()
{
    FILE *fptr;
    char name[20];
    int age;
    float salary;
    fptr = fopen ("emp.rec", "w"); /* open for writing*/
 
    if (fptr == NULL)
    {
        printf("File does not exists \n");
        return;
    }
    printf("Enter the name \n");
    scanf("%s", name);
    fprintf(fptr, "Name    = %s\n", name);
    printf("Enter the age \n");
    scanf("%d", &age);
    fprintf(fptr, "Age     = %d\n", age);
    printf("Enter the salary \n");
    scanf("%f", &salary);
    fprintf(fptr, "Salary  = %.2f\n", salary);
    fclose(fptr);
}
 
Delete a specific Line from a Text File
 
#include 
#include 
int main()
{
    FILE *fileptr1, *fileptr2;
    char filename[40];
    char ch;
    int delete_line, temp = 1;
 
    printf("Enter file name: ");
    scanf("%s", filename);
    fileptr1 = fopen(filename, "r");
    ch = getc(fileptr1);
 `  while (ch != EOF)
    {
        printf("%c", ch);
        ch = getc(fileptr1);
    }
    rewind(fileptr1);
    printf(" \n Enter line number of the line to be deleted:");
    scanf("%d", &delete_line);
    
    fileptr2 = fopen("replica.c", "w");
    ch = getc(fileptr1);
    while (ch != EOF)
    {
        ch = getc(fileptr1);
        if (ch == '\n')
            temp++;
            if (temp != delete_line)
            {
                putc(ch, fileptr2);
            }
    }
    fclose(fileptr1);
    fclose(fileptr2);
    remove(filename);
    rename("replica.c", filename);
    printf("\n The contents of file after being modified are as follows:\n");
    fileptr1 = fopen(filename, "r");
    ch = getc(fileptr1);
    while (ch != EOF)
    {
        printf("%c", ch);
        ch = getc(fileptr1);
    }
    fclose(fileptr1);
    return 0;
}

Output
 
Enter file name: pgm1.c
 
#include
#include 
int main()
{
    long int decimalnum, remainder, quotient;
    int i = 1, j, temp;
    char hexadecimalnum[100];
 
    printf("Enter any decimal number: ");
    scanf("%ld", &decimalnum);
 
    quotient = decimalnum;
 
    while (quotient != 0)
    {
        temp = quotient % 16;
        if (temp < 10)
            temp = temp + 48;
        else
            temp = temp + 55;
 
        hexadecimalnum[i++] = temp;
        quotient = quotient / 16;
   }
 
    printf("Equivalent hexadecimal value of decimal number %d: ", decimalnum);
    for (j = i - 1; j > 0; j--)
        printf("%c", hexadecimalnum[j]);
    return 0;
}
 
 
 Enter line number of the line to be deleted: 10
 
 #include
 #include 
int main()
{
    long int decimalnum, remainder, quotient;
    int i = 1, j, temp;
 
    printf("Enter any decimal number: ");
    scanf("%ld", &decimalnum);
 
    quotient = decimalnum;
 
    while (quotient != 0)
    {
        temp = quotient % 16;
 
        if (temp < 10)
            temp = temp + 48;
        else
            temp = temp + 55;
 
        hexadecimalnum[i++] = temp;
        quotient = quotient / 16;
   }
 
    printf("Equivalent hexadecimal value of decimal number %d: ", decimalnum);
    for (j = i - 1; j > 0; j--)
        printf("%c", hexadecimalnum[j]);
    return 0;
}

No comments:

Post a Comment