// read a file into memory
#include <iostream> // std::cout
#include <fstream> // std::ifstream
using namespace std;
int main () {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
// get length of file:
is.seekg (0, is.end); // set position to end
int length = is.tellg(); // get length
is.seekg (0, is.beg); // set position to start
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
// read data as a block:
is.read (buffer, length);
if (is)
std::cout << "all characters read successfully." << endl;
else
std::cout << "error: only " << is.gcount() << " could be read" << endl;
is.close();
// ...buffer contains the entire file...
cout << buffer << endl;
cout << *buffer << endl; // first word
cout << *(buffer+1) << endl; // second word
cout << buffer[1] << endl; // second word
delete[] buffer;
}
return 0;
}
我是 Howard Weng,很多人叫我嗡嗡。這個網站放了我的各種筆記。希望這些筆記也能順便幫助到有需要的人們!如果文章有幫助到你的話,歡迎幫我點讚哦!
另外,因為定位是「個人的隨手筆記」,有些文章內容「⚠️可能我理解有誤⚠️」或「?只寫到一半?」,如果有發現這樣的情況,歡迎在該文章的最下面留言提醒我!我會儘快修正或補上!感謝大家的建議與幫忙,讓網站能變得更好?