C Tutorial



FILES IN C


πŸ“‚ Files in C

In C, files are handled using the standard library functions in ``. These functions allow you to open, read, write, and close files. Let's explore how to work with files in C!

πŸ”‘ Key File Handling Functions:
- fopen() β€” Open a file
- fclose() β€” Close a file
- fprintf() β€” Write to a file
- fscanf() β€” Read from a file

1. πŸ§‘β€πŸ’» fopen() β€” Opening Files

The fopen() function is used to open a file. The syntax for opening a file is as follows:

FILE *fopen(const char *filename, const char *mode);
  

The mode argument specifies the mode of access: "r" for reading, "w" for writing, "a" for appending, etc.

✨ Example: Using fopen()

#include 

int main() {
    FILE *file = fopen("example.txt", "w");  // Open file for writing
    if (file == NULL) {
        printf("Failed to open file.\n");
        return 1;
    }
    fprintf(file, "Hello, World!\n");  // Write to the file
    fclose(file);  // Close the file
    return 0;
}
  
Explanation:
The file "example.txt" is opened in "write" mode. The text "Hello, World!" is written to the file and then the file is closed using fclose().

2. πŸ“ fprintf() β€” Writing to a File

The fprintf() function writes formatted text to a file, similar to printf() but with the file as the destination.

int fprintf(FILE *stream, const char *format, ...);
  

✨ Example: Using fprintf()

#include 

int main() {
    FILE *file = fopen("data.txt", "w");  // Open file for writing
    if (file == NULL) {
        printf("Failed to open file.\n");
        return 1;
    }
    fprintf(file, "Name: John\nAge: 25\nCity: New York\n");  // Write to the file
    fclose(file);  // Close the file
    return 0;
}
  
Explanation:
The fprintf() function writes multiple lines to the file "data.txt" with formatted data.

3. πŸ“– fscanf() β€” Reading from a File

The fscanf() function is used to read formatted input from a file, similar to scanf() but from a file instead of the console.

int fscanf(FILE *stream, const char *format, ...);
  

✨ Example: Using fscanf()

#include 

int main() {
    FILE *file = fopen("data.txt", "r");  // Open file for reading
    if (file == NULL) {
        printf("Failed to open file.\n");
        return 1;
    }
    
    char name[50];
    int age;
    char city[50];
    
    fscanf(file, "Name: %s\nAge: %d\nCity: %s\n", name, &age, city);  // Read data from the file
    
    printf("Name: %s\nAge: %d\nCity: %s\n", name, age, city);  // Output data
    
    fclose(file);  // Close the file
    return 0;
}
  
Explanation:
The fscanf() function reads the contents of the file "data.txt" and stores them in variables name, age, and city.

4. ❌ fclose() β€” Closing a File

After completing file operations, it’s important to close the file using the fclose() function. This ensures that resources are freed and no data is lost.

int fclose(FILE *stream);
  

✨ Example: Using fclose()

#include 

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Failed to open file.\n");
        return 1;
    }
    
    fprintf(file, "This file will now be closed.");
    
    fclose(file);  // Close the file
    printf("File closed successfully.\n");
    
    return 0;
}
  
Explanation:
After writing to the file, we use fclose() to close it and free up system resources.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review