Explain with suitable code examples various possible file operations in C language?

1 answer

Answer

1117788

2026-07-26 15:20

+ Follow

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.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.