ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Could somebody provide a complete example of code that shows how to read a binary file into a buffer/array in C. Im trying to use fread() but my C book doesnt give very understandable examples. Or if theres something better than fread thats cool too.
With your code, it reads in (im guessing) first 4 bytes of data, which is what is happening with the different code blocks that i've written. The file is ok, its a .bmp file. The output when printing buffer is:
If im reading on the file right, and displaying it with correctly with printf("%s", buffer) , i should get lots of output to screen. I tried a for loop to read it in char by char, and output is still BM¶, and i kept an integer count with how many char's read, and its 4.
Heres the code u gave me (modified).....any other ideas??
THANKS!!
FILE *f;
unsigned char buffer[10000];
int n;
f = fopen("bmp.bmp", "rb");
n = fread(buffer, 10000, 1, f);
printf("%s\n", &buffer);
You can't just print a binary buffer as a string. The reason is because if the binary data contains 8-bits in a row of 0's it's interpreted as a string-terminating NULL. So let's say your buffer looks like this:
Usually I find it's more useful to display binary data in a hexidecimal format. You can use something like:
Code:
for (int c=0;c<bufferSize;c++)
{
printf("%.2X ", (int)buffer[c]);
// put an extra space between every 4 bytes
if (c % 4 == 3)
{
printf(" ");
}
// Display 16 bytes per line
if (c % 16 == 15)
{
printf("\n");
}
}
// Add an extra line feed for good measure
printf("\n");
Of course, you can also add extra stuff in there to make it look more like a hex editor as well, but you get the idea...
Use the code posted by deiussum in article #8 of this thread. That won't interpret the file in any way, but simply display the bytes as hexadecimal. Interpretation of the data is application specific, and most people here won't know how to extract meaningful information from files of that type. For that, you will need some kind of published documentation, or a code library/API written for the purpose of interpreting the file.
--- rod.
hi all, i am a newbie in this forum. luckily i got the post i was searching for. but i have some issues. i have a .m4v file and want to read it and display its contents. i read the file and tried to print the buffer, but got some junk data on screen. i even tried the code given above for printing buffer, but effort seems to be in vain. The above modified code i used is :-
hi all, i am a newbie in this forum. luckily i got the post i was searching for. but i have some issues. i have a .m4v file and want to read it and display its contents. i read the file and tried to print the buffer, but got some junk data on screen. i even tried the code given above for printing buffer, but effort seems to be in vain. The above modified code i used is :-
What kind of data do you expect on the screen and why do you expect it to look not like junk ? I.e. do you know what data is displayed on screen as something meaningful and what data as junk/gibberish ? If I may, have you ever heard of ASCII ? If not, try to perform web search on ASCII and/or try
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.