【发布时间】:2011-06-03 02:23:28
【问题描述】:
我正在尝试 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”和一些额外字符。
你能帮我找出问题所在吗?
提前致谢
【问题讨论】:
-
我无法重现这一点:我使用 VC10 得到的正是“hellohello”
68 6f 6c 6c 6f 68 6f 6c 6c 6f。你能发布文件的十六进制内容吗? -
啊。没有看到滚动条。