0% found this document useful (0 votes)
117 views12 pages

Programming in C: Unit - V File Processing

This document discusses file processing in C programming. It covers: 1) Opening, reading from, and closing files using functions like fopen(), fgets(), fclose(). 2) Writing to files using functions such as fputs(), fputc(), fprintf(). 3) Reading specific data types from files using fscanf(). 4) Reading and writing binary data to files using fread() and fwrite().

Uploaded by

yuvan raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views12 pages

Programming in C: Unit - V File Processing

This document discusses file processing in C programming. It covers: 1) Opening, reading from, and closing files using functions like fopen(), fgets(), fclose(). 2) Writing to files using functions such as fputs(), fputc(), fprintf(). 3) Reading specific data types from files using fscanf(). 4) Reading and writing binary data to files using fread() and fwrite().

Uploaded by

yuvan raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming In C

UNIT – V
FILE PROCESSING
FILE PROCESSING

 Creating a file pointer FILE *fp;


 Opening a File
fp=fopen(“a.txt”,”r”);
 Reading from a File
 fgets() , fgetc(), fscanf(),fread()
 Writing to a File
 fputs(),fputc(),fprintf(),fwrite()
 Closing a File
 fclose(fp);
Reading: char* fgets(char *str,int n,FILE *fp)

#include<stdio.h>
#include<stdlib.h>
void main()
{
char str[100];
FILE *fp;
clrscr();
fp=fopen("a.txt","r");
while(fgets(str,5,fp)!=NULL)
puts(str);
fclose(fp);
}
Return char array if successful, Else returns NULL;
Reading : int fgetc (FILE *fp)
#include<stdio.h>
w e l c o m e EOF
void main()
{
char ch; fp
FILE *fp;
clrscr();
fp=fopen("a.txt","r");
while((ch=fgetc(fp))!=EOF)
putch(ch);
fclose(fp);
getch();
}
Return same char if successful, Else returns EOF;
int fscanf(FILE *fp , char * formatstring)
#include<stdio.h>
void main()
{
char name[20];
int roll;
float avg;
FILE *fp;
clrscr();
fp=fopen("a1.txt","r");

while((fscanf(fp,"%d%s%f",&roll,name,&avg))!=EOF)
printf("\n%d\t%s\t%f",roll,name,avg);
fclose(fp);
}
Return number of items read if successful Else returns Zero
Writing: int fputs(char *str, FILE *fp)
#include<stdio.h>
void main()
{
char str[100];
FILE *fp;
clrscr();
fp=fopen("b.txt","w");
gets(str);
fputs(str,fp);
fclose(fp);
}

Return 0 if successful, Else returns EOF;


int fputc(char var,FILE *fp)
#include<stdio.h>
void main()
{
char ch;
FILE *fp;
clrscr();
fp=fopen("b.txt","w");
ch=getchar();
fputc(ch,fp);
fclose(fp);
}
Input:
char var – character to be written into the file
fp- specifies the stream where the character is to be written.
Return Value:
same character if successful Else, EOF is returned
int fprintf(fp, char format string)
#include<stdio.h>
void main()
{
char name[20];
int roll,i;
FILE *fp;
clrscr();
fp=fopen("c.txt","w");
for(i=0;i<3;i++)
{
scanf("%d%s",&roll,name);
fprintf(fp,"\n%d %s",roll,name);
}
fclose(fp);
}

Return number of items read if successful Else returns Zero


size_t fread(void *ptr, size_t size, size_t n,
FILE *fp);
Input :Takes 4 argument
ptr is the starting address of the memory block
n no. of items
Size – space occupied by each item
Stream – from where data will be read

Return Value
If it reads n items from the file and returns n.
Else returns end of the file
size_t fwrite(void *ptr, size_t size, size_t n,
FILE *fp);
Input :Takes 4 argument
ptr is the starting address of the memory block
n no. of items
Size – space occupied by each item
Stream – from where data will be read

Return Value
If it reads n items from the file and returns n.
Else returns end of the file
void main()
#include<stdio.h> {
struct student struct student s1;
{ struct student s2;
int roll; FILE *fp;
char name[12]; clrscr();
}; fp=fopen("c.bin","wb");
scanf("%d%s",&s1.roll,s1.name);
fwrite(&s1,sizeof(s1),1,fp);
fclose(fp);

fp=fopen("c.bin","rb");
//printf("%d%s",&s2.roll,s2.name);
fread(&s2,sizeof(s2),1,fp);
printf("%d%s",s2.roll,s2.name);
fclose(fp);
}
Binary FIle

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy