Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

I want to read and write numbers between files and memory in a byte-at-a-time way, not as formatted characters the way fprintf and fscanf do. How can I do this?

0
Posted

I want to read and write numbers between files and memory in a byte-at-a-time way, not as formatted characters the way fprintf and fscanf do. How can I do this?

0

What you’re trying to do is usually called “binary” I/O. First, make sure that you are calling fopen with the “b” modifier (“rb”, “wb”, etc.; see question 12.38). Then, use the & and sizeof operators to get a handle on the sequences of bytes you are trying to transfer. Usually, the fread and fwrite functions are what you want to use; see question 2.11 for an example. Note, though, that fread and fwrite do not necessarily imply binary I/O. If you’ve opened a file in binary mode, you can use any I/O calls on it (see for example the examples in question 12.42); if you’ve opened it in text mode, you can use fread or fwrite if they’re convenient. Finally, note that binary data files are not very portable; see question 20.5. See also question 12.40. comp.lang.c FAQ list ยท Question 12.38 Q: How can I read a binary data file properly? I’m occasionally seeing 0x0a and 0x0d values getting garbled, and I seem to hit EOF prematurely if the data contains the value 0x1a. A: When you’re reading a b

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123