2. ïĄ A collection of data or information that has a are
stored on computer known as file
ïĄ A file is collection of bytes stored on a secondary
storage device.
ïĄ There are different types of file
ï§ Data files
ï§ Text files
ï§ Program files
ï§ Directory files
ïĄ Different types of file stored different types of
information
3. ïĄA file has a beginning and an end, it has a
current position, typically defined as on many
bytes from the beginning.
ïĄ You can move the current position to any
other point in file.
ïĄ A new current position can be specified as an
offset from the beginning the file.
4. ïĄA stream is an abstract representation of any
external source or destination for data, so the
keyword, the command line on your display
and files on disk are all examples of stream.
ïĄ âCâ provides numbers of function for reading
and writing to or from the streams on any
external devices.
5. ïĄA stream is a series of bytes data flow from your
program to a file or vice-versa.
ïĄ There are two formats of streams which are as
follows
ï§ Text Stream
âȘ It consists of sequence of characters, depending on the
compilers
âȘ Each character line in a text stream may be terminated by a
newline character.
âȘ Text streams are used for textual data, which has a consistent
appearance from one environment to another or from one
machine to another
6. ïĄA stream is a series of bytes data flow from
your program to a file or vice-versa.
ïĄ There are two formats of streams which are
as follows
ï§ Binary Stream
âȘ It is a series of bytes.
âȘ Binary streams are primarily used for non-textual data,
which is required to keep exact contents of the file.
7.
8. ïĄA text file can be a stream of characters that
a computer can process sequentially.
ïĄ It is processed only in forward direction.
ïĄ It is opened for one kind of operation
(reading, writing, or appending) at any give
time.
ïĄ It can read only one character at a time.
9. ïĄ A binary file is collection of bytes.
ïĄ In âCâ both a both a byte and a character are
equivalent.
ïĄ A binary file is also referred to as a character
stream, but there are two essential
differences.
10.
11.
12. ïĄ A file is identified by its name.
ïĄ This name is divided into two parts
ï§ File Name
âȘ It consists of alphabets and digits.
âȘ Special characters are also supported, but it depends on
operating system.
ï§ Extension
âȘ It describe the file type
13. ïĄ Before opening a file, we have to create a file
pointer.
ïĄ FILE structure is defined in the âstdio.hâ
header file.
ïĄ It stores the complete information about file.
ï§ Name of file, mode it is opened in, starting buffer address,
a character pointer that points to the character being read.
14. ïĄ To perform any operation (read or write) the
file has to be brought into memory from the
storage device.
ïĄ Thus, bringing the copy of file from disk to
memory is called opening the file.
15. Mode Meaning
r ï§ Open a text file for reading only. If the file doesnât exist, it returns null.
w ï§ Opens a file for writing only.
ï§ If file exists, than all the contents of that file are destroyed and new fresh blank file
is copied on the disk and memory with same name
ï§ If file dosenât exists, a new blank file is created and opened for writing.
ï§ Returns NULL if it is unable to open the file
a ï§ Appends to the existing text file
ï§ Adds data at the end of the file.
ï§ If file doesnât exists then a new file is created.
ï§ Returns NULL if it is unable to open the file.
rb ï§ Open a binary file for reading
wb ï§ Open a binary file for reading
ab ï§ Append to a binary file
r+ ï§ Open a text file for read/write
w+ ï§ Opens the existing text file or Creates a text file for read/write
16. Mode Meaning
a+ ï§ Append or create a text file for read/write
r+b ï§ Open a binary file for read/write
w+b ï§ Create a binary file for read/write
a+b ï§ Append a binary file for read/write
17. ïĄ fopen() function, like all the file-system
functions, uses the head file stdio.h
ïĄ The name of the file to open is pointed to by
fname
ïĄ The string pointed at for mode determined
how the file may be accessed.
19. ïĄ When we want to read contents from existing
file, then we require to open that file into
read mode that means ârâ mode
ïĄ To read file follow the below steps
1. Initialize the file variable
2. Open a file in read mode
3. Accept information from file
4. Write it into the output devices
5. Repeat steps 3 and 4 till file is not ends
6. Stop procedure
20. #include<stdio.h>
void main()
{
FILE *fp;
char ch;
fp=fopen(âclear.câ,ârâ);
if(fp==NULL)
print(âUnable to open clear.câ);
else
{
do
{
ch = getc(fp);
putchar(ch);
}while(ch!=EOF);
fclose(fp);
}
}
21. ïĄ A file contains a large amount of data.
ïĄ We some times cannot detect the end of file.
ïĄ Intext file, a special character EOP denotes
the end-of-file
22. ïĄ To close a file and dis-associate it with a
stream, use fclose() function.
ïĄ It returns 0 if successful else returns EOP if
error occurs.
ïĄ The fcloseall() closes all the files opened
previously.
23. #include<stdio.h>
void main()
{
FILE *fp;
char ch;
fp=fopen(âclear.câ,ârâ);
if(fp==NULL)
print(âUnable to open clear.câ);
else
{
do
{
ch = getc(fp);// gets the character from file
putchar(ch);
}while(ch!=EOF);
fclose(fp);
}
}
24. ïĄ We can add contents to the existing file
whenever it is required.
ïĄ Perform the following steps
1. Initialize the variable
2. Open a file in append mode
3. Accept information from user
4. Write it into the file
5. Repeat steps 3 and 4 according to userâs
choice
6. Stop procedure
25. ïĄ char *fgets(char *str, int n, FILE *fptr);
ï§ This statement reads character from the stream
fptr into to the character array âstrâ until a new line
character is read or end of file is reached or n-1
characters have been read.
ïĄ fputs(const char *str, FILE *fptr);
ï§ This statement writes to the stream fptr except
the terminating null character of string âstrâ
26. #include<stdio.h>
void main()
{
FILE *fp;
char line[280];int ch, i=0;
fp=fopen(âa.datâ,âaâ);
if(fp==NULL)
print(âUnable to open clear.câ);
else {
do{
do{
line[i++] = getchar();
}while(line[i-1]!=â*â);
fputs(line, fp);//Writes string to the file
i=0;
printf(ânPress 1 to continueâ);
scanf(â%dâ,&ch);
}while(ch==1);
fclose(fp);
printf(âFile is successfully createdâ);
}
}
27. ïĄ We can modify the files in two ways
ï§ Serial access
ï§ Random access
ïĄ Generally all the text files are considered to be
sequential files because lines of text files or
records of the formatted files are not equal.
ïĄ So address of each record is un predictable
ïĄ Whereas, in random access file all the record
length are same.
28. ïĄ To modify the file open the file with read and write mode
ïĄ âr+â or âw+â or âa+â
ïĄ Generally âr+â mode is use for this purpose
1. Initialize the variable
2. Open a file in read/write mode
3. Read information from file
4. Print it
5. Check whether the information is right or wrong?
1. If Wrong, accep correct information from user, move the file
pointer to the start of record or information. Re-write it
that means new information into the file.
2. If not wrong, go to step 6.
6. Repeat steps 3 to 6 till file is not end.
7. Stop procedure
29. ïĄ To modify the file open the file with read and write mode
ïĄ âr+â or âw+â or âa+â
ïĄ Generally âr+â mode is use for this purpose
1. Initialize the variable
2. Open a file in read/write mode
3. Read information from file
4. Print it
5. Check whether the information is right or wrong?
1. If Wrong, accep correct information from user, move the file
pointer to the start of record or information. Re-write it
that means new information into the file.
2. If not wrong, go to step 6.
6. Repeat steps 3 to 6 till file is not end.
7. Stop procedure
30. #include<stdio.h>
struct stock
{
int itid, qty;
char n[100];
float rate;
}it;
void main()
{
FILE *fp; int ch; int r = 0;
fp = fopen(âitem.câ, âr+â);
if(fp==NULL)
{
printf(âUnable to open item.câ);
}
}
32. fseek(FILE *fptr, long offset, int reference)
ïĄ Moves the pointer from one record to
another.
ïĄ The first argument is the file pointer
ïĄ The second argument tells the compiler by
how many bytes the pointer should be moved
from a particular position.
ïĄ The third argument is the reference from
which the pointer should be offset.
33. fseek(FILE *fptr, long offset, int reference)
ïĄ The third argument is the reference from
which the pointer should be offset.
ï§ SEEK_END moves the pointer from end of file
ï§ SEEK_CUR moves the pointer from the current
position
ï§ SEEK_SET moves the pointer from the beginning
of the file
34. fseek(FILE *fptr, long offset, int reference)
ïĄ Example
ï§ fseek(fptr, size, SEEK_CUR) sets the cursor ahead
from current position by size bytes
ï§ fseek(fptr, -size, SEEK_CUR) sets the cursor back
from current position by size bytes
ï§ fseek(fptr, 0, SEEK_END) sets cursor to the end of
the file
ï§ fseek(fptr, 0, SEEK_SET) sets cursor to the
beginning of the file