Ticker

6/recent/ticker-posts

C Language (File writing in C)

FILE WRITING IN C

The file writing feature allows programmers to write different types of data and information in a file.

SYNTAX TO CREATE AND OPEN A NEW FILE
file_pointer_name(file_pointer_name,"writing mode");

EXAMPLE
FILE *fp;
fp=fopen("File_name", "w");

Example Explanation: In the above example, FILE is a predefined file class in the C programming language.

 *fp is a special pointer of class 'FILE'

fopen is a predefined function of file handling function in C programming language and it is used to open an already existing file (if file does not exist then it create file first) 

File_name is a file, that you want to open.

w stands for writing mode.


PROGRAM TO WRITE A FILE IN C
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fp;  //Create a file pointer

// Open file in writing mode 
fp=fopen("test.txt","w");

//Wring content in "test.txt" file
fprintf(fp,"Welcome in first file");

printf("Data written in file successfully ...");
getch();
}




----------------------------

         OUTPUT 
----------------------------

Data written in file successfully ...






HOW TO SHOW FILE CONTENTS

We can read file contents by File reading features.
We can also see the file contents by using the MS-DOS commands.

After successfully run the C program, 
Follow the below steps to show the file contents.

1. Click on the "File" menu and then click on "DOS shell"


2. Now type the following command.
type test.txt














Post a Comment

0 Comments