printing bytes read from binary file at a certain file position C [closed] The Next CEO of Stack OverflowHow do I create a Java string from the contents of a file?How to read all files in a folder from Java?ftell error after the first call to freadHow to read a large text file line by line using Java?Reading a binary file 1 byte at a timetrying to read a filefread is signalling EOF prematurely with a binary filefread can't read string value of a struct from a binary file in Cfread into buffer is blank despite non-empty fileC: unsigned short stored into buffer after fread from a binary file doesn't match original pattern
Is it reasonable to ask other researchers to send me their previous grant applications?
Is it possible to create a QR code using text?
Creating a script with console commands
What did the word "leisure" mean in late 18th Century usage?
Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?
Can Sri Krishna be called 'a person'?
Read/write a pipe-delimited file line by line with some simple text manipulation
How can I replace x-axis labels with pre-determined symbols?
Incomplete cube
Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?
Upgrading From a 9 Speed Sora Derailleur?
My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?
How can I separate the number from the unit in argument?
Can I cast Thunderwave and be at the center of its bottom face, but not be affected by it?
Is a distribution that is normal, but highly skewed, considered Gaussian?
How to compactly explain secondary and tertiary characters without resorting to stereotypes?
A hang glider, sudden unexpected lift to 25,000 feet altitude, what could do this?
Does Germany produce more waste than the US?
Simplify trigonometric expression using trigonometric identities
Calculating discount not working
Finitely generated matrix groups whose eigenvalues are all algebraic
How can a day be of 24 hours?
Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?
Is it a bad idea to plug the other end of ESD strap to wall ground?
printing bytes read from binary file at a certain file position C [closed]
The Next CEO of Stack OverflowHow do I create a Java string from the contents of a file?How to read all files in a folder from Java?ftell error after the first call to freadHow to read a large text file line by line using Java?Reading a binary file 1 byte at a timetrying to read a filefread is signalling EOF prematurely with a binary filefread can't read string value of a struct from a binary file in Cfread into buffer is blank despite non-empty fileC: unsigned short stored into buffer after fread from a binary file doesn't match original pattern
I am trying to print a fixed number of bytes (128) at at fixed file position (256) from a binary file to the screen.
However, the results show up look like:
For reference, I don't really know what's in the binary file; but I suspect that it's not squares.
Code is here:
int main()
FILE *file;
char *buffer;
file = fopen ( "binaryfile.bin" , "rb" );
if (file==NULL)
fputs ("Couldn' open file",stderr);
exit (-1);
fseek (file , 0 , 256);
buffer = (char*) malloc (sizeof(char)*128);
fread (buffer,128,1,file);
fwrite (buffer , sizeof(char)*128, 1, stdout);
return 0;
c io fopen fread
closed as off-topic by Antti Haapala, ekad, M-M, dgknca, Paul Roub Mar 22 at 14:05
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, ekad, M-M, dgknca, Paul Roub
add a comment |
I am trying to print a fixed number of bytes (128) at at fixed file position (256) from a binary file to the screen.
However, the results show up look like:
For reference, I don't really know what's in the binary file; but I suspect that it's not squares.
Code is here:
int main()
FILE *file;
char *buffer;
file = fopen ( "binaryfile.bin" , "rb" );
if (file==NULL)
fputs ("Couldn' open file",stderr);
exit (-1);
fseek (file , 0 , 256);
buffer = (char*) malloc (sizeof(char)*128);
fread (buffer,128,1,file);
fwrite (buffer , sizeof(char)*128, 1, stdout);
return 0;
c io fopen fread
closed as off-topic by Antti Haapala, ekad, M-M, dgknca, Paul Roub Mar 22 at 14:05
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, ekad, M-M, dgknca, Paul Roub
1
Why do you expect printable characters in a binary file? There are no squares in a file, only numbers which make sense in context. You can check their values by printing them in a loop, or with a file dump utility. Some text editors allow you to view the raw file bytes too, in hexadecimal format.
– Weather Vane
Mar 21 at 20:05
This will probably be garbage because you are getting random control characters and upper-half characters which depend on your the terminal's character set. Are you wanting a hex editor?
– Neil Edelman
Mar 21 at 20:06
You're actually usingfwrite
very strangely - it's got separate arguments from element size and number of elements, yet you multiply both into one... (and sizeof(char) is always 1)
– Antti Haapala
Mar 21 at 20:20
add a comment |
I am trying to print a fixed number of bytes (128) at at fixed file position (256) from a binary file to the screen.
However, the results show up look like:
For reference, I don't really know what's in the binary file; but I suspect that it's not squares.
Code is here:
int main()
FILE *file;
char *buffer;
file = fopen ( "binaryfile.bin" , "rb" );
if (file==NULL)
fputs ("Couldn' open file",stderr);
exit (-1);
fseek (file , 0 , 256);
buffer = (char*) malloc (sizeof(char)*128);
fread (buffer,128,1,file);
fwrite (buffer , sizeof(char)*128, 1, stdout);
return 0;
c io fopen fread
I am trying to print a fixed number of bytes (128) at at fixed file position (256) from a binary file to the screen.
However, the results show up look like:
For reference, I don't really know what's in the binary file; but I suspect that it's not squares.
Code is here:
int main()
FILE *file;
char *buffer;
file = fopen ( "binaryfile.bin" , "rb" );
if (file==NULL)
fputs ("Couldn' open file",stderr);
exit (-1);
fseek (file , 0 , 256);
buffer = (char*) malloc (sizeof(char)*128);
fread (buffer,128,1,file);
fwrite (buffer , sizeof(char)*128, 1, stdout);
return 0;
c io fopen fread
c io fopen fread
edited Mar 22 at 1:52
Govind Parmar
13k53764
13k53764
asked Mar 21 at 19:59
lildoodilydolildoodilydo
214
214
closed as off-topic by Antti Haapala, ekad, M-M, dgknca, Paul Roub Mar 22 at 14:05
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, ekad, M-M, dgknca, Paul Roub
closed as off-topic by Antti Haapala, ekad, M-M, dgknca, Paul Roub Mar 22 at 14:05
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, ekad, M-M, dgknca, Paul Roub
1
Why do you expect printable characters in a binary file? There are no squares in a file, only numbers which make sense in context. You can check their values by printing them in a loop, or with a file dump utility. Some text editors allow you to view the raw file bytes too, in hexadecimal format.
– Weather Vane
Mar 21 at 20:05
This will probably be garbage because you are getting random control characters and upper-half characters which depend on your the terminal's character set. Are you wanting a hex editor?
– Neil Edelman
Mar 21 at 20:06
You're actually usingfwrite
very strangely - it's got separate arguments from element size and number of elements, yet you multiply both into one... (and sizeof(char) is always 1)
– Antti Haapala
Mar 21 at 20:20
add a comment |
1
Why do you expect printable characters in a binary file? There are no squares in a file, only numbers which make sense in context. You can check their values by printing them in a loop, or with a file dump utility. Some text editors allow you to view the raw file bytes too, in hexadecimal format.
– Weather Vane
Mar 21 at 20:05
This will probably be garbage because you are getting random control characters and upper-half characters which depend on your the terminal's character set. Are you wanting a hex editor?
– Neil Edelman
Mar 21 at 20:06
You're actually usingfwrite
very strangely - it's got separate arguments from element size and number of elements, yet you multiply both into one... (and sizeof(char) is always 1)
– Antti Haapala
Mar 21 at 20:20
1
1
Why do you expect printable characters in a binary file? There are no squares in a file, only numbers which make sense in context. You can check their values by printing them in a loop, or with a file dump utility. Some text editors allow you to view the raw file bytes too, in hexadecimal format.
– Weather Vane
Mar 21 at 20:05
Why do you expect printable characters in a binary file? There are no squares in a file, only numbers which make sense in context. You can check their values by printing them in a loop, or with a file dump utility. Some text editors allow you to view the raw file bytes too, in hexadecimal format.
– Weather Vane
Mar 21 at 20:05
This will probably be garbage because you are getting random control characters and upper-half characters which depend on your the terminal's character set. Are you wanting a hex editor?
– Neil Edelman
Mar 21 at 20:06
This will probably be garbage because you are getting random control characters and upper-half characters which depend on your the terminal's character set. Are you wanting a hex editor?
– Neil Edelman
Mar 21 at 20:06
You're actually using
fwrite
very strangely - it's got separate arguments from element size and number of elements, yet you multiply both into one... (and sizeof(char) is always 1)– Antti Haapala
Mar 21 at 20:20
You're actually using
fwrite
very strangely - it's got separate arguments from element size and number of elements, yet you multiply both into one... (and sizeof(char) is always 1)– Antti Haapala
Mar 21 at 20:20
add a comment |
2 Answers
2
active
oldest
votes
This program is a mcve that shows how to read 128 bytes from a binary file and print each byte as a 2-digit hexadecimal value:
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *file;
char *buffer;
file = fopen("binaryfile.bin", "rb");
if (file == NULL)
perror("Could not open file");
exit(1);
fseek(file, 256, SEEK_SET);
buffer = malloc(sizeof(char) * 128);
fread(buffer, sizeof(char), 128, file);
for (int index = 0; index < 128; index++)
printf("%02X", ((unsigned int) buffer[index]) & 0x0FF);
if (index % 8 == 7)
printf("n");
else
printf(" ");
printf("n");
return 0;
Example Output
AD 0D 25 C8 74 AD D3 13
D5 00 62 EC CF 73 8E A4
61 2E 31 2F 70 ED D3 14
03 CC 06 B6 FB 77 FB ED
B4 0A 59 5F 50 00 61 59
D6 0B 21 F2 E6 1B 1A 07
DF 5E B8 32 17 D5 3E 29
0D 9E AE 47 D7 AF 86 4F
1E 75 6F EA C7 F7 A3 82
56 41 AA E8 02 42 91 D6
08 34 4F 63 40 9B 9C 06
0A 4E 65 30 90 7D 8A 4B
33 D1 50 9A 99 C5 F3 34
D9 A6 E0 2C 33 76 B3 DD
69 DF F9 30 8C 37 3A 51
9A 4F DB 81 81 F7 F5 F9
Note
This program is missing some error checking and does not free allocated memory. This code should not be used in a production environment.
add a comment |
fseek (file , 0 , 256);
is wrong. The third argument to fseek
is supposed to be one of SEEK_SET
, SEEK_CUR
or SEEK_END
; the second is the offset. To set the file pointer to the 256th byte in the file, use:
fseek(file, 256, SEEK_SET);
And how is this going to make the squares go away? :D
– Antti Haapala
Mar 21 at 20:22
1
@AnttiHaapala If the file is in fact binary, it probably won't. But now OP'sfread
call will in fact read 128 bytes starting at position 256 like they wanted.
– Govind Parmar
Mar 21 at 20:24
That is true, yes.
– Antti Haapala
Mar 21 at 20:25
Also, fread() is being used improperly. It should beint n = fread(buffer,1,128,file);
The second parameter is the size of each element; the third is the number of elements to read.Then n will be the number of bytes actually read.
– FredK
Mar 21 at 20:46
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This program is a mcve that shows how to read 128 bytes from a binary file and print each byte as a 2-digit hexadecimal value:
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *file;
char *buffer;
file = fopen("binaryfile.bin", "rb");
if (file == NULL)
perror("Could not open file");
exit(1);
fseek(file, 256, SEEK_SET);
buffer = malloc(sizeof(char) * 128);
fread(buffer, sizeof(char), 128, file);
for (int index = 0; index < 128; index++)
printf("%02X", ((unsigned int) buffer[index]) & 0x0FF);
if (index % 8 == 7)
printf("n");
else
printf(" ");
printf("n");
return 0;
Example Output
AD 0D 25 C8 74 AD D3 13
D5 00 62 EC CF 73 8E A4
61 2E 31 2F 70 ED D3 14
03 CC 06 B6 FB 77 FB ED
B4 0A 59 5F 50 00 61 59
D6 0B 21 F2 E6 1B 1A 07
DF 5E B8 32 17 D5 3E 29
0D 9E AE 47 D7 AF 86 4F
1E 75 6F EA C7 F7 A3 82
56 41 AA E8 02 42 91 D6
08 34 4F 63 40 9B 9C 06
0A 4E 65 30 90 7D 8A 4B
33 D1 50 9A 99 C5 F3 34
D9 A6 E0 2C 33 76 B3 DD
69 DF F9 30 8C 37 3A 51
9A 4F DB 81 81 F7 F5 F9
Note
This program is missing some error checking and does not free allocated memory. This code should not be used in a production environment.
add a comment |
This program is a mcve that shows how to read 128 bytes from a binary file and print each byte as a 2-digit hexadecimal value:
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *file;
char *buffer;
file = fopen("binaryfile.bin", "rb");
if (file == NULL)
perror("Could not open file");
exit(1);
fseek(file, 256, SEEK_SET);
buffer = malloc(sizeof(char) * 128);
fread(buffer, sizeof(char), 128, file);
for (int index = 0; index < 128; index++)
printf("%02X", ((unsigned int) buffer[index]) & 0x0FF);
if (index % 8 == 7)
printf("n");
else
printf(" ");
printf("n");
return 0;
Example Output
AD 0D 25 C8 74 AD D3 13
D5 00 62 EC CF 73 8E A4
61 2E 31 2F 70 ED D3 14
03 CC 06 B6 FB 77 FB ED
B4 0A 59 5F 50 00 61 59
D6 0B 21 F2 E6 1B 1A 07
DF 5E B8 32 17 D5 3E 29
0D 9E AE 47 D7 AF 86 4F
1E 75 6F EA C7 F7 A3 82
56 41 AA E8 02 42 91 D6
08 34 4F 63 40 9B 9C 06
0A 4E 65 30 90 7D 8A 4B
33 D1 50 9A 99 C5 F3 34
D9 A6 E0 2C 33 76 B3 DD
69 DF F9 30 8C 37 3A 51
9A 4F DB 81 81 F7 F5 F9
Note
This program is missing some error checking and does not free allocated memory. This code should not be used in a production environment.
add a comment |
This program is a mcve that shows how to read 128 bytes from a binary file and print each byte as a 2-digit hexadecimal value:
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *file;
char *buffer;
file = fopen("binaryfile.bin", "rb");
if (file == NULL)
perror("Could not open file");
exit(1);
fseek(file, 256, SEEK_SET);
buffer = malloc(sizeof(char) * 128);
fread(buffer, sizeof(char), 128, file);
for (int index = 0; index < 128; index++)
printf("%02X", ((unsigned int) buffer[index]) & 0x0FF);
if (index % 8 == 7)
printf("n");
else
printf(" ");
printf("n");
return 0;
Example Output
AD 0D 25 C8 74 AD D3 13
D5 00 62 EC CF 73 8E A4
61 2E 31 2F 70 ED D3 14
03 CC 06 B6 FB 77 FB ED
B4 0A 59 5F 50 00 61 59
D6 0B 21 F2 E6 1B 1A 07
DF 5E B8 32 17 D5 3E 29
0D 9E AE 47 D7 AF 86 4F
1E 75 6F EA C7 F7 A3 82
56 41 AA E8 02 42 91 D6
08 34 4F 63 40 9B 9C 06
0A 4E 65 30 90 7D 8A 4B
33 D1 50 9A 99 C5 F3 34
D9 A6 E0 2C 33 76 B3 DD
69 DF F9 30 8C 37 3A 51
9A 4F DB 81 81 F7 F5 F9
Note
This program is missing some error checking and does not free allocated memory. This code should not be used in a production environment.
This program is a mcve that shows how to read 128 bytes from a binary file and print each byte as a 2-digit hexadecimal value:
#include <stdio.h>
#include <stdlib.h>
int main()
FILE *file;
char *buffer;
file = fopen("binaryfile.bin", "rb");
if (file == NULL)
perror("Could not open file");
exit(1);
fseek(file, 256, SEEK_SET);
buffer = malloc(sizeof(char) * 128);
fread(buffer, sizeof(char), 128, file);
for (int index = 0; index < 128; index++)
printf("%02X", ((unsigned int) buffer[index]) & 0x0FF);
if (index % 8 == 7)
printf("n");
else
printf(" ");
printf("n");
return 0;
Example Output
AD 0D 25 C8 74 AD D3 13
D5 00 62 EC CF 73 8E A4
61 2E 31 2F 70 ED D3 14
03 CC 06 B6 FB 77 FB ED
B4 0A 59 5F 50 00 61 59
D6 0B 21 F2 E6 1B 1A 07
DF 5E B8 32 17 D5 3E 29
0D 9E AE 47 D7 AF 86 4F
1E 75 6F EA C7 F7 A3 82
56 41 AA E8 02 42 91 D6
08 34 4F 63 40 9B 9C 06
0A 4E 65 30 90 7D 8A 4B
33 D1 50 9A 99 C5 F3 34
D9 A6 E0 2C 33 76 B3 DD
69 DF F9 30 8C 37 3A 51
9A 4F DB 81 81 F7 F5 F9
Note
This program is missing some error checking and does not free allocated memory. This code should not be used in a production environment.
edited Mar 21 at 21:52
answered Mar 21 at 21:30
David CullenDavid Cullen
6,22412445
6,22412445
add a comment |
add a comment |
fseek (file , 0 , 256);
is wrong. The third argument to fseek
is supposed to be one of SEEK_SET
, SEEK_CUR
or SEEK_END
; the second is the offset. To set the file pointer to the 256th byte in the file, use:
fseek(file, 256, SEEK_SET);
And how is this going to make the squares go away? :D
– Antti Haapala
Mar 21 at 20:22
1
@AnttiHaapala If the file is in fact binary, it probably won't. But now OP'sfread
call will in fact read 128 bytes starting at position 256 like they wanted.
– Govind Parmar
Mar 21 at 20:24
That is true, yes.
– Antti Haapala
Mar 21 at 20:25
Also, fread() is being used improperly. It should beint n = fread(buffer,1,128,file);
The second parameter is the size of each element; the third is the number of elements to read.Then n will be the number of bytes actually read.
– FredK
Mar 21 at 20:46
add a comment |
fseek (file , 0 , 256);
is wrong. The third argument to fseek
is supposed to be one of SEEK_SET
, SEEK_CUR
or SEEK_END
; the second is the offset. To set the file pointer to the 256th byte in the file, use:
fseek(file, 256, SEEK_SET);
And how is this going to make the squares go away? :D
– Antti Haapala
Mar 21 at 20:22
1
@AnttiHaapala If the file is in fact binary, it probably won't. But now OP'sfread
call will in fact read 128 bytes starting at position 256 like they wanted.
– Govind Parmar
Mar 21 at 20:24
That is true, yes.
– Antti Haapala
Mar 21 at 20:25
Also, fread() is being used improperly. It should beint n = fread(buffer,1,128,file);
The second parameter is the size of each element; the third is the number of elements to read.Then n will be the number of bytes actually read.
– FredK
Mar 21 at 20:46
add a comment |
fseek (file , 0 , 256);
is wrong. The third argument to fseek
is supposed to be one of SEEK_SET
, SEEK_CUR
or SEEK_END
; the second is the offset. To set the file pointer to the 256th byte in the file, use:
fseek(file, 256, SEEK_SET);
fseek (file , 0 , 256);
is wrong. The third argument to fseek
is supposed to be one of SEEK_SET
, SEEK_CUR
or SEEK_END
; the second is the offset. To set the file pointer to the 256th byte in the file, use:
fseek(file, 256, SEEK_SET);
answered Mar 21 at 20:09
Govind ParmarGovind Parmar
13k53764
13k53764
And how is this going to make the squares go away? :D
– Antti Haapala
Mar 21 at 20:22
1
@AnttiHaapala If the file is in fact binary, it probably won't. But now OP'sfread
call will in fact read 128 bytes starting at position 256 like they wanted.
– Govind Parmar
Mar 21 at 20:24
That is true, yes.
– Antti Haapala
Mar 21 at 20:25
Also, fread() is being used improperly. It should beint n = fread(buffer,1,128,file);
The second parameter is the size of each element; the third is the number of elements to read.Then n will be the number of bytes actually read.
– FredK
Mar 21 at 20:46
add a comment |
And how is this going to make the squares go away? :D
– Antti Haapala
Mar 21 at 20:22
1
@AnttiHaapala If the file is in fact binary, it probably won't. But now OP'sfread
call will in fact read 128 bytes starting at position 256 like they wanted.
– Govind Parmar
Mar 21 at 20:24
That is true, yes.
– Antti Haapala
Mar 21 at 20:25
Also, fread() is being used improperly. It should beint n = fread(buffer,1,128,file);
The second parameter is the size of each element; the third is the number of elements to read.Then n will be the number of bytes actually read.
– FredK
Mar 21 at 20:46
And how is this going to make the squares go away? :D
– Antti Haapala
Mar 21 at 20:22
And how is this going to make the squares go away? :D
– Antti Haapala
Mar 21 at 20:22
1
1
@AnttiHaapala If the file is in fact binary, it probably won't. But now OP's
fread
call will in fact read 128 bytes starting at position 256 like they wanted.– Govind Parmar
Mar 21 at 20:24
@AnttiHaapala If the file is in fact binary, it probably won't. But now OP's
fread
call will in fact read 128 bytes starting at position 256 like they wanted.– Govind Parmar
Mar 21 at 20:24
That is true, yes.
– Antti Haapala
Mar 21 at 20:25
That is true, yes.
– Antti Haapala
Mar 21 at 20:25
Also, fread() is being used improperly. It should be
int n = fread(buffer,1,128,file);
The second parameter is the size of each element; the third is the number of elements to read.Then n will be the number of bytes actually read.– FredK
Mar 21 at 20:46
Also, fread() is being used improperly. It should be
int n = fread(buffer,1,128,file);
The second parameter is the size of each element; the third is the number of elements to read.Then n will be the number of bytes actually read.– FredK
Mar 21 at 20:46
add a comment |
1
Why do you expect printable characters in a binary file? There are no squares in a file, only numbers which make sense in context. You can check their values by printing them in a loop, or with a file dump utility. Some text editors allow you to view the raw file bytes too, in hexadecimal format.
– Weather Vane
Mar 21 at 20:05
This will probably be garbage because you are getting random control characters and upper-half characters which depend on your the terminal's character set. Are you wanting a hex editor?
– Neil Edelman
Mar 21 at 20:06
You're actually using
fwrite
very strangely - it's got separate arguments from element size and number of elements, yet you multiply both into one... (and sizeof(char) is always 1)– Antti Haapala
Mar 21 at 20:20