<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to print each line from a string]]></title><description><![CDATA[<p dir="auto">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:</p>
<p dir="auto">How do I get each line from my <code>char* buffer</code>, which contains all of my text, so I can output each line, including empty lines, with the correct line number in front of it.</p>
<p dir="auto">I tried several different ways already, but none have stuck. I tried <code>strtok()</code>, which is what is currently pushed to my repo, and it ignores whitespace. I tried <code>strchr()</code> 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.</p>
<p dir="auto">Here is the repo: <a href="https://codeberg.org/Mister_Bones/txt-ed" rel="nofollow ugc">https://codeberg.org/Mister_Bones/txt-ed</a></p>
<p dir="auto">Here is the offending code:</p>
<pre><code class="language-c">// 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;
}
</code></pre>
]]></description><link>https://forum.ieu.app/topic/c7c48ef2-eb25-46c4-8ee0-a57786bd2c9e/how-to-print-each-line-from-a-string</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 12:16:11 GMT</lastBuildDate><atom:link href="https://forum.ieu.app/topic/c7c48ef2-eb25-46c4-8ee0-a57786bd2c9e.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 17 Aug 2026 19:50:15 GMT</pubDate><ttl>60</ttl></channel></rss>