Você está na página 1de 15

Files in C

ADVANTAGE
File operations
• Open a file
• Read data from the file
• Write data into the file
• Closing a file
fopen()
• fp=fopen( char *filename,char *mode)
• Return values
- file pointer if successful
- NULL if unsuccessful
mode description starts..
r open for reading beginning

w open for writing (creates file if it doesn't exist). Deletes content and overwrites the file. beginning

a open for appending (creates file if it doesn't exist) end

r+ open for reading and writing beginning

w+ open for reading and writing. Deletes content and overwrites the file. beginning

a+ open for reading and writing (append if file exists) end


fclose()
• Returns values
- 0 if successful
-EOF if unsuccessful
Disk I/O functions
• High-level unformatted character I/O
--getc(),putc(),fgetc(),fputc()
• High-level unformatted numeric I/O
--getw(),putw()
• High-level formatted I/O
--fscanf(),fprintf()
• fwrite()
Program to read and display the
content of the file named “f.c”
#include <stdio.h>
void main()
{
FILE *fp;
char ch;
fp=fopen("f.c","r");
if(fp == NULL)
{
printf("failed to open f.c");
return 0;
}
while((ch=getc(fp))!=EOF) // or while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
}
Program to copy the file named “f.c”
as “fc.c”
#include <stdio.h>
void main()
{
FILE *pt1,*pt2;
char ch;
pt1=fopen("f.c","r");
pt2=fopen("fc.c","w");
while((ch=getc(pt1))!=EOF)
{
putc(ch,pt2); // fputc(ch,pt2);
}
}
Usage of getw() and putw() and
restrictions
#include <stdio.h>
void main()
{
FILE *pt1,*pt2;
int ch;
pt1=fopen("fw.c","r");
pt2=fopen("fc.c","w");
while((ch=getw(pt1))!=EOF)
{
putw(ch,pt2);
}
}
fscanf() and fprintf()
fp=fopen("input1","w");
#include <stdio.h>
for(i=0;i<n;i++)
#include <stdlib.h>
{
void main()
printf("enter name ,age and salary\n");
{
fscanf(stdin,"%s %d %f",name,&age,&salary);
FILE *fp;
fprintf(fp,"%s %d %f",name,age,salary);
int n;
fprintf(fp,"\n");
int name[20];
}
int age;
fclose(fp);
int i;
fp=fopen("input1","r");
float salary;
for(i=0;i<n;i++)
printf("Enter employee details:\n");
{
scanf("%d",&n);
fscanf(fp,"%s %d %f",name,&age,&salary);
fprintf(stdout,"%s %d %f",name,age,salary);
}
}
Fwrite()
• size_t fwrite (const void *array, size_t size, size_t count,
FILE *stream);
• fwrite function writes a block of data to the stream. It
will write an array of count elements to the current
position in the stream. For each element, it will write
size bytes. The position indicator of the stream will be
advanced by the number of bytes written successfully.
• The function will return the number of elements
written successfully. The return value will be equal to
count if the write completes successfully. In case of a
write error, the return value will be less than count.
The following program opens a file named sample.txt,
writes a string of characters to the file, then closes it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
size_t count;
const char *str = "hello\n";
fp = fopen("sample.txt", "w");
if(fp == NULL)
{
printf("failed to open sample.txt");
return 0;
}
count = fwrite(str, 1, strlen(str), fp);
printf("Wrote %zu bytes. fclose(fp) %s.\n", count, fclose(fp) == 0 ? "succeeded" : "failed");
return 1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
int count;
const char str[3]={11,12,13};
fp = fopen("sample.txt", "w");
if(fp == NULL)
{
printf("failed to open sample.txt");
return 0;
}
count = fwrite(str, 4, 3, fp);
printf("Wrote %zu bytes. fclose(fp) %s.\n", count, fclose(fp) == 0 ?
"succeeded" : "failed"); return 1;
}
Exercise
• Assume N records
• Each record will have
• USN(integer),Name(string),Marks1(integer),
Marks2(integer), Marks3(integer)
• Storing students information in file
• Add user defined function
1. void append_record(FILE *fp)
2. int search_record(int key_usn,FILE *fp)
3. void display_records(FILE *fp)

Você também pode gostar