#include <stdafx.h>
#include <fstream>
#include <iostream>
#include <strstream>using namespace std;int main()
{
char buffer[80];
int lineNo = 0;
long position;
int rs;
fstream fs;
char replace[80] ; sprintf(replace,"%s%s%s","[HKEY_CURRENT_USER\\","America","\\Software\\Microsoft\\Internet Explorer\\Main]");
fs.open("c:\\main.reg",ios::in|ios::out);
if(!fs)
{
cout << "error open regfile" << endl;
return 0;
} while(!fs.eof())
{
fs.getline(buffer,80); if( fs.gcount() <= 2 )//跳过空行
continue ; cout << buffer ;
lineNo++;
position = fs.tellp(); //定位文件指针位置
cout << position << endl; rs = strcmp(buffer,"[HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main]");
if(rs == 0)
{
fs.seekp(position - (strlen(buffer))-1); //文件指针定位到要修改的内容cout << fs.tellp();
fs.write((char *)replace,strlen(replace)) << flush; //写入文件
}
}
fs.close(); return 1;
}执行完之后,为什么main.reg的内容没变?问题出在哪?哪位大虾帮忙看看吧.
#include <fstream>
#include <iostream>
#include <strstream>using namespace std;int main()
{
char buffer[80];
int lineNo = 0;
long position;
int rs;
fstream fs;
char replace[80] ; sprintf(replace,"%s%s%s","[HKEY_CURRENT_USER\\","America","\\Software\\Microsoft\\Internet Explorer\\Main]");
fs.open("c:\\main.reg",ios::in|ios::out);
if(!fs)
{
cout << "error open regfile" << endl;
return 0;
} while(!fs.eof())
{
fs.getline(buffer,80); if( fs.gcount() <= 2 )//跳过空行
continue ; cout << buffer ;
lineNo++;
position = fs.tellp(); //定位文件指针位置
cout << position << endl; rs = strcmp(buffer,"[HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main]");
if(rs == 0)
{
fs.seekp(position - (strlen(buffer))-1); //文件指针定位到要修改的内容cout << fs.tellp();
fs.write((char *)replace,strlen(replace)) << flush; //写入文件
}
}
fs.close(); return 1;
}执行完之后,为什么main.reg的内容没变?问题出在哪?哪位大虾帮忙看看吧.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货