Skip to content

feat(file): Implement character read and write functions for LocalFile#1431

Merged
xezon merged 1 commit intoTheSuperHackers:mainfrom
Mauller:feat-file-character-handling
Aug 7, 2025
Merged

feat(file): Implement character read and write functions for LocalFile#1431
xezon merged 1 commit intoTheSuperHackers:mainfrom
Mauller:feat-file-character-handling

Conversation

@Mauller
Copy link

@Mauller Mauller commented Aug 7, 2025

This PR adds extra character handling interfaces to the File class and implements the functionality into the LocalFile class.
The ram file just implements stubs that return errors if used as character handling is not required.

@Mauller Mauller self-assigned this Aug 7, 2025
@Mauller Mauller added Enhancement Is new feature or request Major Severity: Minor < Major < Critical < Blocker labels Aug 7, 2025
@Mauller Mauller added this to the Code foundation build up milestone Aug 7, 2025
@Mauller Mauller force-pushed the feat-file-character-handling branch from 6734241 to 2d43ef2 Compare August 7, 2025 18:04
// LocalFile::readChar
//=================================================================

Int LocalFile::readChar( )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should return Char.

Copy link
Author

@Mauller Mauller Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have it replicating the behaviour of what the file stream functions do, fgetc in this case, unless we aren't bothered about replicating it?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok. I inquired with Chat Gippity and it appears to be ok then:


fgetc() returns an int instead of char for a very practical reason:

It needs to be able to represent every possible char value and an extra special value — EOF — which indicates the end of the file or an error.


The problem if it returned char

  • Suppose char is 8 bits (typical), it can only hold values 0 to 255 (unsigned) or -128 to 127 (signed).
  • EOF is defined as a negative integer (usually -1), which must be distinguishable from any valid byte value.
  • If fgetc() returned char, there would be no way to tell the difference between reading a byte 0xFF (255) and EOF (−1) on systems where char is signed — they'd both appear as -1.

The solution

  • fgetc() returns an int with a range large enough to:

    1. Represent all possible unsigned char values (0–255).
    2. Also represent EOF as a distinct value (usually -1).

Example

#include <stdio.h>

int main(void) {
    FILE *f = fopen("data.bin", "rb");
    int ch;

    while ((ch = fgetc(f)) != EOF) {  // ch is int, not char
        printf("Byte: %02X\n", (unsigned char)ch);
    }

    fclose(f);
}

Here:

  • ch is int so it can hold EOF.
  • We cast to (unsigned char) when we want to treat the result as a byte.

Quick summary

fgetc returns int so it can safely distinguish between any valid byte value and EOF.

If you want, I can also explain why getchar() follows the same rule and how it’s tied to the C standard’s type promotion rules — which is actually an interesting historical detail. Would you like me to go into that?

// LocalFile::writeChar - Ascii
//=================================================================

Int LocalFile::writeChar( const Char* character )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should return the result of write instead of the character that was passed, or EOF, which is not an intuitive result.

Copy link
Author

@Mauller Mauller Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was mostly copying what the file stream type functions return with fputc etc.
they return the character written or EOF if it failed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok makes sense then.

@xezon xezon changed the title feat(file): implement character read and write functions for LocalFile feat(file): Implement character read and write functions for LocalFile Aug 7, 2025
@xezon xezon removed the Major Severity: Minor < Major < Critical < Blocker label Aug 7, 2025
@xezon xezon merged commit 02fdf4f into TheSuperHackers:main Aug 7, 2025
19 checks passed
@xezon xezon deleted the feat-file-character-handling branch August 7, 2025 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Is new feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants