将二进制数据写入私有流会产生意外的结果
Writing binary data to private ofstream produces unexpected result
我正在研究一个可以在文件中读取和写入二进制数据的类。我正在通过发送'a'
来测试它。当我把它发送给cout
并且它起作用时。我把它发送到一个文本文件,它发送了î。
导致这种情况的流有什么不同?
#include <iostream>
#include "bin2.h"
using namespace std;
int main()
{
bin myBin("e:\Temp\test.txt");
char data[1];
data[0] = 'a';
myBin.write(data, 1);
system("PAUSE");
return 0;
}
bin2.h
#pragma once
#include <fstream>
class bin
{
std::ofstream outfile;
std::ifstream infile;
std::filebuf *outBuff, *inBuff;
int buffSize = 5;
char* buffer;
//0 = input, 1 = output, 2 = ready to delete, 3 = unitialized
char mode = 3;
public:
//constructor with no parameters
bin(){ ; };
//if 'this' is constructed with a file, call init() to set object up
bin(char fileName[])
{
init(fileName);
};
void init(char fileName[])
{
try
{
//check if isUninitialized
if (!isUninitialized())
return;
//open the file and make sure it opened
outfile.open(fileName);
infile.open(fileName);
if (!outfile.is_open() || !infile.is_open())
throw std::runtime_error((std::string)"Failed to open file " + fileName);
//create buffer, get pointers to filebuffs, and set them to the new buffer
buffer = new char[buffSize];
outBuff = outfile.rdbuf();
outBuff->pubsetbuf(buffer, buffSize);
inBuff = infile.rdbuf();
inBuff->pubsetbuf(buffer, buffSize);
//set mode to input
mode = 0;
return
}
//if any exceptions were thrown, call the cleanup then rethrow the exception so
// the caller can handle the error as well
catch (std::exception & ex) {
cleanup();
throw ex;
}
};
virtual ~bin(){
cleanup();
};
//methods to check mode
bool modeIsInput(){ return mode == 0; };
bool modeIsOutput(){ return mode == 1; };
bool isReadyToDel(){ return mode == 2; };
bool isUninitialized(){ return mode == 3; };
std::string getMode(){
switch (mode) {
case 0: return "input";
case 1: return "output";
case 2: return "readyToDel";
case 3: return "unitialized";
default: return "invalid";
}
};
//method to write data into the object
bin * write(char data[], int length){
//make sure object is in input mode
if (mode != 0)
throw std::runtime_error("Cannot write to object when not in input mode. Current mode = " + getMode());
//DEBUG
std::cout << "Writing data: ";
std::cout.write(data, length);
std::cout << std::endl;
//end of DEBUG
//write data and return pointer to object
outfile.write(data, length);
return this;
};
private:
void cleanup()
{
delete buffer;
outfile.close();
infile.close();
//change mode to readyToDel
mode = 2;
};
};
你有:
bool modeIsInput(){ return mode = 0 ? true : false; };
bool modeIsOutput(){ return mode = 1 ? true : false; };
bool isReadyToDel(){ return mode = 2 ? true : false; };
bool isUninitialized(){ return mode = 3 ? true : false; };
我相信你的意思是:
bool modeIsInput(){ return mode == 0 ? true : false; };
bool modeIsOutput(){ return mode == 1 ? true : false; };
bool isReadyToDel(){ return mode == 2 ? true : false; };
bool isUninitialized(){ return mode == 3 ? true : false; };
或者更好。
bool modeIsInput(){ return mode == 0; };
bool modeIsOutput(){ return mode == 1; };
bool isReadyToDel(){ return mode == 2; };
bool isUninitialized(){ return mode == 3; };
我不知道解决这些问题是否会解决您的所有问题。
我按原样复制了我的代码并将其保存在不同的文件中。问题解决了,但我不知道是什么原因造成的。
相关文章:
- 尝试将字符串/字符转换为整数会产生意外结果
- RapidXML - 代码创建意外结果
- 类中静态函数C++意外结果
- 指针数组中的意外结果
- 使用指针访问数组元素时出现意外结果
- 使用 sprintf 和 %g 将双精度转换为字符串的意外结果
- C++:比较运算符>和字符串文本的意外结果
- 具有意外结果的 C++ 闭包
- yaml-cpp 比较的意外结果
- 每次都出现意外结果
- 在 Qt 中解析嵌套的 JSON 时出现意外结果(数组不存在)
- 如何避免 std::abs 的意外结果?
- 使用嵌套 if 语句的意外结果
- A ^= B ^= A ^= B;C# Visual Studio 中的意外结果
- 逻辑错误,我将获得意外结果
- 在 c++ 中使用异步的意外结果
- 从函数的返回值将元素C++存储到 std::vector 中时出现意外结果
- 执行递增和递减时"cout"链接会产生意外结果
- OpenCL - 内核方法返回意外结果
- C++使用 std::get_time 解析 YYMMDD ISO 8601 日期字符串会得到意外结果