Entering data in file using file handling in C [Program]

#include <stdio.h>
main( )
{
     FILE *fp;
     char material[1000];

     fp = fopen("material.txt","w"); /* open for writing */
     printf("Enter the material to be written in the text file.\n");
     gets(material); /* For including spaces we use gets function here! */
     fprintf(fp,"%s", material);
     fclose(fp); /* close the file before ending program to ensure no errors in extreme case.. */
}

 

 

Output:

A file named material.txt will be creasted automatcially in the location where your .c file is present.

Here are the screenshots:

1.1 1.2 1.3

Program to display, add and subtract two value of times in HRS, Minutes and Seconds using structures

Code:

#include <stdio.h>
struct time1
{
    int h1,h2;
    int m1,m2;
    int s1,s2;
}time;
main( )
{
    int i,s1,s2,d1,d2,d3;
    printf("Enter the first time in Hrs, mins and seconds.\n");
    scanf("%d %d %d",&time.h1,&time.m1,&time.s1);
    printf("Enter the second time in Hrs, mins and seconds.\n");
    scanf("%d %d %d",&time.h2,&time.m2,&time.s2);
    printf("Choose the operation:\n1. Read\n2. Display\n3. Add\n4. Subtract\n5. Exit.\n");
    scanf("%d",&i);
    switch(i)
    {
        case 1:
        {
            printf("You have entered:\n1. %dHr %dMin %dSeconds.\n2. %dHr %dMin %dSeconds.",time.h1,time.m1,time.s1,time.h2,time.m2,time.s2);
            break;

        }

        case 2:
        {
            printf("You have entered:\n1. %dHr %dMin %dSeconds.\n2. %dHr %dMin %dSeconds.",time.h1,time.m1,time.s1,time.h2,time.m2,time.s2);
            break;
        }

        case 3:
        {
            s1=time.s1+time.s2;
            s2=time.m1+time.m2;
            if(time.s1+time.s2>60)
            {
                s1=time.s1+time.s2;
                s1=s1-60;
                s2++;
            }
            if(time.m1+time.m2>60)
            {
                s2=s2-60;
                time.h1++;
            }

            printf("Addition:\n%dHr %dMin %dSeconds",time.h1+time.h2,s2,s1);
            break;

        }
        case 4:
            {
                if(time.h1>time.h2)
                {

                    d1=time.h1-time.h2;
                    d2=time.m1-time.m2;
                    d3=time.s1-time.s2;
                    if(d2<0)
                    {
                        d2=d2+60;
                        d1=d1-1;
                    }
                     if(d3<0)
                    {
                        d3=d3+60;
                        d2=d2-1;
                    }
                }
                if(time.h2>time.h1)
                {
                    d1=time.h2-time.h1;
                    d2=time.m2-time.m1;
                    d3=time.s2-time.s1;

                    if(d2<0)
                    {
                        d2=d2+60;
                        d1=d1-1;
                    }
                     if(d3<0)
                    {
                        d3=d3+60;
                        d2=d2-1;
                    }

                }
                if(time.h1==time.h2)
                {
                    d1=0;
                    d2=time.m1-time.m2;
                    d3=time.s1-time.s2;
                    if(d2<0)
                    {
                        d2=0-d2;
                    }
                    if(d3<0)
                    {
                        d3=d3+60;
                        d2--;
                    }

                }
                if(time.m1==time.m2)
                {
                    d2=0;
                }
                if(time.s1==time.s2)
                {
                    d3=0;
                }
                    printf("Subtraction - %dHr %dMin %dSeconds.",d1,d2,d3);
                    break;

                }

            }
    }