Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
二进制文件输入、输出和附加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; } } #包括 #包括 #包括 #包括 使用名称空间std; 无效描述(常量字符*); 无效水平(常量字符*); int main() { 描述(“example.bin”); leer(“example.bin”); 系统(“暂停”); 返回0; } 无效描述(常量字符*archivo) { 流文件(archivo,ios::app | ios::binary | ios::ate); if(file.is_open()) { 文件_C++_Input_Stream_Append - Fatal编程技术网

二进制文件输入、输出和附加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; } } #包括 #包括 #包括 #包括 使用名称空间std; 无效描述(常量字符*); 无效水平(常量字符*); int main() { 描述(“example.bin”); leer(“example.bin”); 系统(“暂停”); 返回0; } 无效描述(常量字符*archivo) { 流文件(archivo,ios::app | ios::binary | ios::ate); if(file.is_open()) { 文件

二进制文件输入、输出和附加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; } } #包括 #包括 #包括 #包括 使用名称空间std; 无效描述(常量字符*); 无效水平(常量字符*); int main() { 描述(“example.bin”); leer(“example.bin”); 系统(“暂停”); 返回0; } 无效描述(常量字符*archivo) { 流文件(archivo,ios::app | ios::binary | ios::ate); if(file.is_open()) { 文件,c++,input,stream,append,C++,Input,Stream,Append,问题似乎不在于写入文件,而在于读取和显示文件,即: memblock = new char [size]; file.seekg (0, ios::beg); file.read (memblock, size); file.close(); cout<< memblock<<endl; memblock=newchar[size]; file.seekg(0,ios::beg); file.read(memblock,size); file.close(); cout

问题似乎不在于写入文件,而在于读取和显示文件,即:

memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout<< memblock<<endl;
memblock=newchar[size];
file.seekg(0,ios::beg);
file.read(memblock,size);
file.close();

cout我认为您的错误在输出上:

    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    cout<< memblock<<endl;

我无法复制这个:我用VC10获得了准确的“hellohello”
68 6f 6c 6f 68 6f 6c 6c 6f
。你能发布文件的十六进制内容吗?啊。没有看到滚动条。
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    cout<< memblock<<endl;
    memblock = new char [size + 1];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();
    memblock[size]='\0';

    cout<< memblock<<endl;