|
发表于 2019-4-30 01:09:40
|
显示全部楼层
本帖最后由 xx_player 于 2019-4-30 10:47 编辑
脑洞大开,还有这么写东西的就想执行地。.exe文件是有特定格式地。不是简单相加。不过流文件操作例程应该是这样。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
wstring filename = L"demo.txt";
//检查文件是否存在
struct _stat64 fileStat;
int fileExists = _wstat64(filename.c_str(), &fileStat);
if (fileExists == -1) {
//建立文件
ofstream ofile(filename, ios::out | ios::app);
ofile << "111111111111111111111" << endl;
ofile << "222222222222222222222" << endl;
ofile << "333333333333333333333" << endl;
ofile << "444444444444444444444" << endl;
ofile << "555555555555555555555" << endl;
ofile.close();
return -1;
}
//打开文件
ifstream ifile(filename, ios::in|ios::binary);
if (!ifile) {
cout << filename.c_str() << "cannot input!" << endl;
return -1;
}
//按行读
char linebuf[2][128];
ifile.getline(linebuf[0], 128);
ifile.getline(linebuf[1], 128);
//指定位置读
int pos = 46;
ifile.seekg(pos, ios::beg);
char buf[128] = { 0 };
ifile.read(buf, 23);
//接着读
ifile.read(&buf[23], 23);
//结果
cout << linebuf[0] << endl;
cout << linebuf[1] << endl;
cout << buf << endl;
//最后一次读的字节
cout << "read bytes=" << ifile.gcount() << endl;
//关闭文件
ifile.close();
return 0;
}

|
|