c++ ifstreamread
The read function in C++ is used to read a specified number of bytes from a file into a character array. It is a member function of the ifstream class which is used for input operations on files.
Syntax:
istream& read (char* s, streamsize n);
Parameters:
s: Pointer to an array of characters where the read data is stored.n: Number of bytes to be read from the file.
Return Value:
- The function returns the input stream object
istream.
Example Usage:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream file("example.txt", ios::binary);
if (file.is_open()) {
char buffer[5];
file.read(buffer, 5);
for (int i = 0; i < 5; i++) {
cout << buffer[i];
}
file.close();
}
return 0;
}
In this example, the read function is used to read the first 5 bytes from the file "example.txt" into a character array named buffer. The cout statement is then used to print the contents of buffer to the console
原文地址: https://www.cveoy.top/t/topic/fyuB 著作权归作者所有。请勿转载和采集!