File editing help!

Pages: 123
Hello. I am trying to make a program that edits an .msbt file, which is a
file containing a game's text. In this case a file containing unicode char
ID's, it's a software keyboard file telling the keyboard what keys to use.

Anyway,

My question is, how would I make a code that edits the file at a certain offset, because, for example the ID for the letter a is at an offset that
I want the code to edit.

My code would edit the number at the offset and save the file.


Something like:
1
2
3
4
5
6
7
8
#include ... //fstream, etc.

int main()
{
//code to get the whole file content in 1 line (maybe using getline())

//....Please help!!!
}


Tell me if clarification is needed.
if you want offsets treat it as a binary file.
all text files are binary files.
not all binary files are text files.

any tutorial on binary files will show you how to move around, basically seek() and tell() type things.
alternately if the file is small or you prefer it you can pull it into a large vector of bytes or a string or something and use [] to get to offsets, then write your buffer back over the original when done.

if you add or remove bytes, you have to deal with that. If its just overwriting a byte or two you do not, do you understand this?
Last edited on
Ok, but my main problem here is getting the whole file as one string, how do I do this?

I do not know how to use binary by the way. How would I do this?
Last edited on
Here's a fairly awful but succinct function:
1
2
3
4
auto streambuf_contents(std::istream& stream)
{
  return std::string(std::istreambuf_iterator{stream}, {});
}
Last edited on
What does auto mean? And what does the code do?
Puts the contents of the stream (in your case, a file stream), into a std::string.
auto deduces the return type automatically... in this case it's just a std::string.

If you actually showed us an example of this file format you're dealing with, someone might be able to give more specific help. If the game you're talking about has a wiki, it might have some file format information. For example, the classic Fallout games used "FRM" files, and their format is documented at https://fallout.fandom.com/wiki/FRM_file
Last edited on
Here is the file content:

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
MsgStdBnÿþ     0            LBL1¤          e       ,      ,     ,     L     …     –     §      Ç     Ç      ×     ×     ç          (     =     O     e      |      |     |     ¶     Ó     å                     K      ^      ^     ^     º      æ     æ      û     û     5     H     p     ¬     å     	     <     T     k      ~      ~     ~     –      ¨      ¨     ¨      Á      Á     Á     è     ú      J     J     ]     Á      ×      ×     ×     ç     <	      ,	     ,	     F	     	     º	     >
     ,
     T
     g
     w
     ‹
     ¢
     Ô
     ó
     ,     9     F     X     g     |     ®      Ø      Ø      Ø      Ø     Ø               -      ?      ?     ?      O     O     t     ‹  char_type_00   
grid_spaceP   char_type_01   qwerty_keytop_c]   kana50_hiragana<   char_type_02   char_type_03	   char_type_04
   
dlg_dic_pt   qwerty_eiji*   qwerty_conv)   cell_input_hiraZ   dlg_banned_char_err}   qwerty_backspace-   cell_input_alpha\   
qwerty_dic_en    kana50_handakuten:   bottom_2btn_decide   qwerty_keytop_s<   
qwerty_dic_es"   dlg_parental_ups   
dlg_dic_fr
   	dlg_1b_okr   
dlg_dic_title   kana50_keytop_h.   euro_keytop_04_cL   
dlg_dic_ru   kana50_keytop_k2   &dlg_parental_err_msetx   grid_backspaceR   kana50_sutegana;   euro_keytop_02_cH   	grid_capsO   cell_dic_eni   tenkey_backspaceq   kana50_keytop_k_daku3   dlg_ngword_erry   kana50_backspace>   bottom_3btn_decide   sign_keytop_00?   cell_dic_esk   sign_keytop_01@   sign_keytop_02A   euro_keytop_00_cD   qwerty_keytop_ru_c   sign_keytop_03B   
dlg_no_number{   
qwerty_dic_nl'   kana50_keytop_h_daku/   	cell_capsa   qwerty_keytop_ru   
kana50_bar6   bottom_2btn_cancel   ]kana50_keytop_k_handaku4   cell_input_cyrillic_   qwerty_keytop_jp_c
   dlg_dic_decide&   dlg_parental_ng_numu   
