请问如何把一个文本文件的全部内容读到一个string中

cure 2002-06-14 02:30:01
使用标准的c++

我用
ifstream in_file( "abc.txt" );
string word;
in_file >> word;
读到空格就中止了.可我的string要求有空格和换行符.

请教大家了.
...全文
152 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
狐帝 2002-06-18
  • 打赏
  • 举报
回复
不是String s=oss.str();,应该是string s=oss.str();注意大小写。
pi1ot 2002-06-18
  • 打赏
  • 举报
回复
大概borland的String的实现和std::string不太一样把。

标准string的说明在这里:
http://www.roguewave.com/support/docs/SourcePro/stdlibref/basic-string.cfm
gois 2002-06-18
  • 打赏
  • 举报
回复
#include <conio.h>//getch()
#include <vcl.h>
#pragma hdrstop
#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <strstream.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
ifstream infile;
infile.open("Input.in",ios::in);
if(!infile)
{
cout<<"dljf"<<endl;
getch();
exit(1);
}
ostrstream oss;
oss<<infile.rdbuf()<<ends;
String s=oss.str();

cout<<s;//编译通不过
cout<<s[1];//输出第一个字符
cout<<s[0];//运行时错误

getch();

}
//---------------------------------------------------------------------------


gois 2002-06-18
  • 打赏
  • 举报
回复
#include <conio.h>
#include <vcl.h>
#pragma hdrstop
#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <strstream.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
ifstream infile;
infile.open("Input.in",ios::in);
if(!infile)
{
cout<<"dljf"<<endl;
getch();
exit(1);
}
ostrstream oss;
oss<<infile.rdbuf()<<ends;
String s=oss.str();

cout<<s;//编译通不过
cout<<s[1];//输出第一个字符
cout<<s[0];//运行时错误

getch();

}
//---------------------------------------------------------------------------


pi1ot 2002-06-18
  • 打赏
  • 举报
回复
如果你想了解std::string可以到 http://www.roguewave.com/support/docs/index.cfm 看标准c++的类库手册.
gois 2002-06-18
  • 打赏
  • 举报
回复
#include <conio.h>
#include <vcl.h>
#pragma hdrstop
#include <iostream.h>
#include <stdio.h>
#include <fstream.h>
#include <strstream.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
ifstream infile;
infile.open("Input.in",ios::in);
if(!infile)
{
cout<<"dljf"<<endl;
getch();
exit(1);
}
ostrstream oss;
oss<<infile.rdbuf()<<ends;
String s=oss.str();

cout<<s;//编译通不过
cout<<s[1];//输出第一个字符
cout<<s[0];//运行时错误

getch();

}
//---------------------------------------------------------------------------


pi1ot 2002-06-18
  • 打赏
  • 举报
回复
你用的是AnsiString?把这部分源程序写出来看看.
gois 2002-06-18
  • 打赏
  • 举报
回复
不好意思我在定义s时用了String ,而不是string

抱歉

顺便问一下:String(AnsiString)与 string具体是怎样操作字符串的
pi1ot 2002-06-18
  • 打赏
  • 举报
回复
你用的是AnsiString?把这部分源程序写出来看看.
gois 2002-06-18
  • 打赏
  • 举报
回复
我的编译器是C++ Builder 6.0
出错的语句是cout<<s<<endl;
//[C++ Error] Unit1.cpp(27): E2094 'operator<<' not implemented in type 'ostream' for arguments of type 'AnsiString'

还有如果这个文件的内容如下:
abcd
<EOF>

那么s[0]在运行过程中出现了严重错误
而s[1]却是第一个字符,s[2]却是第二字符,依次类推...


你能解释一下吗?
gois 2002-06-18
  • 打赏
  • 举报
回复
我的编译器是C++ Builder 6.0
出错的语句是cout<<s<<endl;
//[C++ Error] Unit1.cpp(27): E2094 'operator<<' not implemented in type 'ostream' for arguments of type 'AnsiString'

还有如果这个文件的内容如下:
abcd
<EOF>

那么s[0]在运行过程中出现了严重错误
而s[1]却是第一个字符,s[2]却是第二字符,依次类推...


你能解释一下吗?
pi1ot 2002-06-18
  • 打赏
  • 举报
回复
怎么可能

#include<iostream>
#include<fstream>
#include<strstream>
#include<string>

using namespace std;

int main()
{
ifstream infile( "file.txt" );
ostrstream oss;
oss << infile.rdbuf() << ends;
string s = oss.str();
cout << s << endl;
}

我用g++ 2.95.3编译运行了,没有任何问题.
gois 2002-06-18
  • 打赏
  • 举报
回复
To pi1ot(pilot):
你有没有亲自试一试呢,你所说的都通不过编译!
  你能解释一下吗,多谢了?
其实我也对我的试验结果感到奇怪!
pi1ot 2002-06-18
  • 打赏
  • 举报
回复
cout << s 就可以了阿,string可以用[]取字符的,[0]是第一个字符.
gois 2002-06-18
  • 打赏
  • 举报
回复
To pi1ot(pilot)
  请问一下:如果要在标准输出上显示这个字符串s,应该用什么语句?
s[0]是什么,是不是这个文件的第一个字符?
s[1]又是什么?
pi1ot 2002-06-18
  • 打赏
  • 举报
回复
你需要include: <fstream>,<strstream>或者<sstream>
xiaoxiangyy 2002-06-18
  • 打赏
  • 举报
回复
ostrstream 是一个string的输出流对象
rdbuf() 返回一个流对象 可以用它来构造istream 和ostream等
cure 2002-06-18
  • 打赏
  • 举报
回复
to pi1ot(pilot):
能不能解释一下
ifstream infile( filename );
ostrstream oss; //ostrstream是什么意思?
oss << infile.rdbuf() << ends; //rdbuf()是什么?
string s = oss.str();
pi1ot 2002-06-14
  • 打赏
  • 举报
回复
写错了
ostrstream oss << infile.rdbuf() << ends;
应该是
ostrstream oss;
oss << infile.rdbuf() << ends;
pi1ot 2002-06-14
  • 打赏
  • 举报
回复
写错了
ostrstream oss << infile.rdbuf() << ends;
应该是
ostrstream oss;
oss << infile.rdbuf() << ends;
加载更多回复(6)

69,371

社区成员

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

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