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

一小段7行程序代码看不懂,请求解答.多谢你们

2012-03-01 
一小段7行程序代码看不懂,请求解答..谢谢你们boolopenout(strings){fstreamout(s.c_str(),ios::in|ios::ou

一小段7行程序代码看不懂,请求解答..谢谢你们
bool   openout(string   s){
fstream   out(s.c_str(),ios::in|ios::out|ios::ate);
    if(!out)   return   false;
    cout < <s+ "is   here.\n ";
    out < <s+ "is   ok.\n ";
    return   true:
}

不长的一段代码,我想问的是
1.s.c_str(),为什么要用c_str,c_str的作用本来是返回string内部存放字符串的缓冲区,   以0结尾.但是用在这个程序里有什么作用???

2.ios::in|ios::out|ios::ate,这个是什么用法??在这里表示什么意思??

3.out < <s+ "is   ok.\n ";,上面不是有一个cout < <s+ "is   here.\n ";,为什么还要加上这句呢?有什么作用?望能详细解答...

[解决办法]
查MSDN就知道啦
fstream( const char* szName, int nMode, int nProt = filebuf::openprot );
第一参数是const char *
ios::ate The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.


ios::in The file is opened for input. The original file (if it exists) will not be truncated.


ios::out The file is opened for output.

cout流到屏幕

out流到指定的对象

[解决办法]
ios::in|ios::out|ios::ate这个表示打开的文件是可写和可读的,并且在打开文件时定位到文件末尾(ate表示这个),,,

s.c_str()用在这里是表示打开的文件名字的意思,...,,,.没有文件名就不能打开文件了= =

out < <s+ "is ok.\n ";是把s+ "is ok.\n "字符串写入刚才所打开的文件中去,其实就是把
文件名字加个is ok.输到文件中去....
而cout < <s+ "is here.\n ";则是把s+ "is ok.\n "字符串字符串打印在屏幕上让我们看见
[解决办法]
c_str() 将string对象转化为c字符串,
fstream 是一个类, 用于文件的操作, 它的构造函数的第一个参数必须是c字符串,因此调用c_str()
ios::in, ios::out, ios::ate 标准的写法是 ios_base::in, ios_base::out, ios_base::ate
,它们都是预先定义好的常量,分别表示 以 "读 "方式打开文件 以 "写 "方式打开文件 以 "追加 "方式打开文件.
cout 是ostream的对象,用于在控制台输出(console output),这个对象已经预定义好了.
out 是fstream 的对象, 是在程序里面定义的, 用于在文件里面输出.

热点排行