Friday, 16 January 2015

File Structures - C program to create a new file and write some data into it.


This is a C Program which creates a new file and writes some data into it.

We will use a file pointer to create a new file in write mode then we enter some data into it.

Then we read  that data until end of file is reached and place that data in the  file we just created.

PROGRAM :
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;
    char ch;
    fp = fopen("sample.txt","w");
    printf("Write some text here !\n");
    while((ch=getchar())!=EOF)
        putc(ch,fp);
    fclose(fp);
    return 0;
}

OUTPUT : 


Now open sample.txt file in the directory where you saved the  c source file



No comments:

Post a Comment