Last active
January 23, 2019 20:12
-
-
Save kavan-mevada/5d54171ba830052bf3f16b936b8c7ef6 to your computer and use it in GitHub Desktop.
Kotlin/Native : Write to a text file using fprintf()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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