急问!关于文件修改操作实现!

woxihuanbohe 2003-03-14 09:36:13
现有一文件(good.txt)里面存在很多信息,信息(内存中用一个结构存放)格式如下:
goodID goodName goodNumber
1001 what 21
.... ..... ..

当我查询到有一条信息(当时记录下此时的文件指针位置),并需要修改其中某些数据,如上条信息 要修改goodNumber为56,修改确认后,写回文件(替换原来信息)!

这个过程怎么实现?本人疑问在文件指针移动定位,写回。
请大家给出C++示例代码!谢谢!
...全文
30 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
woxihuanbohe 2003-03-17
  • 打赏
  • 举报
回复
高手来看看???????????
woxihuanbohe 2003-03-15
  • 打赏
  • 举报
回复
aaaaaaaaaaaa
今天周末,高手应该来了吧????????
mengyu_102 2003-03-14
  • 打赏
  • 举报
回复
UP
woxihuanbohe 2003-03-14
  • 打赏
  • 举报
回复
我自己的问题

自己up
寻求高手帮忙!!!!!!!!!!!!!!!1


大家都去关税了????
Januarius_ 2003-03-14
  • 打赏
  • 举报
回复
seekp(pos);
然后在该位置写下修改数值就行了
limzustc 2003-03-14
  • 打赏
  • 举报
回复
up
woxihuanbohe 2003-03-14
  • 打赏
  • 举报
回复
我的C++ primer(3e)被盗了
所以没法查找相关技术,所以来这里向大家询问!
多谢楼上提点!

如果大家有效率好点的代码,不妨共享下!
欢迎继续讨论!
snipersu 2003-03-14
  • 打赏
  • 举报
回复
用ofstream 的话用seekp()
具体的msdn解释:

ostream& seekp( streampos pos );
ostream& seekp( streamoff off, ios::seek_dir dir );
Parameters
pos
The new position value; streampos is a typedef equivalent to long.
off
The new offset value; streamoff is a typedef equivalent to long.
dir
The seek direction specified by the enumerated type ios::seek_dir, with values including:
ios::beg Seek from the beginning of the stream.
ios::cur Seek from the current position in the stream.
ios::end Seek from the end of the stream.
多看看msdn,有很大好处的。


langziji 2003-03-14
  • 打赏
  • 举报
回复
seek(point,pos);
write(point,str,sizeof(struct));
catyou 2003-03-14
  • 打赏
  • 举报
回复
如果是一次读出之后, 根据条件修改, 则在内存中修改之后, 再将其写入文件即可。如果是每读一个数据, 进行判断, 在原位置写入, 有fseek可以用吧。
macadam 2003-03-14
  • 打赏
  • 举报
回复
ing
woxihuanbohe 2003-03-14
  • 打赏
  • 举报
回复
搞不定啊 ~~~~~
大家来帮我看看~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void main()
{
/* ofstream oFile;
oFile.open("text.txt",ios_base::app);
if (oFile.fail())
{
cerr << "file open error!" << endl;
exit(1);
}*/

struct good
{
char goodID[5];
char goodName[10];
int goodNumber;
};
/* good sogood[5];

for(int i=0; i<5; i++)//从键盘读入五条信息到文件!
{
cout << "input a kind of good information!" << endl;
cin >> sogood[i].goodID
>> sogood[i].goodName
>> sogood[i].goodNumber;
oFile << sogood[i].goodID << setw(5)
<< sogood[i].goodName << setw(5)
<< sogood[i].goodNumber;
oFile << endl;
}
oFile.close();*/

fstream ioFile;
ioFile.open("text.txt",ios_base::in || ios_base::out);
if (ioFile.fail())
{
cerr << "open file error!" << endl;
exit(1);
}
good agood;
char ID[5];

cout << "input the good ID you want to alter its information!"<<endl;
cin >> ID;
//数据读入内存
ioFile >> agood.goodID >> agood.goodName >> agood.goodNumber ;
while(ioFile.good())
{
streampos here = ioFile.tellp();//?
if (strcmp(agood.goodID,ID) == 0)
{

ioFile.seekp(40,ios::cur);//?
//执行修改操作
agood.goodNumber = 80;
//写回文件操作,替换原来记录
ioFile.wirte();//这个怎么用?
break;
}
ioFile >> agood.goodID >> agood.goodName >> agood.goodNumber;
}
if (ioFile.eof())
cout << "this good is not exist!" <<endl;

ioFile.close();
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