Typedef statement is use to define a new data type that are equivalent to existing data type.
Syntax: Typedef int num
Sometime it is necessary to store the output in a file instead of just displaying it. In C Language this can be done by using file handling.
|1.||r||Opens an existing text file for reading purpose.|
|2.||w||Creates a new text file for writing purpose. If file already exists, then it open the file and overwrite it.|
|3.||a||Open a text file for writing in append mode, if it does not exist then a new file is created.|
|4.||r+||Opens a text file for reading and writing both.|
|5.||w+||Opens a text file for reading and writing both. It first truncate the file to zero length.|
|6.||a+||Opens a text file for reading and writing both. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.|
|1.||Create a new File|
|2.||Opening an existing File|
|3.||Reading from a file|
|4.||Writing to a file|
|5.||Searching in a file|
|6.||Closing a file|
Functions used in File Handling
1. fopen() – opening a file
2. fclose() – closing a file
3. fgetc() or getc – use to read a single character from a file at a time.
Syntax: char ch=fgetc (filepointer);
4. fputc() – write a character in a file.
Syntax: fputc(char variable, filepointer);
5. fputs() – write a string in a file.
Syntax: fputs(string, filepointer);
6. fgets () – read a string from a file.
Syntax: fgets(string, numberofcharacters, filepointer);
7. fprintf() – Write a record in a file.
Syntax: fprintf (filepointer,”%s %d”, e.name, e.age);
8. fscanf() – reading a record from a file.
Syntax: fscanf( fp, “%s %d”, &e.name, &e.age);
9. fwrite() – write a record in a file
Syntax: fwrite(&object, sizeof(object), 1, fp);
10. fread() – read a record from a file
Syntax: fread(&object, sizeof(object), 1, fp);
11. fseek() – search a record
Syntax: fseek (fp, -recsize, SEEK_CUR);
SEEK_END, SEEK_CUR, SEEK_SET
12. remove() – to delete a file
Syntax: remove (“filename.txt”);
13. rename() – to rename the file
Syntax: rename (“Oldfilename”, “Newfilename”);
14. ftell() – to locate the current position
Syntax: ftell (filepointer);
15. feof() – to locate end of file
Syntax: !feof(fp) – runs the loop till end of file.
1. count number of vowels, characters, words
2. Copy contents of one file into another.
If you are going to handle binary files then you will use below mentioned access modes instead of the above mentioned:
"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"