File Management
File Management
File Handling
Text Streams:
A Text stream is a sequence of characters.
Standard C states that a text stream is organized into lines terminated by a newline character.
Binary Streams:
A Binary stream is a sequence of bytes that has a one-to-one correspondence to the bytes in the external device— that is, no character translations occur.
fopen() :
fopen() is used for opening a file.
Syntex :
FILE *fopen(const char *filename, const char *mode);
Example:
FILE *fp;
fp = fopen("test", "w");
The modes used with fopen() :
r - Open a text file for reading
w - Create a text file for writing
a - Append to a text file
rb - Open a binary file for reading
wb - Create a binary file for writing
ab - Append to a binary file
r+ - Open a text file for read/write
w+ - Create a text file for read/write
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 or create a binary file for read/write
fseek():
Repositions the file pointer of a stream
You can perform random read and write operations using the C I/O system with the help of fseek( ),which sets the file position indicator.
Syntax:
int fseek(FILE *fp, long int numbytes, int origin);
Example:
FILE *fp;
fp=fopen("D:/student.txt","r");
fseek(*fp,0L,SEEK_SET);
ftell( ):
Returns the current file pointer
You can determine the current location of a file using ftell( ).
Syntax:
long int ftell(FILE *fp);
Example:
FILE *fp;
int i;
Fp=fopen("D:/student.txt","r");
i=ftell(fp);
feof( ) :
The feof( ) function determines whether the end of the file associated with
the stream has been reached.
Syntax:
int feof(FILE *stream);
Example:
while(!feof(fp))
{
getc(fp);
}
fread( ) :
fread( ) functions allow the reading of blocks of any type of data.
Syntax:
size_t fread(void *buffer, size_t num_bytes, size_t count, FILE *fp);
fwrite( ) :
fwrite( ) functions allow the writing of blocks of any type of data.
Syntax:
size_t fwrite(const void *buffer, size_t num_bytes, size_t count, FILE *fp);
Comments
Post a Comment