last day (15 days later) » 

7:07 PM
-1
Q: Reading and sorting binary file in c with read()

VanessaI have not used binary files till this moment and I got a little confused. Lets say we have this binary file 00000000: 4c6f 7265 6d20 6970 7375 6d20 646f 6c6f Lorem ipsum dolo 00000010: 7220 7369 7420 616d 6574 2c20 636f 6e73 r sit amet, cons 00000020: 6563 7465 7475 7220 6164 6970 6973 6369 ecte...

 
4c6f 7265 is 4 bytes, not one byte. One hexadecimal digit corresponds to 4 bits (1 nibble).
"when I am reading byte by byte, I am reading the following chars: 0, 0, ..., 0, 4, c, 6, f, 7, 2" so you want to deal with a dump of your binary file generatd by some tool instead of dealing with your binary file directly?
 
user17588651
So 4c is one byte? It confuses me because when I use read(fd, &b, 1), I read 1 byte but when I printf it It just printf only one char
 
1 ASCII character can be represented by 1 byte.
 
user17588651
Yes I work with I work with a binry file that is generated by some other tool
 
Is your binary file the text file containing "Lorem ipsum...", or is it the text file containing the hex dump output of xxd? Either way, those sure look like text files. Why are you thinking about them as binary files? In your mind, what is the definition of a "binary file"?
 
user17588651
7:07 PM
I have just used xxd
 
Don't use xxd, just open the file directly.
 
user17588651
If I open the file directly, then its not gonna contain the column in the right that contains the text, right?
 
user17588651
Okay, so from what I understood till now - read(fd, &b, 1) reads char by char from the file like 0, 0, 0, ..., 4, c, 6, f. One byte is represented like that - 4c, 6f, etc. If I wanna read a byte by byte I have to use char b[2] and then in a while loop read(fd, &b, 2), right? But later on how I am supposed to sort it?
 
What is "the file" that will provide things like "0, 0, 0, ..., 4, c, 6, f"? Try printing what are read before thinking too much.
 
user17588651
I tried this: while((size = read(fd, &b, 1)) == 1){ printf("%c \t", b); } and it just prints char by char.
 
7:07 PM
Try this: printf("%02x ", b)
 
user17588651
You are just confusing be even more
 
user17588651
I asked this question with the hope that things get clearer
 
Are you reading the xxd output?
 
user17588651
Yes, I am, the xxd output just contains the text in the right but everything else is the same?
 
user17588651
How I can sort when I have read 2 chars in the buff? Like when I have read 4c in my buff then how I can sort?
 
7:07 PM
What do you mean by "sort"? Sort by words in the Lorem ipsum text? You best route is to forget about binary files, and read as a text file. If you use fscanf with format %s that will break the file into words with minimal effort. Then all you need do is to remove punctuation.
 
user17588651
I wanna sort the binary file. Just forget about the Lorem ipsum part. I wanna sort the bytes.
 
user17588651
What would be the point to ask this question if I wanted to srt just the text file?
 
Think that you have an input file input_data with the data to dump (Lorem ipsum... in the example) and your compiled program a.out in your current working directory. What command will you execute to run your program?
 
I ask, because it isn't clear. You don't even understand what a binary file is, but insist that you need to use one. Please show a short extract of an actual binary file, and what you expect the output to be. This is a guessing game on all sides.
 
user17588651
I asked 2 times if the binary file should look like the one that I showed in the question but without the column with the text, apparently no one answered my question and everyone keeps asking even more questions when my previous ones are not answered.
 
7:07 PM
How contents of binary file look will depend on the viewer application.
 
No, please tell us what your binary file is. We have no idea. What you showed in the question is some representation of a binary file.
 
user17588651
So I just wanna sort the bytes that are showed in my question - 00000000: 4c6f 7265 6d20 6970 7375 6d20 646f 6c6f 00000010: 7220 7369 7420 616d 6574 2c20 636f 6e73 00000020: 6563 7465 7475 7220 6164 6970 6973 6369 00000030: 6e67 2065 6c69 742c 2073 6564 2064 6f20
 
user17588651
The bytes - not the text
 
OK then, is the first thing " 00000000: " part of the file itself, or is it generated by the output tool you used, or is it generated by a binary file viewer? Binary files do not usually contain that - it is put there by a file viewer.
 
The sample you showed originally is a text file. The fact that it is a text file demonstrating a hex dump of a file (which is also a text file) doesn't really change anything. Until you describe what you think a "binary file" is, no one can help you.
 
user17588651
7:07 PM
I just used it and this is the output
 
Yes, and that output is text file.
 
user17588651
Okay, so it is a text file. Lets say we have a REAL binary file. How I can sort it?
 
For now, forget about Lorem ipsum and pick any other file on your computer. Read it byte by byte in binary mode and count the frequency of each character in an array[256]. That automatically sorts the file, by byte value.
 
user17588651
Is there a way I can read in binary mode using read()?
 
There is no difference between a binary file and a text file. Only in the way you read it.
 
user17588651
7:07 PM
So when reading a binary file it read byte by byte - like 4c, 6f, etc, but when I read a text file - it reads char by char - 4, c, 6, f, etc. Did I understand it correctly?
 
