file_io.tc¶
File I/O demo — write and read back
// File I/O demo — write and read back
// On ESP32: uses LittleFS, in browser: simulated virtual filesystem
int main() {
char data[64];
char buf[64];
// Prepare data to write
strcpy(data, "Hello from TinyC!\n");
// Write to file (r=read, w=write, a=append)
int f = fileOpen("/test.txt", w);
if (f >= 0) {
fileWrite(f, data, strlen(data));
fileClose(f);
printStr("Written OK\n");
}
// Read back
f = fileOpen("/test.txt", r);
if (f >= 0) {
int n = fileRead(f, buf, 63);
buf[n] = 0; // null-terminate
fileClose(f);
printStr("Read back: ");
printString(buf);
}
// Check file info
if (fileExists("/test.txt")) {
printStr("File size: ");
print(fileSize("/test.txt"));
printStr("\n");
}
// Clean up
fileDelete("/test.txt");
printStr("File deleted\n");
return 0;
}