Friday, June 20, 2014

Creating File & Writing data in file

//Exercise 1: Write a C Program to open a file named “DATA” and write a line of text in it by reading the text from the keyboard.
#include<stdio.h>
void header()
{
    printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
    printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t  Lahore , Pakistan\n\n\n");

}
int main()

{
    FILE *f;
    char a[500];
   header();
   system("pause");
   system("cls");
    f=fopen("DATA.txt" , "w");
    if (f==NULL)
    {
        printf("\nUnable to Open File ....!!");
        return 1;
    }
    printf("\nEnter any Line to Write in File : ");
    gets(a);
    while(!feof(stdin))
    {
        fprintf(f,"%s",a);
        printf("\nEnter any Line to Write in File Ctrl + Z to exit the program : ");
        gets(a);
    }
    fclose(f);
    printf("\nFile Has been created Successfully....!!!\n\n\n\n");

}

No comments:

Post a Comment