Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is data type of FILE in C?
In C, FILE is a special data type used to handle file operations. FILE is what we call an "opaque data type" − its internal implementation is hidden from the programmer and is system-specific. When we work with files, we use pointers of type FILE to access and manipulate file data.
Syntax
FILE *file_pointer;
What Makes FILE an Opaque Data Type?
The FILE structure contains internal fields that manage file operations like buffering, positioning, and error states. The actual definition varies between different operating systems and C library implementations.
FILE Structure Definition (System-Specific)
Here's an example of how FILE might be defined in a typical Linux system −
struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
char* _IO_read_ptr; /* Current read pointer */
char* _IO_read_end; /* End of get area. */
char* _IO_read_base; /* Start of putback+get area. */
char* _IO_write_base; /* Start of put area. */
char* _IO_write_ptr; /* Current put pointer. */
char* _IO_write_end; /* End of put area. */
char* _IO_buf_base; /* Start of reserve area. */
char* _IO_buf_end; /* End of reserve area. */
char *_IO_save_base; /* Pointer to start of non-current get area. */
char *_IO_backup_base; /* Pointer to first valid character of backup area */
char *_IO_save_end; /* Pointer to end of non-current get area. */
struct _IO_marker *_markers;
struct _IO_FILE *_chain;
int _fileno;
int _flags2;
_IO_off_t _old_offset; /* This used to be _offset but it's too small. */
unsigned short _cur_column;
signed char _vtable_offset;
char _shortbuf[1];
_IO_lock_t *_lock;
};
Note: To run the following example, create a text file named "sample.txt" in the same directory with some content, or the program will create one for writing.
Example: Using FILE Pointer
#include <stdio.h>
int main() {
FILE *fp;
char buff[100];
/* Create and write to a file */
fp = fopen("sample.txt", "w");
if (fp == NULL) {
printf("Error opening file!<br>");
return 1;
}
fprintf(fp, "Hello TutorialsPoint<br>");
fprintf(fp, "Learning FILE data type<br>");
fclose(fp);
/* Read from the file */
fp = fopen("sample.txt", "r");
if (fp == NULL) {
printf("Error opening file!<br>");
return 1;
}
printf("File contents:<br>");
while (fgets(buff, sizeof(buff), fp) != NULL) {
printf("%s", buff);
}
fclose(fp);
return 0;
}
File contents: Hello TutorialsPoint Learning FILE data type
Key Points
- FILE is an opaque data type − its implementation details are hidden
- We always use FILE pointers (FILE*) to work with files
- The actual structure definition is system-specific and may vary
- FILE handles buffering, positioning, and error management internally
Conclusion
FILE is a system-defined opaque data type in C that abstracts file handling operations. Its internal structure is hidden from programmers, allowing us to work with files through a consistent interface using FILE pointers.
