SlideShare a Scribd company logo
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");
files c programming handling in computer programming
files c programming handling in computer programming
files c programming handling in computer programming
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:UsersIIIT KOTTAYAMDesktopC Programscfans.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:UsersadminDesktopfile1.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.
files c programming handling in computer programming

More Related Content

PPTX
want to learn files,then just use this ppt to learn
PPT
PPTX
Programming C- File Handling , File Operation
PPT
File handling
PPTX
file handling in c programming with file functions
PPTX
File in C language
PPTX
PPS PPT 2.pptx
PDF
VIT351 Software Development VI Unit5
want to learn files,then just use this ppt to learn
Programming C- File Handling , File Operation
File handling
file handling in c programming with file functions
File in C language
PPS PPT 2.pptx
VIT351 Software Development VI Unit5

Similar to files c programming handling in computer programming (20)

PPT
Files_in_C.ppt
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPT
File management and handling by prabhakar
PPT
File handling in c
PPT
C-Programming Chapter 5 File-handling-C.ppt
PPTX
PPS-II UNIT-5 PPT.pptx
PPT
Lecture 20 - File Handling
PPTX
File Handling in C Programming for Beginners
PDF
637225560972186380.pdf
PPTX
Concept of file handling in c
PDF
Module 03 File Handling in C
PPT
C-Programming Chapter 5 File-handling-C.ppt
DOCX
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
PPTX
File handling in c
PPTX
File Handling ppt.pptx shjd dbkd z bdjdb d
PPT
file_handling_in_c.ppt
PPTX
Presentation of file handling in C language
PPT
How to do file-handling - in C language
PPTX
C-Programming File-handling-C.pptx
Files_in_C.ppt
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
File management and handling by prabhakar
File handling in c
C-Programming Chapter 5 File-handling-C.ppt
PPS-II UNIT-5 PPT.pptx
Lecture 20 - File Handling
File Handling in C Programming for Beginners
637225560972186380.pdf
Concept of file handling in c
Module 03 File Handling in C
C-Programming Chapter 5 File-handling-C.ppt
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
File handling in c
File Handling ppt.pptx shjd dbkd z bdjdb d
file_handling_in_c.ppt
Presentation of file handling in C language
How to do file-handling - in C language
C-Programming File-handling-C.pptx
Ad

Recently uploaded (20)

PDF
Lifting Equipment Inspection Checklist with eAuditor Audits & Inspections
PPTX
ATL_Arduino_Complete_Presentation_AI_Visuals.pptx
PPTX
making presentation that do no stick.pptx
PPTX
dhcp concept.pptxfeegrvewfegrgerhtrhtrhredew
PPTX
kvjhvhjvhjhjhjghjghjgjhgjhgjhgjhgjhgjhgjhgjh
PPTX
Disorders of the anterior horn cells.pptx
PPTX
title _yeOPC_Poisoning_Presentation.pptx
PPTX
PROGRAMMING-QUARTER-2-PYTHON.pptxnsnsndn
PDF
Layer23-Switch.com The Cisco Catalyst 9300 Series is Cisco’s flagship stackab...
PDF
YKS Chrome Plated Brass Safety Valve Product Catalogue
PPT
L1-Intro.ppt nhfjkhghjjnnnmkkjhigtyhhjjj
PPT
FABRICATION OF MOS FET BJT DEVICES IN NANOMETER
PPTX
Sem-8 project ppt fortvfvmat uyyjhuj.pptx
PDF
Core Components of IoT, The elements need for IOT
PDF
How NGOs Save Costs with Affordable IT Rentals
PPTX
IOT piching HEALTH MONITORING SYSTEM USING ARDUINO123.pptx
PPTX
udi-benefits-ggggggggfor-healthcare.pptx
PDF
DOC-20250802-WA0013._20250802_161719_0000.pdf
PPTX
Eco-DROPLETS (1).pptx {watering smarter,not harder
PDF
Prescription1 which to be used for periodo
Lifting Equipment Inspection Checklist with eAuditor Audits & Inspections
ATL_Arduino_Complete_Presentation_AI_Visuals.pptx
making presentation that do no stick.pptx
dhcp concept.pptxfeegrvewfegrgerhtrhtrhredew
kvjhvhjvhjhjhjghjghjgjhgjhgjhgjhgjhgjhgjhgjh
Disorders of the anterior horn cells.pptx
title _yeOPC_Poisoning_Presentation.pptx
PROGRAMMING-QUARTER-2-PYTHON.pptxnsnsndn
Layer23-Switch.com The Cisco Catalyst 9300 Series is Cisco’s flagship stackab...
YKS Chrome Plated Brass Safety Valve Product Catalogue
L1-Intro.ppt nhfjkhghjjnnnmkkjhigtyhhjjj
FABRICATION OF MOS FET BJT DEVICES IN NANOMETER
Sem-8 project ppt fortvfvmat uyyjhuj.pptx
Core Components of IoT, The elements need for IOT
How NGOs Save Costs with Affordable IT Rentals
IOT piching HEALTH MONITORING SYSTEM USING ARDUINO123.pptx
udi-benefits-ggggggggfor-healthcare.pptx
DOC-20250802-WA0013._20250802_161719_0000.pdf
Eco-DROPLETS (1).pptx {watering smarter,not harder
Prescription1 which to be used for periodo
Ad

files c programming handling in computer programming

  • 1. 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.
  • 2. Types of Files • When dealing with files, there are two types of files you should know about: • Text files • Binary files
  • 3. 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
  • 4. 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;
  • 5. 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");
  • 9. 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.
  • 10. 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);
  • 11. To write data to file #include<stdio.h> int main() { FILE *fp; char str[100]; fp=fopen("C:UsersIIIT KOTTAYAMDesktopC Programscfans.txt","w"); printf("enter string to write to the file"); gets(str); fputs(str,fp); fclose(fp); return 0; }
  • 12. 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; }
  • 13. fgetc() • fgetc is a function that gets one character from a file • This function is declared in stdio.h • Declaration: • fgetc(FILE *pointer);
  • 14. 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);
  • 15. 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 }
  • 16. Example- To read data from file and display it to screen #include<stdio.h> void main(){ FILE*fp; char ch; fp=fopen("C:UsersadminDesktopfile1.txt","r");//openingfile ch=fgetc(fp); // reading data from file printf("%c",ch); // write data on the screen fclose(fp);//closing file }
  • 17. Example 1: Write to a text file #include <stdio.h> int main() { int num; FILE *fptr; fptr = fopen("program.txt","w");
  • 18. if(fptr == NULL) { printf("Error!"); exit(1); } printf("Enter num: "); scanf("%d",&num); fprintf(fptr,"%d",num); fclose(fptr); return 0; }
  • 19. 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.
  • 20. 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");
  • 21. // Program exits if the file pointer returns NULL. exit(1); } fscanf(fptr,"%d", &num); printf("Value of n=%d", num); fclose(fptr); return 0; }
  • 22. 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.