c++ - Writing binary data to private ofstream produces unexpected result -
i working on class read , write binary data to/from file. testing sending 'a'
. when sent cout
, worked. sent text file, sent î.
what different ofstream causing this?
#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 delete, 3 = unitialized char mode = 3; public: //constructor no parameters bin(){ ; }; //if 'this' constructed file, call init() set object bin(char filename[]) { init(filename); }; void init(char filename[]) { try { //check if isuninitialized if (!isuninitialized()) return; //open file , make sure opened outfile.open(filename); infile.open(filename); if (!outfile.is_open() || !infile.is_open()) throw std::runtime_error((std::string)"failed open file " + filename); //create buffer, pointers filebuffs, , set them new buffer buffer = new char[buffsize]; outbuff = outfile.rdbuf(); outbuff->pubsetbuf(buffer, buffsize); inbuff = infile.rdbuf(); inbuff->pubsetbuf(buffer, buffsize); //set mode input mode = 0; return } //if exceptions thrown, call cleanup rethrow exception // caller can handle error catch (std::exception & ex) { cleanup(); throw ex; } }; virtual ~bin(){ cleanup(); }; //methods 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 write data object bin * write(char data[], int length){ //make sure object in input mode if (mode != 0) throw std::runtime_error("cannot write 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 , return pointer object outfile.write(data, length); return this; }; private: void cleanup() { delete buffer; outfile.close(); infile.close(); //change mode readytodel mode = 2; }; };
you have:
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; };
i sure meant:
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; };
or better still.
bool modeisinput(){ return mode == 0; }; bool modeisoutput(){ return mode == 1; }; bool isreadytodel(){ return mode == 2; }; bool isuninitialized(){ return mode == 3; };
i don't know whether fixing fix problems.
Comments
Post a Comment