# Failed to write and read file on Nanos

**URL:** <https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55>\
**Category:** Uncategorized\
**Created:** [September 23, 2024, 2:31pm UTC](https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55 "2024-09-23T14:31:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![itzpires](https://avatars.discourse-cdn.com/v4/letter/i/eb8c5e/32.png) [@itzpires](https://forums.nanovms.com/u/itzpires)\
**Post date:** [September 23, 2024, 2:31pm UTC](https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55/1 "2024-09-23T14:31:30Z")

</div>

Hello, I hope you’re well. I’ve been working with Nanos for a few days, but I’ve come up against a problem.  
I have a program that should write logs to disk, but it turns out that it’s not possible to write to disk with Nanos. I’ve followed these instructions, but it doesn’t keep failing to write, it creates the file, but it’s always empty. ([https://docs.ops.city/ops/volumes](https://docs.ops.city/ops/volumes))  
How can I solve it? How do I write to a file?

Another small question, is it possible to interact with Nanos with a command line?

Thank you very much

---

<div class="post-metadata">

**Author:** ![eyberg](https://avatars.discourse-cdn.com/v4/letter/e/ea666f/32.png) [@eyberg](https://forums.nanovms.com/u/eyberg)\
**Post date:** [September 23, 2024, 3:18pm UTC](https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55/2 "2024-09-23T15:18:04Z")

</div>

We would probably need to see some code but more than likely you don’t have a large enough volume allocated. The base volume size is, by default, set to slightly more than what is initially put onto the disk so you’ll want to bump it up by however much is necessary.

> **[Configuration | Ops](https://docs.ops.city/ops/configuration#base_volume_size)**

As for your other question - what do you mean interact with nanos? OPS has several tools to inspect a running instance or image.

---

<div class="post-metadata">

**Author:** ![itzpires](https://avatars.discourse-cdn.com/v4/letter/i/eb8c5e/32.png) [@itzpires](https://forums.nanovms.com/u/itzpires)\
**Post date:** [October 3, 2024, 2:52pm UTC](https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55/3 "2024-10-03T14:52:24Z")

</div>

Good afternoon. I’ve tried more than once with discs much larger than what was supposed to be, because what would be written would only take up KB, but it still didn’t work. Thank you.

---

<div class="post-metadata">

**Author:** ![eyberg](https://avatars.discourse-cdn.com/v4/letter/e/ea666f/32.png) [@eyberg](https://forums.nanovms.com/u/eyberg)\
**Post date:** [October 3, 2024, 3:08pm UTC](https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55/4 "2024-10-03T15:08:07Z")

</div>

Can you post some sample code? What language are you using?

---

<div class="post-metadata">

**Author:** ![itzpires](https://avatars.discourse-cdn.com/v4/letter/i/eb8c5e/32.png) [@itzpires](https://forums.nanovms.com/u/itzpires)\
**Post date:** [October 3, 2024, 3:35pm UTC](https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55/5 "2024-10-03T15:35:21Z")

</div>

Yes, of course. I try to create the file, if it already exists, create it with the same name, but with a higher index. Then I close it, open it again and check the contents. It always appears empty.

```auto
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *file;
    char filename[50];
    char ch;
    int file_index = 0;

    // Try to open the file
    sprintf(filename, "ARQUIVO.txt");
    
    // If the file already exists, increment the index and try again
    while ((file = fopen(filename, "r")) != NULL) {
        fclose(file);
        file_index++;
        sprintf(filename, "ARQUIVO_%d.txt", file_index);
    }

    file = fopen(filename, "w");
    if (file == NULL) {
        printf("Error.\n");
        return 1;
    }

    // Write some text to the file
    fprintf(file, "Hello\n");
    fclose(file);

    printf("The text has been written to %s.\n", filename);

    // Open and read the file
    file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error.\n");
        return 1;
    }

    printf("Contents of %s:\n", filename);
    fclose(file);

    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![eyberg](https://avatars.discourse-cdn.com/v4/letter/e/ea666f/32.png) [@eyberg](https://forums.nanovms.com/u/eyberg)\
**Post date:** [October 3, 2024, 4:02pm UTC](https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55/6 "2024-10-03T16:02:01Z")

</div>

Ok - 2 things:

1. I would put an explicit flush call after your write but before your close:

```auto
    fprintf(file, "Hello\n");

    fflush(file);

    fclose(file);

```

This ensures that this happens before the program exits - if this was a long running service you proably wouldn’t need to care about that but in this example the program exits really fast so there is a chance that it might not flush immediately. The difference between linux and nanos here is that when you run this program on linux the system is not shutting down - here it is.

1. I would pad the filesystem above what is produed. On my system I had a 16mb binary so I gave it 20mb.

```auto
eyberg@box:~/ff$ cat config.json
{
  "BaseVolumeSz": "20m"
}

```

Without that I could tell it wasn’t actually writing cause if you turn on ‘–trace’ you’ll see something like this:

```auto
    2 file_write_complete: f 0xffffc000000978f8, sg, 0xffffc0000062efc0, completion fdesc_io_complete, status (fsstatus:1 result:unable to create extent)

```

Anytime you see “unable to create extent” that means you aren’t writing to the filesystem.

After you do both of those things you should be able to verify that the file was written to the image:

```auto
eyberg@box:~/ff$ ops image cat main ARQUIVO.txt
Hello

```

---

<div class="post-metadata">

**Author:** ![francescolavra](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.nanovms.com/francescolavra/32/33_2.png) [@francescolavra](https://forums.nanovms.com/u/francescolavra)\
**Post date:** [October 4, 2024, 9:02am UTC](https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55/7 "2024-10-04T09:02:48Z")

</div>

If you use fprintf() to write to the file, you should check the return value from fclose() to verify whether the write was successful: if there is not enough space in the disk, fclose() will return -1 and set `errno` to ENOSPC. (Likewise, if you call fflush(), you should check the return value from that function.)

---

<div class="post-metadata">

**Author:** ![itzpires](https://avatars.discourse-cdn.com/v4/letter/i/eb8c5e/32.png) [@itzpires](https://forums.nanovms.com/u/itzpires)\
**Post date:** [October 9, 2024, 3:13pm UTC](https://forums.nanovms.com/t/failed-to-write-and-read-file-on-nanos/55/8 "2024-10-09T15:13:37Z")

</div>

Perfect! Thank you so much! It works now ahah