Both of them are read byte by byte, or block by block (and a text file can be read word by word or line by line). If you use fgetc() that gives you each byte. if you want to convert newline styles open in text mode. If you want to read it "as is" open in binary mode.
 
user17588651
I mean when reading a binary file a byte is represented as two characters (like the ascii table)? But when read a text file each symbol is a byte? That confuses me a lot
 
No, a byte is a byte is a byte. But the textual representation which you showed, uses two text characters to represent each byte. Like I type here: 41 is the hexadecimal version of the character 'A'.
 
user17588651
Can you give me an example please? When I read the text file that is in the question byte by byte and print each byte I get 0, 0, 0, ..., 4, c, 6, f.... etc.
 
Please see previous comment. What you are reading is a textual representation of a binary file. Each byte is expanded to two textual byes, in hexadecimal.
 
user17588651
7:07 PM
Okay but when I read byte by byte and if I have 41 the hexdec vers of 'A' then it will read 4 and then 1, so 4 in a single byte and 1 is a single byte?
 
It is pointless reading this as a binary file. It is a textual dump of a binary file.
 
user17588651
I get that 4c 6f 72 65 is L o r e, BUT WHEN USING char b and then in a while loop read(fd, &b, 1) and then printing it I get 4, c, 6, f, 7, 2, 6, 5 so this is reading byte by byte, thats just the hexdecimal representation of the Lorem ipsum text. BUT WHEN READING A BINARY FILE that lets say looks like this 4c 6f 72 65 it will read byte by byte but it read 4c as a whole, I mean reads it like L?
 
We have been trying very hard. We want to answer. Suppose you go to your bank andat window A you ask for the money in you account and they give you a statement saying $100 on it. Then at window B you ask the same thing and they give you a $100 bill. That was your actual money and the first window gave you a textual representation of the money. You can't spend it. You have a textual represenation of the binary file.
 
user17588651
I asked many questions and there are some above your last comment that just got ignored. I also asked if there is a way to read a binary file with the func READ.
 
It isn't the actual Lorem Ipsum file. That's what you need, not the output of some other tool. read() does what it says in the text books and man pages, and nobody is going to repeat that for you. I have been trying to answer your questions.
 
7:07 PM
@Vanessa WeatherVane is not ignoring your questions. You have a fundamental misunderstanding that needs to be resolved. You have a text file. You can read a text file with read. Text is stored in a "binary" format, but you are making an artificial distinction that is confusing you.
 
user17588651
The thing is that I have to work only with the file that I have posted in the question, I cant work with the text file directly.
 
user17588651
I have to work with a binary file that is given to me and sort it, thats just the deal
 
user17588651
I dont have the real text file, I just have the binary representation with the bytes and I have to sort the bytes
 
user17588651
I dont know what else I gotta say, I asked so many times the same thing and still no answer or some help
 
user17588651
I have the the man page for read and there is nothing about binary files and I want to use read(2)
 
7:14 PM
So you actually want to read "binary dump of a file", not raw binary files? Then, what to do looks like 1. read the characters used in the dump 2. parse them to bytes 3. "sort" the bytes in whatever way you like
 
user17588651
Yes, I wanna read a binary dump, my question is - when read 2 chars into a buff, lets say I have read 4c or 6f, how I can parse it into a byte or a char so I can store everyone into an array so later on I can sort it?
 
The "binary representation" with the bytes is a text file. Perhaps you want to use xxd -r to generate another text file. You keep talking about a "binary" files, but you keep showing text files. You need to clarify in your mind what
 
sscanf() and/or strtol() should be useful to convert hexadecimal strings to integers (values of bytes).
 
user17588651
I saw in other discussions that people have suggested (b[0] << 8) | b[1], is this the same?
 
No. 1. You have to first convert characters like '0' and 'a' to corresponding integers (nibble) like 0 and 10. 2. A byte is 8 bits (in typical modern computers, at least), so shifting by 8 doesn't make sense for constructing a byte. Shifting by 4 should be useful to combine 2 nibbles to 1 byte.
 
7:32 PM
Suppose you have char b[2] which contains the 4c (this is not a string, as it's not null terminated). Then 0x4c == 16 * b[0] - '0' + b[1] - 'a' + 10. The only tricky part is distinguishing between 0-9 and a-f and dealing with A-F.
 
@WilliamPursell You forgot parenthesis: 0x4c == 16 * (b[0] - '0') + (b[1] - 'a' + 10) The second pair of parenthesis is optional, but first one is required because there is a multiplication.
 
user17588651
I tried this: int num = 16 * (b[0] - '0') + (b[1] - 'a' + 10)
 
user17588651
then printf("%c\n", num)
 
user17588651
but for some chars it shows only blank space
 
user17588651
>
=



o


9
e


>

X
 
user17588651
7:36 PM
Its like that
 
It looks like manual parsing is too difficult for you. Try char b[3] = ""; (one more element for terminating null-character) and int num = strtol(b, NULL, 16); after reading 2 hexadecimal characters to b.
 
user17588651
You mean b[2]="", not b[3]?
 
9:55 PM
No, I mean b[3] with an extra element for a terminating null-character.
 

last day (15 days later) »