get_var() pulling Null instead of the stored data

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By denxi

EDIT3: I’m an idiot and was pulling the wrong file in a different section of code because I forgot to stick the rest of the file path string at the beginning of the filename in the load section of the code (not shown here). I got the example code I gave to work by just closing the file, re-opening it, and then calling get_var(), but I still couldn’t pull the data when I tried running it through the actual program. That’s when I saw my error.
This can be closed, sorry.

EDIT2: If I save the file as a text file, I can open it in notepad and see that it’s saved the data. So I guess the problem isn’t with the saving portion, but the get_var().

I’m having a problem with store_var that I haven’t had before, where it’s not storing any information for some reason.

The chunk of code that I have is as follows:

	for prof in profiles.values():
	    var file = File.new()
	
	    file.open(save_path + "/" + prof["name"] + ".data", File.WRITE_READ)
	    file.store_var("1")
	    print (file.get_var())
	    file.close()

I have it set up to just store a “1” for testing. Printing the get_var prints “Null.” This is what happens as well when I try to store the actual information that I want to, i.e. “prof”, which is in a dictionary form. It’s creating the file perfectly fine in my file explorer.

No idea why this is happening, I’d appreciate any insight. I’m running win10(64) btw.

EDIT: Also running 3.1 Mono

:bust_in_silhouette: Reply From: Zylann

When you call store_var, the file cursor writes the data and advances to the position right after your data. When you call get_var(), it tries to read the data from the point of the cursor. But the cursor is already past your data, so it has nothing to read next.

If you want to read what you just wrote, you need to seek back to the position you were before writing it (although there isn’t often a reason to do so).

var begin = file.get_position()
file.store_var("1")
file.seek(begin)
print(file.get_var())

Yea you posted when I was in the middle of editing my post, but I realized that my example code wasn’t actually written properly so that was causing a completely separate problem to my actual error, and that my actual error was just my forgetting to properly specify the file path. Thanks for the response though.

denxi | 2020-01-15 18:57