you are viewing a single comment's thread
view the rest of the comments
[โ€“] 2 points 2 months ago* (2 children)

It is not beginner friendly but it is optimized. It doesn't allocate memory or whatever. I don't expect you to understand all this but I did it for fun anyway.

int print_file(const char* buffer)
{
    // Validate the buffer
    if (!buffer || *buffer == '\0') return 1;

    // Prepare the first line prefix
    unsigned int line = 1;
    printf("%4d\t", line); // You could pre-format it if u want

    const char* cursor = buffer; // The pointer that points to the first char
    const char* linestart = cursor; // The start of the line
    char ch; // Character register

    while (true) {
        ch = *cursor++; // Read character THEN advance the cursor.

        // Check if the character is null or is newline or windows thing

        if (ch == '\0') {
            int linelength = cursor - linestart - 1; // Minus the null terminator
            printf("%.*s\n", linelength, linestart); // Print line using the length of string
            break;
        } else if (ch == '\n') {
            int linelength = cursor - linestart - 1; // Minus the newline
            printf("%.*s\n", linelength, linestart); // Print line using the length of string
            linestart = cursor;

            printf("%4d\t", ++line); // Print next line prefix
        } else if (ch == '\r') {
            continue; // Ignore the Windows thing
        }
    }
    
    return 0;
}
  • source
  • hideshow 2 child comments
  • [โ€“] 1 point 2 months ago (1 child)

    I feel like I'm a pretty beginner C dev and I could read this. What made this advanced?

  • source
  • parent
  • hideshow 1 child comment
  • [โ€“] 2 points 2 months ago*

    TBF it is not complicated but it does use the simplest form of pointer arithmetic and order of operation of (++var) or (*var++). Considering OP couldn't write a basic version of this I did not want to put pressure on him. If you can understand it as a beginner good for you! You must remember that a lot of developers struggle to learn pointers in the first place for some reason. I blame AI.

    EDIT: Also I did not say this is advanced, just not beginner friendly

  • source
  • parent