我想一个基本的输入,输出(及附加)在C++中,这里是我的代码二进制文件的输入,输出和追加C++
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void escribir(const char *);
void leer(const char *);
int main()
{
escribir("example.bin");
leer("example.bin");
system("pause");
return 0;
}
void escribir(const char *archivo)
{
ofstream file (archivo,ios::app|ios::binary|ios::ate);
if (file.is_open())
{
file<<"hello";
cout<<"ok"<<endl;
}
else
{
cout<<"no ok"<<endl;
}
file.close();
}
void leer(const char *archivo)
{
ifstream::pos_type size;
char * memblock;
ifstream file (archivo,ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout<< memblock<<endl;
delete[] memblock;
}
else
{
cout << "no ok"<<endl;
}
}
它运行良好的第一次,但是当我运行它第二次将“hello”和一些extrange字符添加到文件中。
你能帮我弄清楚有什么问题吗?
在此先感谢
我不能重现此:我得到完全 “hellohello”'68 1207 6C 6C 6F 68 6C 6F 6C 6f'与VC10 。你能发布文件的十六进制内容吗? – 2011-06-03 02:39:05
Argh。没有看到滚动条。 – 2011-06-03 03:04:56