Skip to content

Instantly share code, notes, and snippets.

@kavan-mevada
Last active January 23, 2019 20:12
Show Gist options
  • Save kavan-mevada/5d54171ba830052bf3f16b936b8c7ef6 to your computer and use it in GitHub Desktop.
Save kavan-mevada/5d54171ba830052bf3f16b936b8c7ef6 to your computer and use it in GitHub Desktop.
Kotlin/Native : Write to a text file using fprintf()
package sample.helloworld
import platform.posix.*
/*
Reference of C code:
---------------------
int main()
{
int num;
FILE *fptr;
fptr = fopen("test.txt","w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
*/
fun main(args: Array<String>) {
val fptr = fopen("test.txt", "w")
if (fptr == null) {
println("Error!")
return
}
println("Enter num: ")
val num = readLine()?.toInt() ?: 0
println(num)
fprintf(fptr, num.toString())
fclose(fptr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment