WxFFile
From WxWiki
Contents |
[edit] wxFFile
[edit] Newlines
Different platforms have different newline characters (characters that end a line of text).
| Operating System | Newline Type | Newline Character(s) | Hex |
|---|---|---|---|
| Mac Classic | Carriage Return | '\r' | 0D |
| Windows | Carriage Return + Line Feed | "\r\n" | 0D 0A |
| Unix & Mac OS X | Line Feed | '\n' | 0A |
[edit] Example
A Line
Line 2
| Original | A | L | i | n | e | [newline] | L | i | n | e | 2 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Mac Classic | 41 | 20 | 4C | 69 | 6E | 65 | 0D | 4C | 69 | 6E | 65 | 32 |
| Windows | 41 | 20 | 4C | 69 | 6E | 65 | 0D 0A | 4C | 69 | 6E | 65 | 32 |
| Unix & Mac OS X | 41 | 20 | 4C | 69 | 6E | 65 | 0A | 4C | 69 | 6E | 65 | 32 |
[edit] Reading in newlines
By default, most (all?) standard C libraries read files in via text mode by default, and when reading in a newline from a file in a non-unix platform (such as windows) will read in the newline character combo (such as "\r\n") as '\n'.
For instance, in the example above, [newline] would always be 0A.
A Line
Line 2
| Original | A | L | i | n | e | [newline] | L | i | n | e | 2 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Mac Classic | 41 | 20 | 4C | 69 | 6E | 65 | 0A | 4C | 69 | 6E | 65 | 32 |
| Windows | 41 | 20 | 4C | 69 | 6E | 65 | 0A | 4C | 69 | 6E | 65 | 32 |
| Unix & Mac OS X | 41 | 20 | 4C | 69 | 6E | 65 | 0A | 4C | 69 | 6E | 65 | 32 |
This can be a problem when you want to read text files portably.
wxFFile, since it is a simple wrapper around standard c library functions, will mimic this behaviour.
In order to read files in as binary (no translation of newlines), put a "b" at the end (postfix) of your file access string to read the file in binary mode instead of text mode ("r" becomes "rb", "w" = "wb", etc.).
[edit] File Modes
wxFFile is a simple wrapper around the standard c library function fopen and uses all of its file modes.
| "r" | Open an existing file for read. |
|---|---|
| "w" | Open a file for write. An existing file will be overwritten. |
| "a" | Open a file for append. Create the file if it doesn't exit. All writes occur at the end of the file. |
| "r+" | Open an existing file for read/write. |
| "w+" | Open an empty file for read/write. An existing file will be overwritten. |
| "a+" | Open an existing file for read/append. The entire file is available for read, but all writes
occur at the end of the file. The file is created if it doesn't exist |
The following options can be combined with the options shown above.
| "t" | Open the file in text mode. |
|---|---|
| "b" | Open the file in binary mode. |
[edit] Utilities
- Flip - useful (command-line) newline conversion programs with full public domain source
- There are countless visual studio add-ons that will convert newlines for you
- One of the more popular ones is strim'em
[edit] Other resources
- For char-to-hex conversion
