一个关于ofstream的问题

yuyeahcool 2008-02-26 07:46:52
raw.txt里的内容是:
女79 1170.5 1195.5 灰色
女80 1195.5 1199 灰色

out.txt里已经有内容了
想把:女79、女80 这两个读出来,每个一行写在out.txt中,而且写是续写的形式,out.txt里已经有的内容不能丢,本小弟编的程序如下:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ifstream infile("raw.txt");
ofstream outf;
outf.open("out.txt",ios::ate);
if(!infile)
{
cerr<<"No infile"<<endl;
exit(1);
}
string wellname;
while(infile>>wellname)
{
cout<<wellname<<endl;
outf << wellname;
}
infile.close();
outf.close();
}

运行起来不能续写,原来out.txt中有的内容全部被覆盖了,然后女79、女80写在out.txt中的形式是Ů79、Ů80,
不知道该怎么弄了,请大家帮帮忙,看怎么能实现我的功能?
...全文
314 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangshoutian 2012-04-25
  • 打赏
  • 举报
回复
想要原来的out。txt不被覆盖可以用seekp(将输出文件中指针移到指定的位置)或者tell返回输出文件指针当前的位置
zhangshoutian 2012-04-25
  • 打赏
  • 举报
回复
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream outf("out.txt",ios::out);
if(!outf)
{
cerr<<"No outf";
exit(1);
}

ifstream infile("raw.txt");
if(!infile)
{
cerr<<"No infile";
exit(1);
}
string name;
while(infile>>name)
{
cout<<name<<endl;
outf<<name;
}
infile.close();
outf.close();
return 0;
}
zhangshoutian 2012-04-25
  • 打赏
  • 举报
回复
我编译了一下,楼主的管用啊
HelloDan 2008-03-02
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>

using namespace std;
int main()
{
ifstream infile("raw.txt");
ofstream outf("out.txt",ios::app);

if(!infile)
{
cerr<<"no infile"<<endl;
return 1;
}
string wellname;
while(getline(infile,wellname))
{

istringstream stream(wellname);
string word;
stream>>word;
outf<<word<<" ";
cout<<word<<ends;
stream>>word;
outf<<word<<endl;
cout<<word<<endl;
}
outf.close();
infile.close();
return 0;
}
fenglixin70953 2008-03-02
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>

using namespace std;
int main(){
ifstream infile("raw.txt");
ofstream outf;
outf.open("out.txt",ios::app);

if(!infile)
{
cerr<<"no infile"<<endl;
return 1;
}
string wellname;
while(getline(infile,wellname))
{
cout<<wellname<<endl;
istringstream stream(wellname);
string word;
stream>>word;
outf<<" "<<word<<" ";
}
outf.close();
infile.close();
return 0;
}
薛勇 2008-03-02
  • 打赏
  • 举报
回复
ios::app 可以设置读入文件时的属性
hj_etone 2008-02-29
  • 打赏
  • 举报
回复
执行命令后显示
女79
女80
打开out.text,显示
Ů79
Ů80
怎么让里面也显示成
女79
女80
Chappell 2008-02-29
  • 打赏
  • 举报
回复

1、6楼正解
2、试着把raw.txt重建一下,删除原来数据,手动输入测试数据。
星羽 2008-02-26
  • 打赏
  • 举报
回复

女79
女80
请按任意键继续. . .







星羽 2008-02-26
  • 打赏
  • 举报
回复


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

int main()
{
ifstream infile("raw.txt");
ofstream outf;
outf.open("out.txt",ios::app);

if (!infile)
{
cerr<<"No infile"<<endl;
return 1;
}

char buffer[128];
infile.getline(buffer, sizeof(buffer));

while (!infile.fail())
{
char* p = strchr(buffer, ' ');
if (p)
*p = 0;
cout<<buffer<<endl;
outf<<buffer;
infile.getline(buffer, sizeof(buffer));
}

infile.close();
outf.close();

return 0;
}

独孤过儿 2008-02-26
  • 打赏
  • 举报
回复
你测试一下看看可以不可以,我的系统是英文的XP,编码和中文的系统不同,可能会有点小问题

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

int main()
{
ifstream infile("raw.txt");
ofstream outf;
outf.open("out.txt", ios::out);
if (!infile)
{
cerr << "No infile" << endl;
return 1;
}
string wellname, temp;
while( getline (infile, wellname) )
{
cout << wellname <<endl;
temp = wellname.substr(0, 5);
outf << temp;
}
infile.close();
outf.close();
return 0;
}

laolaoliu2002 2008-02-26
  • 打赏
  • 举报
回复
列出了文件打开方式:
文件打开方式 描述
ios::app 将所有输出写入文件末尾
ios::ate 打开文件以便输出,并移到文件末尾(通常用于在
文件中添加数据)。数据可以写入文件的任务地方
ios::in 打开文件以便输入
ios::out 打开文件以便输出
ios::trunc 删除文件中现有内容(这也是ios::out的默认操作)
ios::binary 打开文件以进行二进制(也就是非文本)格式输入或输出
lori227 2008-02-26
  • 打赏
  • 举报
回复
楼上正解~~~
  • 打赏
  • 举报
回复
先用getline获得每一行,然后在读取头一个数据.
CQZE 2008-02-26
  • 打赏
  • 举报
回复
outf.open("out.txt",ios::app);

64,849

社区成员

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

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