File: A collection of Data or Program or Records identified by a Name.
FILE* pointer:
- FILE is a pre-defined structure type variable.
- FILE* can points to any type of file.
- Once we make the pointer pointing to a file, then we can perform all FILE operations such as reading, writing, appending and so on.
Modes of Files:
Read mode(“r”):
- Opens file in Read mode.
- If file is present, it opens and returns pointer to that file.
- If file is not present, it returns NULL pointer.
Write mode(“w”):
- Opens file in Write mode.
- If file is present, it opens and removes the existing contents of File.
- If file is not present, it creates the new file with specified name.
Append mode(“a”):
- Opens file in append mode.
- If file is present, it opens and places the cursor at the end of existing data to add the new contents.
- If file is not present, it creates the new file with specified name.
Opening a File:
- fopen() is a pre-defined function that opens a file in specified mode.
- On success, it opens and returns the pointer to file.
- On failure, it returns NULL pointer.
FILE* fopen(char* path, char* mode);
#include<stdio.h> int main() { FILE* p; p = fopen(“c:/users/srinivas/desktop/code.c”, “r”); if(p==NULL) printf(“No such file to open \n”); else printf(“File opened in read mode \n”); return 0; } |