Panakotta00 / FicsIt-Networks

Control, Monitor, Manage and Automate your Satisfactory.
https://ficsit.app/mod/FicsItNetworks
GNU General Public License v3.0
155 stars 50 forks source link

filestream read only returns empty #273

Closed kfpopeye closed 9 months ago

kfpopeye commented 9 months ago

I have this function creating text files:

function saveVehicleData(veh_hash)
 if (not hasHDD) then return end

 local vehFile
 local filePath = "/" .. veh_hash .. ".truck"
 if (fs.exists(filePath)) then
  vehFile = fs.open(filePath, "+r")
 else
  vehFile = fs.open(filePath, "w")
 end

 vehFile:write(vehicles[veh_hash]["scanner"], "\n")
 vehFile:write(vehicles[veh_hash]["percent"], "\n")
 vehFile:write(vehicles[veh_hash]["lastTripTime"], "\n")
 vehFile:write(vehicles[veh_hash]["lastTrip"])
 vehFile:close()
end

I checked the written files and the look correct. See below:

northRoute
89.583333333333
2
263617

and this function to read the data if the computer restarts;

function loadTruckData()
 local truckFiles = fs.childs("/")
 if (#truckFiles == 1) then
  print("No truck data found but thats OK.")
  return
 end

 for _, tFile in pairs(truckFiles) do
  if (tFile ~= "dev") then
   print("Processing: " .. tFile)
   local vehFile = fs.open("/" .. tFile, "r")
   vehFile:seek("set")

   print(vehFile:read())
   print(vehFile:read())
   print(vehFile:read())
   print(vehFile:read())
   print(vehFile:read())

   vehFile:close()
  end
 end
end

However the print(vehFile:read()) lines only print blank lines. Can't tell what I'm doing wrong.

RenaKunisaki commented 9 months ago

I've found file:read(999) works but file:read('a') returns an empty string.

Panakotta00 commented 9 months ago

This kinda intended behaviour since the string parameter can be fully implemented within lua, and due to some design choices file handles work like UNIX File Handles... you can check the FicsIt-OS or on the Discord check #your-resources to figure out how to use them properly