您的位置:首页 > 其它

在已知文件的指定位置,写一个整数

2010-07-28 11:00 169 查看
// testWriteFile.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>

#include <string>

union DWORD_CHAR
{
int  nValue;
char charBuf[4];
};

/*
Name:
writeFile2Int
Description:
在已知文件的指定位置,写一个整数
这里假设,int类型占四个字节
Parameter:sFN    文件名
nWhere 准备写的位置(以零开始计数)
nValue 具体值。
Remark:
若文件长度小于nWhere,数据写到文件位置0开始的地方
TestEnv:
VS2008+SP1  32位程序
*/
bool writeFile2Int(std::string sFN,int nWhere,int nValue)
{
FILE *fp = NULL;
fp = fopen(sFN.c_str(),"rb+");
if (fp)
{
fseek(fp,nWhere,SEEK_SET);
DWORD_CHAR dc;
dc.nValue=nValue;
fwrite(dc.charBuf,1,4,fp);
fclose(fp);
return true;
}
return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
std::string sFN="temp.txt";
int nWhere=16;
int nValue=0x123456;
writeFile2Int(sFN,nWhere,nValue);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  fp string file
相关文章推荐