READ()
This function is used for reading raw binary data from a file. Unlike formatted input operations (e.g., using >>
), read()
performs an unformatted read, meaning it reads the exact number of bytes requested without interpreting or skipping whitespace, making it suitable for binary files or structured data. It's commonly used in applications that need to read fixed-size records or entire data structures directly from a file. After calling read()
, you can use methods like .gcount()
to determine how many bytes were actually read, which is useful when reaching the end of the file. This function is essential for low-level file I/O operations where precise control over byte-level data is needed.
std::istream& read(char* s, std::streamsize n);
* s: Pointer to a character array (buffer) where the data will be stored.
* n: Number of bytes to read from the input stream.
READING INDIVIDUAL CHARS FROM FILE
// READING INDIVIDUAL CHARS FROM FILE
#include <iostream> // For standard input and output
#include <fstream> // For file stream operations
using namespace std;
int main()
{
// Create an ifstream (input file stream) object to open the file for reading
ifstream read("C:\\Users\\Creator\\fileName.txt");
// Check if the file was successfully opened
if (!read) {
cerr << "Failed to open & read the file." << endl;
return 1; // Exit with an error code
}
char ch; // Variable to store each character read from the file
// Read characters one by one from the file until End Of File (EOF)
while (read.get(ch)) {
cout << ch; // Output the character to the console
}
// File will be automatically closed when the ifstream object goes out of scope
return 0; // Successful program execution
}
* When using C++ file streams to read from or write to files, the user or process
must have the appropriate permissions. Reading requires read access to the file,
while writing requires write access to the file or the
directory (if creating a new file). For files opened for both reading and
writing, both permissions are needed. Without these permissions, file operations
will fail, so it’s important to always check that the file opened successfully
before performing I/O.
READING WORDS FROM FILE
#include <iostream> // For standard input/output streams
#include <fstream> // For file stream operations
#include <string> // For using the std::string class
using namespace std;
int main() {
// Create an input file stream object and open the specified file
ifstream read("C:\\Users\\Creator\\fileName.txt");
// Check if the file was successfully opened
if (!read) {
cerr << "Failed to open file." << endl;
return 1; // Exit with an error code
}
string word; // Variable to store each word read from the file
// Read words one by one from the file until End Of File (EOF)
// The extraction operator (>>) reads characters until it encounters whitespace
while (read >> word) {
cout << word << endl; // Output the current word followed by a newline
}
// File stream is automatically closed when 'read' goes out of scope
return 0; // Successful program termination
}
* When using C++ file streams to read from or write to files, the user or process
must have the appropriate permissions. Reading requires read access to the file,
while writing requires write access to the file or the
directory (if creating a new file). For files opened for both reading and
writing, both permissions are needed. Without these permissions, file operations
will fail, so it’s important to always check that the file opened successfully
before performing I/O.
Last updated