Basic Input/Output in cpp
Introduction to File Handling
In this video, we will be looking at how to handle files. File handling is essential in software engineering because it allows us to store data persistently, enabling our applications to read and write data to files on disk. Without file handling, our code would be limited to temporary data storage in memory, making it less useful for real-world applications.
// Example of file handling in C
#include <stdio.h>
int main() {
FILE *file;
file = fopen("test.txt", "w"); // Open a file for writing
if (file == NULL) {
printf("File failed to open.");
return 1;
}
fprintf(file, "Hello, World!"); // Write to file
fclose(file); // Close the file
return 0;
}
graph TD; A[Start] --> B{Open File?}; B -- Yes --> C[Write Data]; B -- No --> D[Error]; C --> E[Close File]; E --> F[End]; D --> F;
Creating a New File
To create a new file in C, we can use the fopen()
function with the write mode. If the file does not exist, it will be created. If it exists, its content will be overwritten unless you use the append mode instead. Here's a simple example:
FILE *file;
file = fopen("newfile.txt", "w"); // Create or overwrite a file
if (file == NULL) {
printf("Error opening file!");
}
graph TD; A[Start] --> B[Open File in Write Mode]; B --> C{File Exists?}; C -- Yes --> D[Overwrite Content]; C -- No --> E[Create New File]; D --> F[Close File]; E --> F;
Writing to a File
To write data to a file, you can use functions like fprintf()
or fputs()
. These functions allow you to write formatted text or strings to the file. Here's how you can use them:
fprintf(file, "This is a line of text."); // Write formatted data
fputs("This is another line.", file); // Write a string
graph TD; A[Start] --> B[Open File]; B --> C[Write Data Using fprintf()]; C --> D[Write Data Using fputs()]; D --> E[Close File]; E --> F[End];
Reading from a File
To read data from a file, you can use functions like fscanf()
or fgets()
. These functions allow you to read formatted input or strings from the file. Here's an example:
char buffer[100];
fscanf(file, "%s", buffer); // Read formatted data
fgets(buffer, sizeof(buffer), file); // Read a line of text
graph TD; A[Start] --> B[Open File]; B --> C[Read Data Using fscanf()]; C --> D[Read Data Using fgets()]; D --> E[Close File]; E --> F[End];
Closing a File
After performing file operations, it is important to close the file using fclose()
. This frees up the resources associated with the file and ensures that all data is properly written to disk. Here's how to close a file:
fclose(file); // Close the opened file
graph TD; A[Start] --> B[Perform File Operations]; B --> C[Close File]; C --> D[End];
Error Handling in File Operations
When working with files, error handling is crucial. Always check if the file pointer is NULL after attempting to open a file. This indicates that the file could not be opened for some reason, such as it not existing or lacking permissions. Here's an example of error handling:
if (file == NULL) {
printf("Error: File could not be opened.");
}
Conclusion
In this guide, we covered the essentials of file handling in C programming. You learned how to create, write, read, and close files, as well as the importance of error handling. Mastering these concepts will enhance your programming skills and allow you to manage data more effectively in your applications.