C - Language Theory
Files
- File is a Collection of bytes that is stored on secondary storage devices like disk. There are two kinds of files in a system.
- When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates.
- When a computer reads a file, it copies the file from the storage device to memorey, when it writes to a file, it transfers data from memory to the storage device.
Types:
1. Text filesText files are the normal .txt files. You can easily create text files using any simple text editors such as Notepad.
2. Binary filesBinary file contains collection of bytes (0’s and 1’s). Binary files are compiled version of text files.
Files
This method is used to copies the address of an argument into the formal parameter.
Mode | Description |
---|---|
"r" | It opens an existing file for reading only. |
"W" | It opens a new file for writing. If the filename does not exist it will be created and if the file already exists then its contents are deleted. |
"a" | It appends the existing file. If the filename does not exist it will be created. |
"r+" | It opens an existing file for reading and writing. It indicates that the file is to be read before writing. |
"w+" | It opens a new file for reading and writing. If a file with the current filename exists then it is destroyed and a new file name is created. |
"a+" | It opens an existing file for reading and appending. Its stream is positioned at the end of the file content. |
Files
C provides a number of functions that helps to perform basic file operations.
Following are the functions.
Function | Description |
---|---|
fopen() | Create a new file or open a existing file |
fclose() | Close a file |
getc() | Reads a character from a file |
putc() | Writes a character to a file |
fscanf() | Reads a set of data from a file |
fprintf() | Writes a set of data to a file |
getw() | Reads a integer from a file |
putw() | Set the position to desire point |
ftell() | Gives current position in the file |
rewind() | Set the position to the beginning point |