Pages

PRACTICAL 10 SOLUTION

PRACTICAL PROGRAMS:


Program 1: Program 1 : A file named data contains series of integer numbers. Write a c program to read all numbers from file and then write all odd numbers into file named “odd” and write all even numbers into file named “even”. Display all the contents of these file on screen.
#include<stdio.h>
void main()
{
FILE *f1, *f2, *f3;
    int number, i;

    printf("Contents of DATA file\n");

    f1 = fopen("data.txt", "w");

    for( i = 1; i <= 5; i++)
    {
        scanf("%d", &number);

        if(number <= -1)
        {
            break;
        }

        putw(number,f1);
    }

  fclose(f1);

f1 = fopen("data.txt", "r");
  f2 = fopen("odd.txt", "w");
    f3 = fopen("even.txt", "w");

/* Read from DATA file */

    while((number = getw(f1)) != EOF)
    {
        if(number %2 == 0)       
                {
putw(number, f3); /*Write to EVEN file*/
        }
                else       
                {
putw(number, f2);  /* Write to ODD file */
                }
        }

        fclose(f1);
        fclose(f2);
        fclose(f3);

f2 = fopen("odd.txt", "r");
    f3 = fopen("even.txt", "r");

    printf("\nContents of ODD file\n");

    while((number = getw(f2)) != EOF)
    {
        printf("%d\t", number);
    }

    printf("\nContents of EVEN file\n");

    while((number = getw(f3)) != EOF)
    {
        printf("%d\t", number);
    }
    fclose(f2);
    fclose(f3);
}
Output:
Contents of DATA file
1
2
3
4
5
Contents of ODD file
1    3    5
Contents of EVEN file
2    4



Practice Program 1 : Write a program to write a string in file.
#include<stdio.h>
void main()
{
FILE *fp;
char str[100];

fp = fopen("text.txt", "w");

printf("\nEnter data to write in file: \n");
    gets(str);

fputs(str, fp);

fclose(fp);

fp = fopen("text.txt", "r");

fgets(str, 100, fp);

printf("%s\n", str);

fclose(fp);

printf("File closed successful");

getch();
}
Output:
Enter data to write in file:
this is demo of file writing
this is demo of file writing
File closed successful

DBMS PRACTICE QUESTIONS 1

DBMS PRACTICE QUESTIONS 1 1. What is indexing? Explain different types of indexing with example. (UNIT 6) 2. What is hashing?  Explain types...