qwerty_dic_de#   kana50_keytop_k_sute5   qwerty_keytop_ru_s   cell_dic_nlp   
qwerty_dic_it$   qwerty_katakana,   cell_input_number^   cell_backspaced   dlg_atmark_err|   kana50_dakuten9   bottom_3btn_cancel   
dlg_dic_en   kana50_keytop_h_sute1   kana50_space7   cell_input_kata[   cell_keytop_alphaU   cell_dic_del   
dlg_dic_es   
qwerty_dic_pt&   kana50_conv8   dlg_parental_okt   &cell_input_cyrillic_c`   dlg_dic_cancel   qwerty_keytop_jp>   euro_keytop_00C   	grid_convQ   euro_keytop_01E   cell_keytop_alpha_cV   kana50_katakana=   euro_keytop_02G   euro_keytop_05_cN   cell_dic_itm   dlg_number_errz   euro_keytop_03I   euro_keytop_04K   cell_keytop_hiraS   euro_keytop_05M   qwerty_caps   bottom_3btn_etc   cell_input_alpha_c]   euro_keytop_03_cJ   
cell_spaceb   	cell_convc   
qwerty_dic_fr!   cell_l01e   ]kana50_keytop_h_handaku0   cell_l02f   cell_dic_pto   cell_l03g   cell_l04h   
qwerty_dic_ru%   
dlg_dic_nl   euro_keytop_01_cF   cell_keytop_numberW   [cell_keytop_cyrillic_cY   cell_keytop_kataT   dlg_parental_errw   qwerty_shift   qwerty_space(   qwerty_hiragana+   
dlg_dic_de   cell_dic_frj   
qwerty_keytop[   cell_dic_run   
dlg_dic_it   dlg_parental_ng_4v   bottom_1btn_decide    cell_keytop_cyrillicX   ««««««««««««ATR1           ~       ««««««««TXT2
          ~   ü      [  $  6  <  D  N  \  h  j       ®  ¾  Ì  Ü  ì          z  Ö  2  4  6  8  :  <  >  @  P  ^  n  |  Œ  œ  ²  ¾  À    Ä  Æ  È  Ê  Ì  Î  Ð  Ò  Ô  Ö  Ø  Ú  Ü  Þ  à  â  ä  æ  è  ê  ì  6  €  Ê  Ì    T  ž  è       R  „  ¦  È    P  R  T  V  X  Z  \  ´  	  8	  :	  <	  >	  @	  º	  4
  `
  b
  d
  f
  h
  j
  l
  n
  p
  x
  €
  †
  Œ
  ’
  ˜
  ž
  ¤
  ª
  °
  ²
  ¸
  º
  ¼
  ê
  ì
  ì  î  `  ž  Ü  Þ  O K   C a n c e l   O K   C a n c e l   I   F o r g o t   O K   A B C   M o r e   S y m b o l   P h o n e     S e l e c t   a   l a n g u a g e .   E n g l i s h   F r e n c h   S p a n i s h   G e r m a n   I t a l i a n   R u s s i a n   P o r t u g u e s e   D u t c h   C a n c e l     1 2 3 4 5 6 7 8 9 0 - q w e r t y u i o p a s d f g h j k l : [ ] z x c v b n m , . ' / @   1 2 3 4 5 6 7 8 9 0 - Q W E R T Y U I O P A S D F G H J K L : [ ] Z X C V B N M , . ' / @   ! \ # $ % ^ & * ( ) _ Q W E R T Y U I O P A S D F G H J K L ; { } Z X C V B N M < > " ? +                 E n g l i s h   F r e n c h   S p a n i s h   G e r m a n   I t a l i a n   R u s s i a n   P o r t u g u e s e   D u t c h                                                 . , · ' _ < > 
  : ; ¨ ` \ / ’º ª « » ( ) [ ] { } * 9 : "    ~ & #   ? ! ¿ ¡ !’!‘!“!
"@ § ¶   ! | ¦   ¤ $ ¢ ¬ £ ¥ "!© ® < > + - × ÷ = ± "  ^ ÇØÙ¸ ÛÜ2 3 µ ° ¹ ² ³ m&j&" & ¬ ¯ % 0 ¼ ½ ¾ Ò!Ô!Ï%Æ% %²%¼%&&@&B&   à àààààààwàyàzà{à|à}à~àà	à
à ò ó ô õ ö ø Sù ú û ü ý ÿ þ ß   À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö Ø RÙ Ú Û Ü Ý xÞ ß   a~
]>!#'+/i 137:<>BDFHQUY[_e  `} 

