fstream读取文件
2022-10-25 本文已影响0人
江河湖海洋
fstream读取文件
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
int readFile()
{
std::ifstream ifs;
//ifs.open("read.txt", std::ios::in);
ifs.open("read.txt", std::ios::in | std::ios::binary);
if (!ifs.is_open())
{
std::cout << "文件打开失败" << std::endl;
}
#if 0
char buf[1024] = { 0 };
while(ifs >> buf)
{
std::cout << buf << std::endl;
}
char buf[1024] = { 0 };
while(ifs.getline(buf.size(buf)))
{
std::cout<< buf << std::endl;
}
std::string buf;
while(getline(ifs, buf))
{
std::cout<< buf << std::endl;
}
char c;
while((c = ifs.get()) != EOF)
{
std::cout << c;
}
#endif
ifs.seekg(0, ifs.end);
uint32_t fileSize = ifs.tellg();
std::cout << "fileSize="<< fileSize << std::endl;
//读取二进制
char buf[1024] = { 0 };
//ifs.read(buf, sizeof(buf));
ifs.read(buf, 10);
uint32_t readlength = ifs.gcount();
std::cout << "readlength="<< readlength << std::endl;
std::cout << buf << std::endl;
ifs.close();
uint16_t did = 0xf1bc;
printf("%s[%-02x]\n", __FILE__, did);
return 0;
}
int main()
{
readFile();
return 0;
}