data(),size(),binary的用意或其功能????

LIQING LIN 2008-08-18 04:33:31
void ClientData::setLastName( string lastNameString )
{
const char *lastNameValue = lastNameString.data();////////////////
int length = lastNameString.size();////////////////
length = ( length < 15 ? length : 14 );
strncpy( lastName, lastNameValue, length );
lastName[ length ] = '\0';
}

fstream outCredit( "credit.dat", ios::in | ios::out | ios::binary );//////////
//
谁能解释一下.data() .size() binary 的用意,或者解释其功能
...全文
144 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
LIQING LIN 2008-08-25
  • 打赏
  • 举报
回复
这《C++标准程序库—自修教程与参考手册》哪里有完整版的啊,难到只能市校图书馆骂,有完整版下载的说一下地址啊
guzhilei1986 2008-08-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 biosli 的回复:]
你可以查查STL的手册。有本书来着:《C++标准程序库—自修教程与参考手册》。
就你问题来说string::data()是指得到一个char的数组。string::size()表示这个数组的大小。
ios::binary是标志位,表示不要替换特殊字符。
[/Quote]
biosli 2008-08-18
  • 打赏
  • 举报
回复
ios::binary是标志位,表示不要替换特殊字符。
主要指不要替换"\r\n"什么的字符。
K行天下 2008-08-18
  • 打赏
  • 举报
回复
data()的函数原型是:
const char* data() const;
返回和string内容相同 的字符串指针
例子:

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

int main ()
{
int length;

string str = "Test string";
char* cstr = "Test string";

if ( str.length() == strlen (cstr) )
{
cout << "str and cstr have the same length.\n";

length = str.length();

if ( memcmp (cstr, str.data(), length ) == 0 )
cout << "str and cstr have the same content.\n";
}
return 0;
}

Output:
str and cstr have the same length.str and cstr have the same content.


size()返回string的字符长度如string str ("1");的size()返回1

ios::binary 以二进制方式处理文件流
biosli 2008-08-18
  • 打赏
  • 举报
回复
你可以查查STL的手册。有本书来着:《C++标准程序库—自修教程与参考手册》。
就你问题来说string::data()是指得到一个char的数组。string::size()表示这个数组的大小。
ios::binary是标志位,表示不要替换特殊字符。
LIQING LIN 2008-08-18
  • 打赏
  • 举报
回复
data() .size()这两个函数的定义,binary是什么

65,186

社区成员

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

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