files c programming handling in computer programming

chatakonduyaswanth24 15 views 23 slides Nov 13, 2024
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

nil


Slide Content

Why files are needed? When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates. If you have to enter a large number of data, it will take a lot of time to enter them all. However, if you have a file containing all the data, you can easily access the contents of the file using a few commands in C. You can easily move your data from one computer to another without any changes.

Types of Files When dealing with files, there are two types of files you should know about: Text files Binary files

File Operations In C, you can perform four major operations on files, either text or binary: Creating a new file Opening an existing file Closing a file Reading from and writing information to a file

Working with files When working with files, you need to declare a pointer of type file. This declaration is needed for communication between the file and the program. FILE * fptr ;

Opening a file - for creation and edit Opening a file is performed using the fopen () function defined in the stdio.h header file. The syntax for opening a file in standard I/O is: ptr = fopen (" file_name","mode "); For example, fopen (" newprogram.txt","w ");

Closing a File The file (both text and binary) should be closed after reading/writing. Closing a file is performed using the fclose () function. fclose ( fptr ); Here, fptr is a file pointer associated with the file to be closed.

fgets () & fputs () fgets () gets a string from a file   fputs () write a string to a file Syntax:  fputs ( string,fp ); fgets ( buffer,No.of bytes,fp );

To write data to file #include< stdio.h > int main() { FILE * fp ; char str [100]; fp = fopen ("C:\\Users\\IIIT KOTTAYAM\\Desktop\\C Programs\\ cfans.txt","w "); printf ("enter string to write to the file"); gets( str ); fputs ( str,fp ); fclose ( fp ); return 0; }

To read from a file #include< stdio.h > int main() { FILE * fp ; char str [100],str1[100]; fp = fopen ("myfile2.txt","w"); printf ("enter string to write to the file"); gets( str ); fputs ( str,fp ); fclose ( fp ); fp = fopen ("myfile2.txt","r"); fgets (str1,100,fp); printf ("Data from the file is: "); printf ("%s",str1); return 0; }

fgetc () fgetc is a function that gets one character from a file This function is declared in stdio.h Declaration: fgetc (FILE *pointer);

fputc () fputc is a function that outputs or writes a character to a file This function is declared in STDIO.H Declaration or Syntax: putc (char c, FILE *pointer);

Example- To read data from console and write it to file #include< stdio.h > void main(){ FILE* fp ; char ch ; fp = fopen ("file1.txt","w");// openingfile ch = getchar (); // reading data from console fputc ( ch,fp );//writing single character into file printf ("%c", ch ); // write data on the screen fclose ( fp );//closing file } 

Example- To read data from file and display it to screen #include< stdio.h > void main(){ FILE* fp ; char ch ; fp = fopen ("C:\\Users\\admin\\Desktop\\file1.txt","r");// openingfile ch = fgetc ( fp ); // reading data from file printf ("%c", ch ); // write data on the screen fclose ( fp );//closing file }  

Example 1: Write to a text file #include < stdio.h > int main() { int num ; FILE * fptr ; fptr = fopen (" program.txt","w ");

if( fptr == NULL) { printf ("Error!"); exit(1); } printf ("Enter num : "); scanf ("%d",& num ); fprintf ( fptr ,"%d", num ); fclose ( fptr ); return 0; }

Explanation This program takes a number from the user and stores in the file program.txt. After you compile and run this program, you can see a text file program.txt created in your computer. When you open the file, you can see the integer you entered.

Example 2: Read from a text file #include < stdio.h > #include < stdlib.h > int main() { int num ; FILE * fptr ; if (( fptr = fopen (" program.txt","r ")) == NULL){ printf ("Error! opening file");

// Program exits if the file pointer returns NULL. exit(1); } fscanf ( fptr ,"%d", & num ); printf ("Value of n=%d", num ); fclose ( fptr ); return 0; }

Explanation This program reads the integer present in the program.txt file and prints it onto the screen. If you successfully created the file from Example 1, running this program will get you the integer you entered. Other functions like fgetchar (), fputc () etc. can be used in a similar way.
Tags