[简单]c++如何更新文件中的数据?

artwl_cn 2008-12-15 12:41:38
我想更新一个数据文件中的部分数据,不全部读出来,只读要更新的部分可以吗?
如下面这个数据表,我想把c++的学分改成5分,其他的不变,最常规的做法是什么样的呢?


课程名 学分
c++ 4
wuli 2
姓名 学号 性别 c++ wuli
aa 1001 m 80 90
bb 1002 f 70 90
cc 1003 m 90 90


有请各位大侠了!一经解决,分马上奉上!
...全文
453 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
artwl_cn 2008-12-15
  • 打赏
  • 举报
回复
那只把c++ 4这一行读出来进行更新可以吗?
nullah 2008-12-15
  • 打赏
  • 举报
回复
只能找到你要修改的字符串了
artwl_cn 2008-12-15
  • 打赏
  • 举报
回复
多谢13楼的,我还发了一个同样的贴子“散分了(一个小问题) ”,100分的,你回一条也给你吧!
tianfu1 2008-12-15
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 durongjian 的回复:]
求救啊,大侠们!
我把那个数年已读出来了,但不知怎样改

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void main()
{
fstream file;
file.open( "student1.txt",ios::in|ios::out);
int n;
file.seekg(17,ios::beg); // chars to skip
file>>n;
cout < <n;
file.close();
}
输出是4
[/Quote]

将file>>n, 换成file<<5试试
artwl_cn 2008-12-15
  • 打赏
  • 举报
回复
求救啊,大侠们!
我把那个数年已读出来了,但不知怎样改

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void main()
{
fstream file;
file.open( "student1.txt",ios::in|ios::out);
int n;
file.seekg(17,ios::beg); // chars to skip
file>>n;
cout<<n;
file.close();
}
输出是4
herman~~ 2008-12-15
  • 打赏
  • 举报
回复
ifstream file;
file.open( "wenjian.txt" );
file.seekg(6); // chars to skip
herman~~ 2008-12-15
  • 打赏
  • 举报
回复
ifstream file;
file.open( "wenjian.txt" );
file.seekg(6); // chars to skip
shuiyan 2008-12-15
  • 打赏
  • 举报
回复
既然都“天行健”了,怎么不想着“自强不息”呢?

方案就是:查找固定个数的“空格”,第三个空格后,直到后一个(第四个)空格之间读出来,然后改成你需要的就行。

详细的程序?一般是要银子的。
artwl_cn 2008-12-15
  • 打赏
  • 举报
回复
恳请高人指点
artwl_cn 2008-12-15
  • 打赏
  • 举报
回复
有高手在吗?
artwl_cn 2008-12-15
  • 打赏
  • 举报
回复
请问哪位高手能给详细的程序吗?
P_ghost 2008-12-15
  • 打赏
  • 举报
回复
打开文件,搜索指定位置,修改,关闭文件
kier2 2008-12-15
  • 打赏
  • 举报
回复
ifstream file;
file.open( "wenjian.txt" );
file.seekg(6); // chars to skip
artwl_cn 2008-12-15
  • 打赏
  • 举报
回复
有高手在吗,请指点一下!

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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