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
File management
PDF
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
PPTX
file handling in c programming with file functions
PPTX
want to learn files,then just use this ppt to learn
PDF
file_c.pdf
PPTX
File management
PDF
Module 03 File Handling in C
PDF
637225560972186380.pdf
File management
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
file handling in c programming with file functions
want to learn files,then just use this ppt to learn
file_c.pdf
File management
Module 03 File Handling in C
637225560972186380.pdf

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

PPT
File_Handling in C.ppt
PPT
File_Handling in C.ppt
PPTX
MODULE 8-File and preprocessor.pptx for c program learners easy learning
PPT
WK-12-13-file f classs 12 computer science from kv.
PDF
VIT351 Software Development VI Unit5
PPTX
C-FILE MANAGEMENT.pptx
PPTX
COM1407: File Processing
PPSX
File mangement
PPT
How to do file-handling - in C language
PPTX
File handling in C
PPT
PPTX
Data Structure Using C - FILES
PPTX
File in C language
PPTX
File handling With Solve Programs
PPTX
File management
PPTX
File Handling
PPTX
File Handling
PPTX
Engineering Computers L34-L35-File Handling.pptx
PPTX
PPS-II UNIT-5 PPT.pptx
File_Handling in C.ppt
File_Handling in C.ppt
MODULE 8-File and preprocessor.pptx for c program learners easy learning
WK-12-13-file f classs 12 computer science from kv.
VIT351 Software Development VI Unit5
C-FILE MANAGEMENT.pptx
COM1407: File Processing
File mangement
How to do file-handling - in C language
File handling in C
Data Structure Using C - FILES
File in C language
File handling With Solve Programs
File management
File Handling
File Handling
Engineering Computers L34-L35-File Handling.pptx
PPS-II UNIT-5 PPT.pptx
Ad

Recently uploaded (20)

PPT
chapter_1_a.ppthduushshwhwbshshshsbbsbsbsbsh
PDF
Dozuki_Solution-hardware minimalization.
PPTX
"Fundamentals of Digital Image Processing: A Visual Approach"
PPTX
sdn_based_controller_for_mobile_network_traffic_management1.pptx
PPTX
code of ethics.pptxdvhwbssssSAssscasascc
PPTX
PLC ANALOGUE DONE BY KISMEC KULIM TD 5 .0
PPTX
Fundamentals of Computer.pptx Computer BSC
PDF
Dynamic Checkweighers and Automatic Weighing Machine Solutions
PPTX
quadraticequations-111211090004-phpapp02.pptx
PPTX
Operating System Processes_Scheduler OSS
PPTX
Lecture-3-Computer-programming for BS InfoTech
PPTX
Sem-8 project ppt fortvfvmat uyyjhuj.pptx
PDF
How NGOs Save Costs with Affordable IT Rentals
PPTX
02fdgfhfhfhghghhhhhhhhhhhhhhhhhhhhh.pptx
PPTX
Computers and mobile device: Evaluating options for home and work
PPTX
Presentacion compuuuuuuuuuuuuuuuuuuuuuuu
PPTX
title _yeOPC_Poisoning_Presentation.pptx
PPTX
5. MEASURE OF INTERIOR AND EXTERIOR- MATATAG CURRICULUM.pptx
PPTX
unit1d-communitypharmacy-240815170017-d032dce8.pptx
PPT
Hypersensitivity Namisha1111111111-WPS.ppt
chapter_1_a.ppthduushshwhwbshshshsbbsbsbsbsh
Dozuki_Solution-hardware minimalization.
"Fundamentals of Digital Image Processing: A Visual Approach"
sdn_based_controller_for_mobile_network_traffic_management1.pptx
code of ethics.pptxdvhwbssssSAssscasascc
PLC ANALOGUE DONE BY KISMEC KULIM TD 5 .0
Fundamentals of Computer.pptx Computer BSC
Dynamic Checkweighers and Automatic Weighing Machine Solutions
quadraticequations-111211090004-phpapp02.pptx
Operating System Processes_Scheduler OSS
Lecture-3-Computer-programming for BS InfoTech
Sem-8 project ppt fortvfvmat uyyjhuj.pptx
How NGOs Save Costs with Affordable IT Rentals
02fdgfhfhfhghghhhhhhhhhhhhhhhhhhhhh.pptx
Computers and mobile device: Evaluating options for home and work
Presentacion compuuuuuuuuuuuuuuuuuuuuuuu
title _yeOPC_Poisoning_Presentation.pptx
5. MEASURE OF INTERIOR AND EXTERIOR- MATATAG CURRICULUM.pptx
unit1d-communitypharmacy-240815170017-d032dce8.pptx
Hypersensitivity Namisha1111111111-WPS.ppt
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.