In C, file operations are performed using functions provided by the standard library, primarily through the FILE structure and functions like fopen, fclose, fread, fwrite, fprintf, and fscanf. Here’s a brief example of opening a file, writing to it, reading from it, and closing it:
<code class="language-c">#include <stdio.h>int main() { // Writing to a file FILE *file = fopen("example.txt", "w"); fprintf(file, "Hello, World!\n"); fclose(file);
// Reading from a file char buffer[100]; file = fopen("example.txt", "r"); fscanf(file, "%s", buffer); printf("%s\n", buffer); fclose(file);
return 0; }
</code>
This code creates a file, writes a string into it, reads the string back, and then closes the file.
Copyright © 2026 eLLeNow.com All Rights Reserved.