Ask coding questions

← Back to all posts
File written to by the program not saving occasionally
h
Baconman321 (1103)

I have a program in Go that allows me to make and edit articles.

Sometimes, though the program won't save an edit of the article. The articles are stored via a .json file.

I have noticed that this usually happens if I close out of the window after it says it has successfully been edited, OR I close out of the computer. It always saves if I open the repl and check if it has been saved (as far as I know).

@tussiez told me that it was simply replit not saving sometimes, but I start to doubt that. It shows the edited article, but if I come back the next day the new edit is gone! It must be deleting the contents of the edit, but why? Also, how would I fix this?

Help would be appreciated.

- A confused bacon

Comments
hotnewtop
InvisibleOne (3186)

Is this a text file on replit? Becuase occasionally replit will not save what your code has written to a text file if you are say; opening it in another tab or something. This is because replit does not save changes unless they are made by you, if that makes any sense.

Baconman321 (1103)

@InvisibleOne

The articles are stored via a .json file.

I already found the answer, but now I need to know how to work around this.

robowolf (550)

Well sometimes if you haven't closed the file, for example when you do something like files = open('file',w) and forget to do files.close() it doesn't save. Also could you put your code, and when it say it's been edited has the file actually been edited.

Baconman321 (1103)

@robowolf Yeah, but AFAIK ioutil.WriteFile doesn't have a close method :/

I think the method I use already closes it for me when it's done.

My friend did recommend to me that I should check if the contents of the file are edited. I just want to know what is going on...

It saves but the next day it is gone!

>:/

robowolf (550)

@Baconman321 Could you post your code?

Baconman321 (1103)

@robowolf Example:

ioutil.WriteFile([]byte("hello, world"), "testFile.txt", 0644)
Andy_4sberg (54)

@Baconman321, does the language your programming this in support Replit's database?

robowolf (550)

@Baconman321 Well if your working with json why not just use the default json methods. Also try on another repl, sometimes if a repl is buggy for me when I open a new one it works.

Baconman321 (1103)

@Andy_4sberg Yeah, Go supports DB functions, but I don't want to use it rn.

replit's database isn't as good as file storage IMO, except for the fact that it actually saves the content.

If replit DB got much better (increased key name range and more storage size), then yes I think I'd use it.

Andy_4sberg (54)

Replit doesn't allow repls to edit files. It creates a new instance of the repl and anything changed will just revert to how it was programmed. You should try using the Replit database, which doesn't reset unless you tell it to.

Baconman321 (1103)

@Andy_4sberg No, it does because it saves sometimes.

It's the program itself that is writing to the file, which many people have done before.

xxpertHacker (931)

@Baconman321 ^ they're right, once the Repl dies and turns off, the Repl is reset to the last point that a user had interacted with the Repl, therefore any programmatic changes are consequently reset.

Baconman321 (1103)

@xxpertHacker

Makes sense, but IDK why it would save in the first place, then :/

What makes it save, and how do I write to files then?

I've seen people use files for storage, so how do those work?

xxpertHacker (931)

@Baconman321 a) it saves since the program is still executing, so the behavior is expected.

b) sure, plenty of replers do! They just encounter the bugs that you have ;)

Baconman321 (1103)

@xxpertHacker So, how would I make sure it saves?

If it saves because the program is still executing, then why does it have this problem at all? Wouldn't it always save because it always writes to the file while the program is executing?

Is there a surefire workaround for this, then?

Andy_4sberg (54)

@Baconman321 try running the program in the same tab as it's code on replit. Does it work? If it does, I think it works because it's running as the owner, and gives itself permission write. Does it work on the site in a new tab? If it does, I'm very confused. If it doesn't, I think the new tab is a new instance of the program, and the instance of it is greater on replit, so it overwrites the data (the one running in replit). This is just a theory, so of course I don't know if it's correct.

xxpertHacker (931)

@Baconman321 Once the program ends execution, the Repl's image is reset. Changes are temporary.

Solution: use a DB.

Baconman321 (1103)

@xxpertHacker DB is a bit flawed and limited rn, that's the problem.

Plus, file storage have much a higher size limit, thus why I use it :/

Baconman321 (1103)

@Andy_4sberg Wdym "same tab".

It's a web server that serves a web page which sends a fetch request back to the program which in turn writes to the file.