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

系列C++有关问题之四:NULL charater & get file sizes

2012-02-21 
系列C++问题请教高手之四:NULL charater & get file sizes1。我记得好像有一种字符串类型可以存NULL字符的,

系列C++问题请教高手之四:NULL charater & get file sizes
1。我记得好像有一种字符串类型可以存NULL字符的,即NULL字符也会算在这个字符串里面,但试了下面CString和string两种都不行

CString   str1( "\x20\x30\0\x0\40 ");
int   i   =   str1.GetLength();

std::string   str2;
str2   =   "\x20\x30\0\x0\40 ";
int   j   =   str2.length();

请问什么类型可以啊?

2。请问一个文件流ifstream   in;怎么取得它的文件大小:我用WORD   out_size   =GetFileSize(in,   NULL);取得的结果不对。。。。。
难道用C++我要用如下代码来取得流文件大小?

ifstream   in;
in.open( "1.bin ",   ios::binary);
WORD   out_size   =   0;
  char   ch;
  while(in.get(ch))
  {  
      out_size++;
 
    }
  in.close();

[解决办法]
1. 这个估计不行吧
2. 用seek/tell吧
[解决办法]
str2.push_back( '\x20 ');
str2.push_back( '\x30 ');
str2.push_back( '\0 ');
str2.push_back( '\40 ');

[解决办法]
2.可以计算大小
long FileSize(FILE *stream)
{
long curpos1,curpos2,length;
curpos1=ftell(stream);
fseek(stream,0L,SEEK_END);
curpos2=ftell(stream);
length=curpos1-curpos2;
fseek(stream,curpos1,SEEK_SET);
return length;
}
[解决办法]
原始的const char*就可以,只不过你不知道长度而已
[解决办法]
1.
std::string str2;
//str2 = "\x20\x30\0\x0\40 ";
str2.push_back( '\x20 ');
str2.push_back( '\x30 ');
str2.push_back( '\0 ');
str2.push_back( '\40 ');

int j = str2.length();

2.
ifstream in;
in.open( "1.bin ", ios::binary);
in.seekg(0, ios::end);
unsigned int nBytes = in.tellg(); // the file size



[解决办法]
请问一个文件流ifstream in;怎么取得它的文件大小:我用WORD out_size =GetFileSize(in, NULL);取得的结果不对。。。。。
---------------------

把文件指针移到文件末尾,用seek()函数

再调用tell()函数,返回值就是文件的长度了。。。


楼主记得散分哈
[解决办法]
str2.insert(str2.end(), '\x20 ');
str2.insert(str2.end(), '\x30 ');
str2.insert(str2.end(), '\0 ');
str2.insert(str2.end(), '\40 ');
[解决办法]
还有问题请教:构造函数中为什么不行?因为它把参数作为char *看作,遇到 '\x0 '就终止?
===>
Yes:)

2. CString
int Insert(
int iIndex,
PCXSTR psz
);
int Insert(
int iIndex,
XCHAR ch
);



Parameters
iIndex
The index of the character before which the insertion will take place.

psz
A pointer to the substring to be inserted.

ch
The character to be inserted.

Return Value
The length of the changed string.

Remarks
The iIndex parameter identifies the first character that will be moved to make room for the character or substring. If nIndex is zero, the insertion will occur before the entire string. If nIndex is higher than the length of the string, the function will concatenate the present string and the new material provided by either ch or psz.



[解决办法]
第一,你为什么不直接读入string类型?
第二,你于此处,应该用vector
[解决办法]
去看Effective STL item 29
[解决办法]
1..构造std::string的时候你必须指定字符串的长度,这样string就会拷贝你所指定的字节数而不管字节是否是\0.
2.你可以移动文件指针

热点排行