how do I tell if there is byres <4

Pages: 12
Do I declare a variable and have it read in exactly four bytes from a file then process the data then read in the Next four bytes from the file untill the number of bytes to read is < 4 and work with the bytes in hex form?
How can you tell what position the pointer is in the file?
I read the articles but they don't seem to go into that much detail about it.
Thanks
Last edited on
I don't really understand what you are trying to do but tellg() gives you the position of the read pointer and tellp() gives you the position of the write pointer.
1
2
3
4
5
#include <cstdint>

uint8_t fourbytes[4];

filestream.read(reinterpret_cast<char *> (fourbytes), 4);


Disclaimer: didn't check if it compiles/works.
closed account (zb0S216C)
char isn't guaranteed to be exactly 1 byte in size. Best idea would be to store an address inside an integer pointer. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#if defined BIT_64
    typedef long long* address;
#endif

#if defined BIT_32
    typedef int* address;
#endif

int main()
{
    address addrs[4U] = {0};

    addrs[0U] = reinterpret_cast<address>(0x0001A);
}


Wazzak
Last edited on
char isn't guaranteed to be exactly 1 byte in size.

It is, actually. sizeof(char) is 1 by definition.
Although a byte doesn't necessarily have 8 bits.
@Framework I'm not sure what you are trying to do but sizeof(char) == 1 always. char is at least 8 bits, if that makes a difference I'm not sure.
char isn't guaranteed to be exactly 1 byte in size.


Lol, neither is a byte guaranteed to be exactly 8 bits.

But we're talking about alien hardware, with an alien OS, with an alien programming language.
closed account (zb0S216C)
Extract from C++ Primer Plus 5th Edition:

"if a large set of characters is the basic character set for an implementation, a compiler vender can define char as a 16-bit byte or larger." (sic)

Just because char is 1 byte one 1 system, it doesn't mean it's 1 byte on all systems. Still, using char to represent a byte is bad in my eyes (it's my opinion. Deal with it).

Catfish wrote:
"Lol, neither is a byte guaranteed to be exactly 8 bits." (sic)

That's where you're wrong. Does anybody realize that a byte is not always 8 bits? Narrow-minded views I suppose.

Wazzak
Last edited on
Does anybody realize that a byte is not always 8 bits? Narrow-minded views I suppose.

Did you even read any of the replies?

Just because char is 1 byte one 1 system, it doesn't mean it's 1 byte on all systems.

...or your own quote from that book, for that matter?
Still, using char to represent a byte is bad in my eyes (it's my opinion. Deal with it).


1
2
istream& read ( char* s, streamsize n );
ostream& write ( const char* s , streamsize n );


So I guess you're as disappointed as I am by the above?
Framework, The quote even say "16-bit byte". If char is 16 bits it's still a byte! Such an implementation can't have uint8_t by the way, which is perfectly fine because uint8_t, uint16_t, etc. are all optional.
closed account (zb0S216C)
Athar wrote:
"Did you even read any of the replies?" (sic)

Stupid question. That's what my reply was structure on.

Athar wrote:
"...or your own quote from that book, for that matter?" (sic)

Page 110, first paragraph.

Wazzak
closed account (zb0S216C)
Catfish wrote:
"So I guess you're as disappointed as I am by the above?" (sic)

Yeah'p.

Peter87 wrote:
"The quote even say "16-bit byte". If char is 16 bits it's still a byte!" (sic)

Which forces me to repeat myself: "...is not always 8 bits...".

Wazzak
Last edited on
Which forces me to repeat myself: "...is not always 8 bits...".

No one claimed anything different.
Last edited on
closed account (zb0S216C)
Athar wrote:
"...which no one claimed." (sic)

Errmm.... Yeah, somebody did:

Catfish wrote:
"Lol, neither is a byte guaranteed to be exactly 8 bits." (sic)


Wazzak
Last edited on
It's like everyone in this thread is talking in different variations of English that are logical negations of each other.
I said a byte is not guaranteed to be exactly 8 bits. What are you reading?

Edit: maybe I should've used "nor"?
Last edited on
could someone explain my question in C not C++?
Thankyou
Sorry, Onceler, sometimes Framework likes to start fights by twisting language.

Exactly what kind of file are we talking about. A binary file, right?
Do you have a specific format to your file? (Even if it is just a list of four-byte integers.)
What are you trying to do with the data?

Here are some routines to help read and write binary data:
http://www.cplusplus.com/forum/beginner/31584/#msg171056
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdint.h>

uint8_t fourbytes[4];

FILE *f = fopen("file.bin", "rb");

if (f != NULL)
    while (ferror(f) == 0 && feof(f) == 0)
        fread(fourbytes, sizeof (uint8_t), sizeof fourbytes, f);


Disclaimer: didn't check if it compiles/works.

Edit: as for where you are in the file, use ftell().
http://www.cplusplus.com/reference/clibrary/cstdio/ftell/
Last edited on
Pages: 12