[< "&*.0I 269;=ACEGPTXZ^d  koqsz|      ’Æó  jnpry{      ’Åò
  ±²³´µ¶·¸¹º»¼½¾¿ÀÁÃÄÅÆÇÈÉ  ‘’“”•–—˜™š›œžŸ ¡£¤¥¦§¨©  ¬­®¯ÌÍÎÊː°Â~„…‡  †ˆ‰ŠŒŽª«°Â~„…‡  012345Q6789:;<=>?@ABCDEFGHIJKLMNO  &[]<>
 !"#$%&'()*+,-./              . , ? @ 
 àà	à

 ààwf 
 g h i 
 j k l 
 m n o 
 p q r s 
 t u v 
 w x y z 
 
 Wà0 
   . , ? @ 
 A B C 
 D E F 
 G H I 
 J K L 
 M N O 
 P Q R S 
 T U V 
 W X Y Z 
 
 Wà0 
   1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 
 0 
           . , ? ! - ' : @ / $ # & 1 
 a b c 2 
 d e f 3 
 g h i 4 
 j k l 5 
 m n o 6 
 p q r s 7 
 t u v 8 
 w x y z 9 
 
   0 
   . , ? ! - ' : @ / $ # & 1 
 A B C 2 
 D E F 3 
 G H I 4 
 J K L 5 
 M N O 6 
 P Q R S 7 
 T U V 8 
 W X Y Z 9 
 
   0 
   1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 
 0 
                   A b c   1 2 3   E N   F R   E S   D E   I T   R U   P T   N L     O K       T h i s   P I N   i s   i n c o r r e c t .     T o   c h a n g e   y o u r   P a r e n t a l 
 C o n t r o l   s e t t i n g s ,   p l e a s e   g o   t o 
 P a r e n t a l   C o n t r o l s   i n   t h e 
 S y s t e m   S e t t i n g s   a n d   a n s w e r 
 t h e   s e c r e t   q u e s t i o n .     T h i s   c o n t a i n s   i n a p p r o p r i a t e 
 l a n g u a g e   a n d   c a n n o t   b e   u s e d .   Y o u   c a n   e n t e r   u p   t o   % d 
 d i g i t s .   T h i s   c h a r a c t e r   c a n n o t 
 b e   u s e d .     % l c   c a n n o t   b e   u s e d .   ««««««««««TSY1ø                             á   â   ã   ä   å                              
   ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÚ   Û   «   «   «   «   «   «   «   «   Ü   Ü   Þ   ß   à   Ù   ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿY   _   _   [   \   `   ]   ^   Z   ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ›   œ   š   ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿt   u   n   p   q   r   s   o   o   o   o   o   o   o   o   ¨      ÿÿÿÿÿÿÿÿ   ÿÿÿÿ   ÿÿÿÿ         ÿÿÿÿ   ««««««««
Last edited on
It's a .LZ file, I don't know what LZ means.
It's a .LZ file, I don't know what LZ means.

https://bfy.tw/PJcm

Simply amazing the information the internet has available.
So, how can I get the whole file as an std::string?
By using mbozzi's function?

1
2
3
4
auto streambuf_contents(std::istream& stream)
{
  return std::string(std::istreambuf_iterator{stream}, {});
}


I tried this code:

1
2
3
4
5
6
7
8
9
10
11
12
#include <include.h> //iostream, etc.

auto streambuf_contents(std::istream& stream)
{
    return std::string(std::istreambuf_iterator{stream}, {});
}

int main()
{
	ifstream file("main.cpp");
	cout << streambuf_contents(file);
}


I get a ton of errors.
Last edited on
When opened in binary mode, you can put the exact contents of the file into a string. A string in this instance is just an array of bytes. But do you know what to then do with that string of bytes? If so, great. you're done. But if not, then you need to look up what the format of this msbt file is.

I searched the first part of your file (MsgStdBn) on the interwebs, and the first result is:
http://mk8.tockdom.com/wiki/MSBT_(File_Format)
which matches your first post's extension, so I am guessing that's indeed the file format you're looking for.

The second result is:
https://github.com/Kinnay/Nintendo-File-Formats/wiki/MSBT-File-Format

You can see the per-byte breakdown of the file format in those links.

To work with binary files, I suggest starting at this tutorial:
Disch's tutorial to good binary files
http://www.cplusplus.com/articles/DzywvCM9/
Last edited on
It's a compressed data file. You will need to uncompress the data in the file before/as you read the file.

There are 3rd party libraries available to deal with compressed data so your program "sees" the uncompressed data. Search the internet.

You will need to recompress ALL the data you read if you modify the data and want to save the file back in a format the game can understand.

This is IMO not a noob task, but it can be a great learning experience when you do the coding. Making mistakes is a great teaching tool.

You will screw up the data in the beginning so making copies of the game data before you start mucking around will save your butt.
There appears to some confusion, because the file that he actually pasted (the half-way readable binary contents) does not appear to be an LZ file, rather it appears to be the MSBT file format that he originally mentioned.

But while we're on the subject, Lzip can be used to decompress LZ files to actually access their contents.
http://lzip.nongnu.org/lzip.html
Last edited on
Well, I've downloaded a .bat file that uses tools like makerom.exe to decompress the .LZ files.

Also, with binary mode, do I just do:
1
2
3
string s;
ifstream f("File", std::ios::binary") //???
ifstream >> s; 

There appears to some confusion, because the file that he actually pasted (the half-way readable binary contents) does not appear to be an LZ file, rather it appears to be the MSBT file format that he originally mentioned.

But while we're on the subject, Lzip can be used to decompress LZ files to actually access their contents.


Is this because it is decompressed? Or what? it was called like: swkbd_lz.bin before decompression.
No idea. I'm just going off what's in this thread and what I glanced at on the links I posted.
Oh, ok. As to how to open a file in binary,
will this work?

1
2
3
4
string s;
ifstream f("File", std::ios::binary") //???
ifstream >> s;
  
Yeah, an MSBT format, and the link you gave, Ganado, has the source for an MSBT editor. But it is written in C#, not C++.

C#, *ick*

C# is not a bad programming language, as such, it's just not as OS generic as C++.
Oh, ok.
This compiles with C++17:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>

auto streambuf_contents(std::istream& stream)
{
	return std::string(std::istreambuf_iterator{stream}, {});
}

int main()
{
	std::ifstream fin("in", std::ios::binary);
	std::string bytes = streambuf_contents(fin);
}


It was the answer to your literal question from earlier. There are other ways to do this, look at https://stackoverflow.com/questions/2912520/read-file-contents-into-a-string-in-c
That being said, I probably don't even suggest doing it this way.

As I mentioned, for binary files I suggest looking at that tutorial link I pasted. A file is just an array of bytes, and the different groups of bytes have meaning. For example, in
0xAA 0xBB 0xCC 0xDD 0xFF
it might mean that byte indices 0 and 1 represent a 16-bit signed integer in little-endian format. Or maybe they're 2x 8-bit characters. It depends on what the file format says they are.
Last edited on
Pages: 123