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)

PPTX
A Clear View_ Interpreting Scope Numbers and Features
PPTX
Embedded for Artificial Intelligence 1.pptx
PPTX
unit1d-communitypharmacy-240815170017-d032dce8.pptx
PPT
Hypersensitivity Namisha1111111111-WPS.ppt
DOCX
A PROPOSAL ON IoT climate sensor 2.docx
PPTX
Prograce_Present.....ggation_Simple.pptx
PPTX
PLC ANALOGUE DONE BY KISMEC KULIM TD 5 .0
PDF
Smarter Security: How Door Access Control Works with Alarms & CCTV
PPT
FABRICATION OF MOS FET BJT DEVICES IN NANOMETER
PDF
PPT Determiners.pdf.......................
PDF
Dynamic Checkweighers and Automatic Weighing Machine Solutions
PPTX
Entre CHtzyshshshshshshshzhhzzhhz 4MSt.pptx
PPTX
PROGRAMMING-QUARTER-2-PYTHON.pptxnsnsndn
PPTX
Sem-8 project ppt fortvfvmat uyyjhuj.pptx
PPTX
code of ethics.pptxdvhwbssssSAssscasascc
PPTX
Presentacion compuuuuuuuuuuuuuuuuuuuuuuu
PPTX
Wireless and Mobile Backhaul Market.pptx
PPTX
"Fundamentals of Digital Image Processing: A Visual Approach"
PPT
Lines and angles cbse class 9 math chemistry
PPTX
Embeded System for Artificial intelligence 2.pptx
A Clear View_ Interpreting Scope Numbers and Features
Embedded for Artificial Intelligence 1.pptx
unit1d-communitypharmacy-240815170017-d032dce8.pptx
Hypersensitivity Namisha1111111111-WPS.ppt
A PROPOSAL ON IoT climate sensor 2.docx
Prograce_Present.....ggation_Simple.pptx
PLC ANALOGUE DONE BY KISMEC KULIM TD 5 .0
Smarter Security: How Door Access Control Works with Alarms & CCTV
FABRICATION OF MOS FET BJT DEVICES IN NANOMETER
PPT Determiners.pdf.......................
Dynamic Checkweighers and Automatic Weighing Machine Solutions
Entre CHtzyshshshshshshshzhhzzhhz 4MSt.pptx
PROGRAMMING-QUARTER-2-PYTHON.pptxnsnsndn
Sem-8 project ppt fortvfvmat uyyjhuj.pptx
code of ethics.pptxdvhwbssssSAssscasascc
Presentacion compuuuuuuuuuuuuuuuuuuuuuuu
Wireless and Mobile Backhaul Market.pptx
"Fundamentals of Digital Image Processing: A Visual Approach"
Lines and angles cbse class 9 math chemistry
Embeded System for Artificial intelligence 2.pptx
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.