Kotlin-native posix directory iteration

Hi i’m a novice with Kotlin, especially native,

I’m trying to build a serial tty package for native to allow my kotlin apps to talk to arduino boards. Im using Linux64.

I cant get this to compile, it fails on both the readdir line complaining about missing types, and on the println line where it cannot find the .d_name member

My expectation is that it would print out all the filenames of the files in the /dev directory, anybody got any ideas, im a bit stuck on this.

import kotlinx.cinterop.CPointer
import platform.posix.*

fun main(args: Array) {

val devdir = opendir("/dev")

if(devdir != null) {
    do {
        val result? = readdir(devdir)
        if (result != null) {
            println(result.d_name )
        }
    } while( result != null)
}

}

I found my own answer, after hundereds of searchs more i found…

Final working example

import kotlinx.cinterop.*
import platform.posix.*

fun main(args: Array) {

val devdir = opendir("/dev")

if(devdir != null) {

        var result = readdir(devdir)
        while ( result != null) {
            println(result.pointed.d_name.toKString())
            result = readdir(devdir)
        }
 }

}

hi tshawkin can u help me about connecting arduino with kotlin in windows because i see in your cod there is a special importation:
import kotlinx.cinterop.*
import platform.posix.* does that work with windows 10
thnx

1 Like