Passing Fixed Character Array into Binary File

Apr 19, 2014 at 7:43pm
all i want to do is to read a fixed char array sized 4 from user and pass it to Binary File
then Print Encrypted content from the the File to the console screen ..
but it seems it prints the same input every time ..
and i tried everything ..
it works fine with integers and strings ..
but when it come to char array nothing ..
i don't understand what is the problem !
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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main()
{
   char buffer[4];
   memset(buffer,0,4);

   fstream FILE( "File.dat" ,ios::in | ios::out | ios::binary | ios::trunc);
   for(int i=0; i<4; i++)
   {
       cin>> buffer[i];
   }

   FILE.write((char*)buffer, 4);

   FILE.seekg(0, ios::beg);
   string cl;
   while(!FILE.eof())
   {
       getline(FILE,cl);
       cout<<cl<<endl;
   }

   FILE.close();
}
Apr 19, 2014 at 8:21pm
After fixing line 3 (you should be including <string>, not <cstring>), this works as expected. Though I think I expect something different from you.

The reason the output is the same as the input is because there is no "encryption" here. You're not encrypting anything, you're just writing characters to a file, then reading them back. So logically, whatever you read back is going to be the same as what you wrote.


I think your confusion comes from the conversion of characters to integers. Let me back up and explain what happens here...


For all of these examples, let's say the user inputs the string "1111".

With your above char array, each of those '1' characters will be treated as characters. The ASCII code for the '1' character is 0x31 (or 49 if you don't like hex). Try this out:

1
2
3
4
if('1' == 0x31) // or ('1' == 49)
{
  cout << "this will print";
}


So if you have a 4-element char array, with each char containing the '1' character, when you write this to a binary file, it will write the binary value of whatever is contained in those characters. In this case... 0x31.

If you were to open the file in a hex editor, you would see this:

31 31 31 31




Now... same thing.. but instead of putting that input in a char array.. let's put it in an int:
1
2
3
4
   int buffer;

   fstream FILE( "File.dat" ,ios::in | ios::out | ios::binary | ios::trunc);
   cin >> buffer;


Here... cin acts differently. Instead of taking the characters the user inputs and copying them directly over (like it does for chars), it will attempt to convert their input into an integral value. So "1111" the string becomes 1111 the integer value.

While the char array stores each individual digit as a character, an int doesn't do that. Instead the 4 bytes represent the value in 2's compliment fashion.

So if you were to write this int to a file... then open the file in a hex editor... you would see this:


57 04 00 00


This is because 0x00000457 == 1111
Apr 20, 2014 at 5:52am
I understood what you said !
but is thought if i work with binary files no body can directly edits it as it's content will be something like this :
È  潊湨
when i try to open it in any text editor
Apr 20, 2014 at 7:47am
If you're writing text to a file, it will appear in the file as text.

You only get garbage characters like that when you try to read non-textual data as text.


Also anyone can edit any file. If not with a text editor.... then with a hex editor.
Last edited on Apr 20, 2014 at 7:55am
Apr 20, 2014 at 7:59am
you got to my point ... i want it not to be able to be edited in text editor .. only through console or Hex editor ! .. can i do that ?!
Apr 20, 2014 at 8:04am
Just don't write any text data to the file. Any text that is written to a file will be visible in a text editor.

IE... "1111" as a string is text.
1111 as a number is not text.
Apr 20, 2014 at 8:08am
Okay thanks alot :)
Topic archived. No new replies allowed.