Data not saved in binary form - c++

I made a program as below
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<fstream.h>
void main() {
char name[24];
cout << "enter string :";
gets(name);
ofstream fout;
fout.open("bin_data",ios::out|ios::binary);
fout.write((char*)&name,10);
fout.close();
}
But when I open the file bin_data by notepad I find that the string is saved in text format not in binary form...... Please help...
This code can save a word of 10 char.
But when I compile this code by turbo c++ v4.5 I find that. When I input 1 or 2 letter word it saves in text format(ignore garbage value) but when I input a word of 3 to 7 letter long it saves in binary format. and in 9 and 10 letter word again in text format..... Can anyone tell me the reason...?
Please compile and run program as I mentioned above and answer

Your data only contains text. It is represented by the very same bits in both text format and binary format.
Binary format means that your data is written to the file unchanged. If you were to use text format, some non-text characters would be modified. For example, byte 10 (which represents newline) could be changed to operating system specific newline (two bytes, 15 and 10, on Windows).
For binary values of text characters, see http://www.asciitable.com/
Your second example has a buffer overflow.
char name[24];
fout.write((char*)&name,10);
You reserve 24 bytes of data, which is filled by random bytes that happen to be at that point of memory. When you save a 2-character string to the buffer, it only overwrites first three bytes. The third byte is set to value 0, which tells you that the text ends at that point. If you were to call strlen(), it would tell you the amount of characters before the first 0 byte.
If your input is a 2-character text, and you choose to write 10 bytes from your buffer, the 7 bytes in the end are filled with invalid data. Note that this does not cause an access violation, because you have reserved data for 24 bytes.
See also: https://en.wikipedia.org/wiki/Null-terminated_string

Related

C++ - A few quetions about my textbook's ASCII table

In my c++ textbook, there is an "ASCII Table of Printable Characters."
I noticed a few odd things that I would appreciate some clarification on:
Why do the values start with 32? I tested out a simple program and it has the following lines of code: char ch = 1; std::cout << ch << "\n"; of code and nothing printed out. So I am kind of curious as to why the values start at 32.
I noticed the last value, 127, was "Delete." What is this for, and what does it do?
I thought char can store 256 values, why is there only 127? (Please let me know if I have this wrong.)
Thanks in advance!
The printable characters start at 32. Below 32 there are non-printable characters (or control characters), such as BELL, TAB, NEWLINE etc.
DEL is a non-printable character that is equivalent to delete.
char can indeed store 256 values, but its signed-ness is implementation defined. If you need to store values from 0 to 255 then you need to explicitly specify unsigned char. Similarly from -128 to 127, have to specify signed char.
EDIT
The so called extended ASCII characters with codes >127 are not part of the ASCII standard. Their representation depends on the so called "code page" chosen by the operating system. For example, MS-DOS used to use such extended ASCII characters for drawing directory trees, window borders etc. If you changed the code page, you could have also used to display non-English characters etc.
It's a mapping between integers and characters plus other "control" "characters" like space, line feed and carriage return interpreted by display devices (possibly virtual). As such it is arbitrary, but they are organized by binary values.
32 is a power of 2 and an alphabet starts there.
Delete is the signal from your keyboard delete key.
At the time the code was designed only 7 bits were standard. Not all bytes (parts words) were 8 bits.

Outputting Huffman codes to file

I have a program that reads a file and saves the frequency of each character. It then constructs a huffman tree based on each character's frequency and then outputs to a file the huffman codes for the tree.
So an input like "Hello World" would output this sequence to a file:
01010101 0010 010 010 01010 0101010 000 01010 00101 010 0001
This makes sense because the most frequent characters have the shortest codes. The issue is, this increases the file size ten-fold. I realized the reason why is because each 1 and 0 is being represented in memory as its own character, so they get each get expanded out to a byte of data.
I was thinking what I could do is convert each code (E.G. "010") to a character and save that to file - but that still would pad the code to be a byte long (Or mess it up if the code is longer than a byte).
How do I go about this? I can give code snippets if needed - I'm basically saving each code into a string so that's why the file's coming out so big (It's outputting each "bit" as a byte). If I were to convert the code to a long for example, then a code like 00010 would be represented as 2 and a code like 010 would also be represented as 2.
You basically have to do it a byte (or a word) at a time. Maintain a byte which you fill with bits, and a record of how many bits have been filled in so far. When you get to 8, write the byte and start over with an empty one.

issues using stringstream to handle binary file

I'm working with a binary file that I need to grab its useful contents from. The structure is:
Based on a quick look at the file, you don't have an "unknown amt of nulls" anywhere. The format appears to be:
N Bytes: number of animals, integer as text delimited by '\n'
24 Bytes per animal:
16 Bytes: name of animal padded with 0
4 Bytes: some 32 bit number (little endian)
4 Bytes: another 32 bit number (little endian)
You shouldn't be reading this as a text file, but instead as a raw binary file. There's absolutely no need for a stringstream, you can simply parse the number of animals by reading in one byte at a time and adding to the previous value * 10 until you reach '\n'.

UDF decimal to binary

I wrote a decimal to binary converter function in order to practice my manipulation of number systems and arrays. I took the int a converted it to binary and stored each character, or so I beleive, in an array, then displayed to the screen, however it is displaying characters I do not know i looked them up on the aski table and do not recognize them, so i would like to ask for your assistance, here is a picture of the code, and console app.
Thanks in advance.
You likely want to insert number chars (such as '1') in your result, but you assign the char value. Try adding the value of '0' to get a readable result (remainder + '0').
If you interpret the result array as a string (that's what i suggested), you should also set the last char to the value 0 (not '0'!) to mark the end of the c string.
Your output function not correct output your binary text because:
1) cout output characters until '\0', so your function will correct output until get first 0 in binary representation of int (for example for 5 = 101 it will output only one smile with code 0x01).
2) your last character in array is not '\0', so cout will output garbage until '\0' or memory access exception.

How to modify a value in textual data file using C

while(!feof(fp))
{
fscanf(fp,"%d %s %d %d",&res[i].id,res[i].title,&res[i].price,&res[i].qty);
i++;
}
while(j<i)
{
printf("\nID:|%d|\tNAME:|%s|\tPRICE:|%d|\tQTY:|%d|",res[j].id,res[j].title,res[j].price,res[j].qty);
j++;
}
I have this piece of code which is collecting data from the file. Now I want to know if get an input from a user like res[id] and I want to decrease the quantity of that particular id how to do that?
If the file is in binary format it is easier to do what you want.
What is the difference between the text and the binary format? If the file is written in binary format, then a 32-bit integer will be represented as 32 consecutive bits in the file. While in text format the number will be represented as sequence of digits for instance 32.
So what's the big deal in that difference? Well if you replace 32 with 1243, in binary format the number will still take the same 32 bits so nothing else needs to be moved, all you change is these 4 bytes. While in the second case you add 2 more digits which will cause all the subsequent contents of the file to shift with two bytes.
In order to shift everything as needed, you will need to read the current contents of the file change the value and then write the contents back. I mean all the contents following the change you are doing.