#include <iostream>
#include <string>
#include <fstream>
using namespace std;int main()
{
cout<<"Input:";
string input;
cin>>input;
ofstream ifile("c:/t.txt",ios_base::binary);
if(!ifile)
cout<<"ERROR:Failed to open the file."<<endl;
ifile.write((char *)&input,sizeof input);
ifile.close(); string output;
ifstream ofile("c:/t.txt",ios_base::binary);
if(!ofile)
cout<<"ERROR:Failed to open the file."<<endl;
ofile.read((char*)&output, sizeof output);
cout<<"Reading:"<<output<<endl;
ofile.close();
return 0;
}编译通过,运行时,若输入大于15个字符就会出错,提示_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)和其他两个错误。很奇怪啊,,请高手点解一下。。
解决方案 »
- 键盘消息处理函数设置了断点为何还是无法调试阿
- 当前目录下明明就有头文件,为什么会提示找不到
- CPropertySheet类实现了Tab control,如何捕捉页面切换的事件
- IE发送到IIS的汉字数据,要经过编码吗?
- 关于我自己写的一个台球游戏的类CBall的问题!!很严重很严重,我为这个付出了2个通宵了!!!!
- 一个关于打印监控的问题,十万火急100分!!!!
- 关于从windowsC到liunx下的一个小问题?
- 多谢iori的解答,请进来领分。
- 怎样用 98 做 FTP 服务器......
- 有关HBITMAP的问题
- 编安装软件需要什么知识?大家推荐几本书吧。
- CString.Format简单问
这个string类,能自动调整大小,不能用sizeof()取其大小。因为他是个不定值。
你要往文件里写内容的话,可以直接使用:
ofile>>output;
期待 N人们的到来。。
读不能用string,要先读到一个自定的char buff[nMax],然后把它转换为string
ifstream ofile("c:/t.txt",ios_base::binary);
if(!ofile)
cout < <"ERROR:Failed to open the file." < <endl;
ofile.read((char*)&output, sizeof output);
cout < <"Reading:" < <output < <endl;
ofile.close();
return 0;
}
string是一个类,大小是固定的,里面保存指针,指针里面存的才是字符串
int main(int argc, char* argv[])
{
ifstream ifs( "temp", ios_base::binary);
ifs.seekg( 0 , ios_base::end );
size_t len = ifs.tellg();
ifs.seekg( 0 , ios_base::beg );
vector<char> vec(len+1);
ifs.read( &vec[0],len );
printf("%s",&vec[0]);
return 0;
}
大家说的挺对的啊收5楼启发,我写了一个小测验程序;
//test_string.cpp#include <iostream>
#include <string>
using namespace std;int main()
{
string str;
cout<<sizeof(str)<<endl;
return 0;
}输出为:32
谢谢4楼的指导,我会改的。