Adam
Adam

Reputation: 10036

Determining the orientation of a file in memory

Say I want to process a CSV file. I know in Python I can call the read() function to open the file and read it in a byte at a time, from the first field in the file (i.e. the field in the top left of the file) to the last field (the field in the bottom right).

My question is how I can determine the orientation of a file in memory. That is, if I view the contents of the file as a single binary number and process it as bit stream, how can I know if the first field (the field the read() first returns to us) is stored in the least significant positions of the binary number or the most significant positions? Would that be determined by the endianness of the machine my program is running on?

Here's one (contrived) instance where this distinction would matter. Say I first scanned the binary representation of the file from least significant position to most significant position to determine the widths of each of the CSV values. If I were to then call read(), the first field width I calculated would correspond to the first field read() returns if and only if the first field of the CSV file is stored at the least significant bit positions when we view the file as a single binary number. If the first field was instead stored at the most significant positions, I'd first have to reverse my list of calculated field widths before I could use it.

Here's a more concrete example:

CSV file: abc,12345

Scanned field widths: either [3, 5] or [5, 3] depending on how the CSV file is laid out in memory.

Now, if I call read(), the first field I'll process is abc. If abc happened to be the first field I scanned through when calculating the field widths, I'm good. I'll know that I've scanned the entire first field after reading 3 characters. However, if I first scanned 12345 when calculating the field widths, I've got a problem.

How can I determine how a file is laid out in memory? Is the first field of a file stored in the least significant bit positions, or the most significant bit positions?

Upvotes: 0

Views: 32

Answers (1)

StenSoft
StenSoft

Reputation: 9609

Endiannes is a problem of binary files. CSV file is a text file. The numbers are not binary numbers but ASCII characters. There is no endiannes in it.

Upvotes: 1

Related Questions