I am trying to learn Odin for Advent of Code and have run into a problem reading a file line by line in a function. I have tried returning the array by value and returning a slice. Each results in the same problem.... The first two elements of the array get corrupted by random garbage. Here is my code:
package main
import "core:os"
import "core:fmt"
import "core:slice"
import "core:strings"
read_file_line_by_line :: proc(filepath: string, lines: ^[dynamic]string ) {
data, ok := os.read_entire_file(filepath, context.allocator)
defer delete(data, context.allocator)
it := string(data)
fmt.println( "As the array is built...")
for line in strings.split_lines_iterator(&it) {
append( lines, line )
fmt.println( line )
}
fmt.println( "After the array is built...")
for line in lines {
fmt.printf( "%s\n", line )
}
}
main :: proc() {
if len(os.args) < 2 {
fmt.eprintf( "Usage: %s \n", os.args[ 0 ] )
return
}
lines : [dynamic]string
read_file_line_by_line( os.args[ 1 ], &lines )
fmt.println( "After the array is returned...")
for line in lines {
fmt.println( line )
}
}
And my input is a file containing:
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82
My output is:
As the array is built...
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82
After the array is built...
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82
After the array is returned...
�` << Corrupted element
�f� << Corrupted element
R48
L5
R60
L55
L1
L99
R14
L82
Any help would be appreciated. Thank you in advance.