#include <iostream>
#include <fstream>
#include <string>
#include<windows.h>
int main()
{
std::string str;
std::ifstream file("test.txt", std::ios::in | std::ios::ate);
if (file) {
std::ifstream::streampos filesize = file.tellg();
str.reserve(filesize);
file.seekg(0);
while (!file.eof())
{
str += file.get();
}
std::cout << str;
HANDLE hFile = CreateFile("test.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dword = GetFileSize(hFile, NULL);
char* pCh = new char[dword + 10];
DWORD word;
ReadFile(hFile, pCh, dword +1, &word, NULL);
pCh[dword + 1] = 0;
CloseHandle(hFile);
std::cout << pCh << std::endl;
int x;
std::cin >> x;
return 0;
}
#include <fstream>
#include <string>
#include<windows.h>
int main()
{
std::string str;
std::ifstream file("test.txt", std::ios::in | std::ios::ate);
if (file) {
std::ifstream::streampos filesize = file.tellg();
str.reserve(filesize);
file.seekg(0);
while (!file.eof())
{
str += file.get();
}
std::cout << str;
}
//这里是用的是C++一般的读取整个文件的内容读到str中;
Win32中读取文件的做法;
HANDLE hFile = CreateFile("test.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dword = GetFileSize(hFile, NULL);
char* pCh = new char[dword + 10];
DWORD word;
ReadFile(hFile, pCh, dword +1, &word, NULL);
pCh[dword + 1] = 0;
CloseHandle(hFile);
std::cout << pCh << std::endl;
int x;
std::cin >> x;
return 0;
}