File is an object on the computer that stores data. File is created for permanent storage of data. Files are needed if we want to keep large volume of data. With file, these information can be accesed using few commands. There are 2 kinds of file in general:
- Text file
source code, plain text, html files, most of configuration files, etc. - Binary file
mosts audio and video file formats, graphics file formats, compressed files, most of office formats, etc.
The syntax is
FILE *fp;
C programming language provides some functions that helps user to perform basic file operations.
There are the functions:
- fopen()
The syntax:
FILE *fopen(const char*filename, constchar*mode);
fopen() to create a new file or open a existing file. Opens the file whose name is specified
in the parameter filename and associates it with a stream that can be identified in future
operations by the FILE pointer returned.
Here filename is the name of the file to be opened and mode specifies the purpose of opening the file. Mode can be of following types, r = opens a text file in reading mode
w = opens or create a text file in writing modea = opens a text file in append moder+ = opens a text file in both reading and writing modew+ = opens a text file in both reading and writing modea+ = opens a text file in both reading and writing mode
rb = opens a binary file in reading modewb = opens or create a binary file in writing modeab = opens a binary file in append moderb+ = opens a binary file in both reading and writing mode
wb+ = opens a binary file in both reading and writing modeab+ = opens a binary file in both reading and writing mode - fclose()
The syntax:
int fclose(FILE*stream);
fclose() to closes a file. Closes the file associated with the stream and disassociates it. Here fclose() function closes the file and returns zero on success, or EOF if there is an error in closing the file. This EOF is a constant defined in the header file stdio.h. - getc()
The syntax:int getc(FILE*stream);
getc() to reads a character from a file. Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced to the next character. - putc()
The syntax:
int putc(int character, FILE*stream);putc to writes a character to a file. The character is written at the position indicated by the internal position indicator of the stream, which is then automatically advanced by one. - fscanf()
The syntax:int fscanf(FILE*stream, const char*format,...);
fscanf() to reads a set of data from a file. Reads data from the stream and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. - fprintf()The syntax:
int fprintf(FILE*stream, const char*format,...);fprintf() to writes a set of data to a file. Writes the C string pointed by format to the stream. If format includes format specifiers, the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. - getw()
getw() to reads a integer from a file. - putw()
putw() to writes a integer to a file. - fseek()
The syntax:
int fseek(FILE*stream, long int offset, int origin);
fseek() to set the position to desire point. For streams open in binary mode, the new position is defined by adding offset to a reference position specified by origin. For streams open in text mode, offset shall either be zero or a value returned by a previous call to ftell, and origin shall necessarily by SEEK_SET.
SEEK_SET => Beginning of file
SEEK_CUR => Current position of the file pointer
SEEK_END => End of file - ftell()
The syntax:
long int ftell(FILE*stream);
ftell() to gives current position in the file. For binary streams, this is the number of bytes from the beginning of the file. For text streams, the numerical value may not be meaningful but can still be used to restore the position to the sam position later using fseek. - rewind()
The syntax:
void rewind(FILE*stream);
rewind() to set the position to the beginning point. On streams open for update (read+write), a call to rewind allows the switch between reading and writing.
File Handling in C Language
C - File I/O
http://www.cplusplus.com/reference/
Have a great time with coding guys! See ya at the next post!
No comments:
Post a Comment