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 are the high level I/O functions in C language?
High-level I/O functions in C provide an abstracted, user-friendly interface for input and output operations. These functions are part of the standard C library and offer better portability compared to low-level I/O functions.
High Level vs Low Level I/O
High Level I/O
- Easy to understand and use by programmers
- Portable across different systems
- Built-in buffering for better performance
Low Level I/O
- Closer to the operating system
- Faster execution time
- System-dependent (non-portable)
High Level I/O Functions
The high level input-output (I/O) functions are explained below −
| Function | Description |
|---|---|
| fprintf() | Write formatted data to a file |
| fscanf() | Read formatted data from a file |
| putc()/fputc() | Write a character to a file |
| getc()/fgetc() | Read a character from a file |
| putw() | Write an integer to a file |
| getw() | Read an integer from a file |
| fputs() | Write a string to a file |
| fgets() | Read a string from a file |
| fread() | Read binary data from a file |
| fwrite() | Write binary data to a file |
Syntax
fprintf(FILE *stream, const char *format, ...); fscanf(FILE *stream, const char *format, ...); int putc(int c, FILE *stream); int getc(FILE *stream); int putw(int w, FILE *stream); int getw(FILE *stream); int fputs(const char *str, FILE *stream); char *fgets(char *str, int n, FILE *stream); size_t fread(void *ptr, size_t size, size_t count, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
Example 1: Using putw() and getw() Functions
The following program stores numbers from 1 to 5 in a file and then reads them back −
#include <stdio.h>
int main() {
FILE *fp;
int i, num;
/* Write numbers to file */
fp = fopen("numbers.txt", "w");
if (fp == NULL) {
printf("Error opening file for writing<br>");
return 1;
}
for (i = 1; i <= 5; i++) {
putw(i, fp);
}
fclose(fp);
/* Read numbers from file */
fp = fopen("numbers.txt", "r");
if (fp == NULL) {
printf("Error opening file for reading<br>");
return 1;
}
printf("File contents: ");
while ((num = getw(fp)) != EOF) {
printf("%d ", num);
}
printf("<br>");
fclose(fp);
return 0;
}
Example 2: Using fwrite() and fread() for Structures
This example demonstrates storing and reading student records using binary file operations −
#include <stdio.h>
#include <string.h>
struct student {
int sno;
char sname[30];
float marks;
};
int main() {
struct student s[2];
FILE *fp;
int i;
/* Initialize student data */
s[0].sno = 1;
strcpy(s[0].sname, "John");
s[0].marks = 85.5;
s[1].sno = 2;
strcpy(s[1].sname, "Alice");
s[1].marks = 92.0;
/* Write to file */
fp = fopen("student.dat", "wb");
if (fp == NULL) {
printf("Error opening file for writing<br>");
return 1;
}
for (i = 0; i < 2; i++) {
fwrite(&s[i], sizeof(struct student), 1, fp);
}
fclose(fp);
/* Read from file */
fp = fopen("student.dat", "rb");
if (fp == NULL) {
printf("Error opening file for reading<br>");
return 1;
}
printf("Student Records:<br>");
for (i = 0; i < 2; i++) {
fread(&s[i], sizeof(struct student), 1, fp);
printf("Student %d: ID=%d, Name=%s, Marks=%.1f<br>",
i+1, s[i].sno, s[i].sname, s[i].marks);
}
fclose(fp);
return 0;
}
Key Points
- High-level I/O functions provide buffered I/O operations
- Always check for file opening errors using NULL comparison
- Use
fclose()to properly close files and flush buffers - Binary mode ("rb", "wb") is recommended for structures
- Functions like
putw()andgetw()are non-standard but widely supported
Conclusion
High-level I/O functions in C provide a portable and efficient way to perform file operations. They handle buffering automatically and offer formatted input/output capabilities, making them ideal for most file handling tasks in C programming.
