对文件的读写;

来源:互联网 发布:sql server 递归排序 编辑:程序博客网 时间:2024/05/05 08:10
#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;

}

//这里是用的是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;
}
0 0