7

I'm trying to make some sort of simple console text editor program to get better with C. I'm having trouble with what I thought would be a somewhat simple task:

How do I get each line from my char* buffer, which contains all of my text, so I can output each line, including empty lines, with the correct line number in front of it.

I tried several different ways already, but none have stuck. I tried strtok(), which is what is currently pushed to my repo, and it ignores whitespace. I tried strchr() but did not have the slightest idea how that function worked and got an infinite loop. I tried doing my own function to create an array of lines but that lead to a segmentation fault which was not fixed by mallocing the array. I am at a loss here, I'm not sure what I can do.

Here is the repo: https://codeberg.org/Mister_Bones/txt-ed

Here is the offending code:

// Print contents of file
int print_file(char* buffer) {
	// Print a new line
	printf("\n");

	// Print each line with line number
	// Set first line
	int line_num = 1;

	// Get individual line from buffer
	char* line = strtok(buffer, "\n");

	// Loop through and print lines
	// TODO: Don't ignore whitespace
	while (line != NULL) {
		printf("%4d\t%s\n", line_num, line);
		line = strtok(NULL, "\n");
		line_num++;
	}

	return 0;
}
you are viewing a single comment's thread
view the rest of the comments
[-] Alphenex53@programming.dev 2 points 1 week ago* (last edited 1 week ago)

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;
}
[-] ExperimentalGuy@programming.dev 1 points 1 week ago

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

[-] Alphenex53@programming.dev 2 points 1 week ago* (last edited 1 week 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

this post was submitted on 17 Aug 2026
7 points (88.9% liked)

C Programming Language

1336 readers
1 users here now

Welcome to the C community!

C is quirky, flawed, and an enormous success.
... When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd.
... The only way to learn a new programming language is by writing programs in it.

© Dennis Ritchie

🌐 https://en.cppreference.com/w/c

founded 3 years ago
MODERATORS