Buffer not writing correctly

I am trying to write FFFFFFFFFF to a binary file using a buffer size of 5. When I open the file up in a hex editor I get FF00FFFFFF. I cant figure out why. Here is the code. If anyone knows please let me know.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string> 
#include <cmath>
#include <ctime>
#include <cstdio>
#include <iostream>
// ============================================================================================
// Declare namespace
using namespace std;
// ============================================================================================
// ======================================================================================================================
long long binaryreadwrite(string whatdo, string file1, long long byteposition, long long byte, long long buffersize)
{

     char buffer(buffersize);
     char pause;
     long long byte1 = byte;
     long long count1 = byteposition;
     long long begin1;
     
       
     // open file
      fstream myfile1(file1.c_str(), ios::out  | ios::in | ios::binary);
     if (!myfile1)
     {
          myfile1.close();
          cout << "error in line 266" << " " << file1 << "\n";
          cin >> pause;
          exit(1);
     }
     if (whatdo == "read")
     {
// cout << byte1 << " We are reading bytes " << "\n"; // debug code
          myfile1.seekg(count1);
          myfile1.read( reinterpret_cast<char*>( &buffer ),buffersize);
          begin1 = myfile1.tellg();
          byte1 = (int)buffer;
          myfile1.close();
          myfile1.clear();
          myfile1.flush();
          return(byte1);
     }
     if (whatdo == "write")
     {
// cout << byte1 << " We are writing bytes " << "\n"; // debug code
          buffer = (char)byte1;
          myfile1.seekp(count1);
          myfile1.write( reinterpret_cast<char*>( &buffer ),buffersize);
          begin1 = myfile1.tellp();
          myfile1.close();
          myfile1.clear();
          myfile1.flush();
          return(0);
     } 
     

     cout << "Error in binary read and write" << "\n";
     cin >> pause;
     exit(1);
}
// ============================================================================================   
// declare main
int main (int argc, char *argv[])
{
    // Declare variables
    long long buffersize;
    long long count1;
    long long holdme;
    long long dummyfile;
    long long byte;
    string whatdo;
    string pause;
    string file1;
    cout << "What is your buffer size" << "\n";
    cin >> holdme;
    file1 = "temp.bin";
    // open file
    fstream myfile1(file1.c_str(), ios::out  | ios::binary);
    if (!myfile1)
    {
         myfile1.close();
         myfile1.clear();
         myfile1.flush();
         cout << "error in line 42" << " " << file1 << "\n";
         cin >> pause;
         exit(1);
    }
    myfile1.close();
    myfile1.clear();
    myfile1.flush();
    buffersize = holdme; // I have been entering 5 here
    byte = 1099511627775; // This should be FFFFFFFFFF in Hexadecimal.
    count1 = 0;
    whatdo = "write";
    dummyfile = binaryreadwrite(whatdo, file1, count1, byte, buffersize);
    exit(0);          
    
}
Last edited on
 
char buffer(buffersize);

This creates a single char that is initialized with the value of buffersize.
Last edited on
I am trying to write FFFFFFFFFF to a binary file using a buffer size of 5....
char buffer(buffersize);

Your buffer is a single char with a size of 1 byte, so your read and write calls result in undefined behavior.

Topic archived. No new replies allowed.