2. Files
Syllabus
FILES: Introduction to files, Streams, I/O
using streams – opening a stream, closing
stream; Character input, Character output,
File position indicator, End of file and errors,
Line input and line output, Formatted I/O,
Block input and output, File type, Files and
command line arguments.
3. Files
Files:
A file is an external collection of related data
treated as a unit.
The aim of a file is to keep a record of data.
Since the contents of primary memory lost
when the computer is shut down, we need
files to store data in a more permanent form.
Additionally, the collection of data is often
too large to reside entirely in main memory
at one time.
4. Files
Files are stored in auxiliary or secondary
storage devices.
The two most common forms of secondary
storage are disk (hard disk, CD, and DVD)
and tape.
The stdio.h header file defines the file
structure;
FILE is predefined datatype. When we need
a file in our program, we declare it using the
FILE type.
FILE *sp; //sp is a pointer to type FILE
5. Files
Streams
Stream is a buffer. It will act as interface between
terminals (such as keyboard,monitor etc..)
It receives or send data as sequence of byes
There are two types of streams
1. input stream
2. output stream
Input stream will send the data to the program by taking
from keyboard or file
Output Stream will send the data to the monitor or file
by taking from the program
7. Files
Text files vs Binary files:
In text files, everything is stored in terms of text
i.e. even if we store an integer 54; it will be
stored as a 2- bytes. In a text file certain
character translation may occur.
A binary file Contains data that was written in
the same format used to store internally in main
memory. For example, the integer value 1245
will be stored in 2 bytes depending on the
machine while it will require 4 bytes in a text
file.
8. Files
Text File Binary File
Bits represent character. Bits represent a custom data.
Less prone to get corrupt as
changes reflect as soon as the
file is opened and can easily be
undone.
Can easily get corrupted, even a
single bit change may corrupt
the file.
Can store only plain text in a
file.
Can store different types of data
(image, audio, text) in a single
file.
Mostly .txt , .dat, .c and .rtf are
used as extensions to text files.
Can have any application
defined extension .jpg,.png,.exe.
20. Files
Write a C program to read and write a character
from/into a file
#include<stdio.h>
main(){
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data...");
ch=getchar();
putc(ch, fp);
fclose(fp);
21. Files
fp = fopen("one.txt", "r");
printf(“nThe data in the file is”);
ch=getc(fp);
printf("%c",ch);
fclose(fp);
}
22. Files
Write a C program to read and write text
from/into a file
#include<stdio.h>
main(){
FILE *fp;
char ch;
fp = fopen("one.txt", "w");
printf("Enter data...");
while( (ch=getchar())!=EOF) {
putc(ch, fp); }
fclose(fp);
23. Files
fp = fopen("one.txt", "r");
printf(“nThe data in the file is”);
while((ch=getc(fp))!=EOF)
printf("%c",ch);
fclose(fp);
}
24. Files
Write a program in C to copy a file in another
name.
#include<stdio.h>
main(){
FILE *fp1,*fp2;
char ch;
fp1 = fopen("one.txt", "w");
printf("Enter data...");
while((ch=getchar())!=EOF) {
fputc(ch, fp1); }
fclose(fp1);
27. Files
Writing File : fputs() function
Syntax:
int fputs(const char *s, FILE *stream)
Reading File : fgets() function
Syntax:
char* fgets(char *s, int n, FILE *stream)
28. Files
//WAP to write and read string into/from file
#include<stdio.h>
void main(){
FILE *fp;
char text[300];
fp=fopen("myfile2.txt","w");
fputs("hello c programming",fp);
fclose(fp);
fp=fopen("myfile2.txt","r");
fgets(text,200,fp);
printf("%s",text);
fclose(fp);
} Output:
hello c programming
29. Files
Formatting Input/Output functions:
fscanf() and fprintf()
If the file contains data in the form of digits,
real numbers, characters and strings, then
character input/output functions are not enough
as the values would be read in the form of
characters.
The syntax for these functions is:
int fscanf(FILE *fp, char *format,. . .);
int fprintf(FILE *fp, char *format,. . .);
30. Files
/* Write a program in C to copy a file in another
name. */
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE *fp1,*fp2;
int a,b;
fp1=fopen("one.txt", "w");
printf("Enter a and b values from key board: n");
scanf("%d%d",&a,&b); /* read from keyboard */
fprintf(fp1, "%d %d",a,b); /* write to file */
fclose(fp1);
33. Files
fseek() function is used to move the file position
to a desired location within the file.
Syntax:
int fseek(FILE *fp, long int numbytes, int
origin);
Here, fp is a file pointer returned by a call to
fopen( ),
numbytes is the number of bytes that pointer
moves from origin, which will become the new
current position.
34. Files
Origin is one of the following macros:
Origin Macro Name value
Beginning of file SEEK_SET 0
Current position SEEK_CUR 1
End of file SEEK_END 2
35. Files
Statement Meaning
fseek(fp,00,0) Go to the beginning.
fseek(fp,00,1) Stay at current position.
fseek(fp,-l,2) Go to the end of the file,
past the last character of the file.
fseek(fp,m,0) move to (m+1)th byte in
the file.
fseek(fp,m,1) Go Forward by m bytes.
fseek(fp,-m,1) Go Backward by m bytes
from the current position.
fseek(fp,-m,2) Go Backward by m
bytes from the end.
36. Files
ftell():
ftell() returns the location of the current position
of the file associated with fp.
If a failure occurs, it returns –1.
long int ftell(FILE *fp);
ftell takes a file pointer as argument and return a
number of type long that corresponds to the
current position.
37. Files
rewind( ):
The rewind( ) function moves the file position
indicator to the start of the specified file.
void rewind(FILE *fp);
It also clears the end-of-file and error flags
associated with file.
This function helps us reading the file more
than once, without having to close and open
the file.
39. Files
printf("nthe cursor position %dn",ftell(fp));
printf("character=%c",fgetc(fp));
rewind(fp);
printf("nthe cursor position %dn",ftell(fp));
printf("character=%c",fgetc(fp));
fclose(fp); } output:
the cursor position 0
character=T
the cursor position 6
character=s
the cursor position 0
character=T
40. Files
Block Input/output functions:
fread( ) and fwrite( ):
To read and write data types that are longer than 1
byte, the C file system provides two functions:
fread( ) and fwrite( ).
These functions allow the reading and writing of
blocks of any type of data.
size_t fread (void *buffer, size_t num_bytes,
size_t count, FILE *fp);
size_t fwrite (const void *buffer, size_t
num_bytes, size_t count, FILE *fp);
41. Files
For fread( ), buffer is a pointer to a region of
memory that will receive the data from the file.
For fwrite( ), buffer is a pointer to the
information that will be written to the file.
The value of count determines how many items
are read or written, with each item being num
bytes in length.
fp is a file pointer to a previously opened stream.
44. Files
Files and command line arguments.
It is a procedure of passing the arguments to the main function
from the command prompt.
/*This program reads a file specified by the user as a command
line argument and display the contents of the file on screen. */
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *fp; char *filename;
char ch;