首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一个关于ofstream的有关问题

2012-04-26 
一个关于ofstream的问题raw.txt里的内容是:女791170.51195.5灰色女801195.51199灰色out.txt里已经有内容了

一个关于ofstream的问题
raw.txt里的内容是:
女791170.51195.5灰色
女801195.51199灰色

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,
不知道该怎么弄了,请大家帮帮忙,看怎么能实现我的功能?


[解决办法]
outf.open("out.txt",ios::app);
[解决办法]
先用getline获得每一行,然后在读取头一个数据.
[解决办法]
楼上正解~~~
[解决办法]
列出了文件打开方式:
文件打开方式 描述
ios::app 将所有输出写入文件末尾
ios::ate 打开文件以便输出,并移到文件末尾(通常用于在
文件中添加数据)。数据可以写入文件的任务地方
ios::in 打开文件以便输入
ios::out 打开文件以便输出
ios::trunc 删除文件中现有内容(这也是ios::out的默认操作) 
ios::binary 打开文件以进行二进制(也就是非文本)格式输入或输出
[解决办法]
你测试一下看看可以不可以,我的系统是英文的XP,编码和中文的系统不同,可能会有点小问题

C/C++ code
#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;}
[解决办法]
C/C++ code
#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;}
[解决办法]
C/C++ code
#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;} 


[解决办法]

C/C++ code
#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;} 

热点排行