[C++] Writing binary file

Hi everyone,

I have the following program:

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
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

/* Defino la estructura*/ 
struct tipoRegistro 
{
   char pri[242];
   long long num1;
   long long num2;
   long long num3;
   long long num4;
   long long num5;
   long long num6;
   long long num7;
   long long num8;
   char ter[273];
};

int main() 
{

	
   tipoRegistro tran;
   
  
   ofstream fsalida("TEMP", ios::out | ios::binary);
   
 
   
   
   strcpy(tran.pri,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
   
  
   
   tran.num1=0;
   tran.num2=0;
   tran.num3=0;
   tran.num4=0;
   tran.num5=0;
   tran.num6=0;
   tran.num7=0;
   tran.num8=0;
   
   
  
   
   strcpy(tran.ter,"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
   
 

   cout << sizeof(tipoRegistro) << endl;
   
   fsalida.write(reinterpret_cast<char *>(&tran), sizeof(tipoRegistro));
   fsalida.close();

   return 0;
}


Three questions:

1) How can i put a new line when i write two record? For example, if i put two "fsalida.write" in the program, it not generates two records, it puts one continuos to other. I tried with "\n" but does not work well.

2) The char buffer for "TER" has 273 characters, but i need only 272 (B count). If i use only 272 in char buffer, the program crash.

3) When in open with notepad++ the file, i see binary characters at the end of the record. I spected see the record finish with B.

Thank's a lot!
Sorry for my english.

1) > it not generates two records, it puts one continuos to other.
┬┐and that's a problem because...? You know the size of the register, so you know exactly where each one starts and ends.

2) You forgot the '\0'. strcpy() will put it automatically, you may use memcpy() instead.

3) Probably padding. You object is bigger than necessary in order to optimize tasks.
Or a bug with that editor, you should use an `hex' viewer when dealing with binary files.
1) I need generate the newline, becouse other program need that format.

Example

AAAA....[BINARY NUMS]....BBBB
AAAA....[BINARY NUMS]....BBBB
AAAA....[BINARY NUMS]....BBBB
AAAA....[BINARY NUMS]....BBBB

2) oK, i will tray, but how can i use "memcpy()" and '\0' in the previus code? Can you do take mi code and modificate for example?

3) The problem probably is asoiciated with the previus point.

thanks!!!
1) You'll need to write in text mode fsalida << '\n'; (or make the translation) http://en.wikipedia.org/wiki/Newline#In_programming_languages

2)
1
2
memcpy( dest, "test", 4 ); //write 4 characters, 't' 'e' 's' 't'
strcpy( dest, "test" ); //write 5 characters, 't' 'e' 's' 't' '\0' 
If you're trying to produce a file in a specified format (241 character string, 8 64-bit numbers, 272-character string, endline), that's exactly what you should do. Your program produces a file that contains the binary representations of struct objects, which is not the same thing.

if you'd like to keep it in a struct, you could give it its own operator>>, and use that:

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
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

struct tipoRegistro 
{
   char pri[241];
   long long num[8];
   char ter[272]; // you said you only need 272

   tipoRegistro() {
        fill_n(pri, sizeof pri, 'A');
        fill_n(num, 8, 0);
        fill_n(ter, sizeof ter, 'B');
   }
};
ostream& operator<<(ostream& os, tipoRegistro& t)
{
    os.write(t.pri, sizeof t.pri);
    for(int n = 0; n < 8; ++n)
        os.write(reinterpret_cast<char*>(&t.num[n]), sizeof t.num[n]);
        // may need htonll(t.num[n])) depending on desired byte order
    os.write(t.ter, sizeof t.ter);
    return os;
}

int main() 
{
   tipoRegistro tran;
   ofstream fsalida("TEMP", ios::binary);
   fsalida << tran << '\n';
   tran.num[0] = 1;
   fsalida << tran << '\n';
   tran.num[1] = 1;
   fsalida << tran << '\n';
}
Last edited on
Topic archived. No new replies allowed.