Ho create a good Save Status file

Hi, i know that probably is a silly question but i cannot read and save at same time into my saving sile. :frowning:

You want to read and write a file without closing it in-between accesses? 
 
First of all, not closing the file is a bad idea.  You’re asking for complications.
 
Second, there isn’t actually true simultaneity in Corona.  You can consider your Lua code to be executing single-threaded and basically linearly.
 
If you merely need code to allow you to read, write, append, etc. a file … then get my io.* extension here:
 
https://github.com/roaminggamer/SSKCorona/tree/master/ssk/extensions
 
It includes these features:

-- base is optional in all cases and defaults to system.DocumentsDirectory -- other choices are: system.ResourceDirectory or system.TemporaryDirectory -- Function Syntax's: -- io.exists( fileName [, base] ) -- does file exist io.readFile( fileName [, base] ) -- read contents of file into a single string io.writeFile( dataToWrite, fileName [, base] ) -- write contents of single string to file io.appendFile( dataToWrite, fileName [, base] ) -- append contents of single string to file (create if needed) io.readFileTable( fileName [, base] ) -- read all lines in file into table, were each entry is a line from the file. Lines are separated by newline (\n)

To use it the library, simply download it and store it in your root folder (same level as main.lua), name the file " io_ext.lua" (not “io.lua”) then do this in main.lua.

require "io\_ext"

Ok, good!! Surely i will download this, thx ;). But for real i cannot understand why i get error on my. I poste here my code:

 function unlockLevel(precLvl)
 
     local pathSave = system.pathForFile( “Save.txt”, system.DocumentsDirectory );
  

– Determine if file exists
  
  
  --pathW = system.pathForFile( “Save.txt”,system.DocumentsDirectory);
  local hFile,errReadingSave = io.open(pathSave, “r”) --Reading.
  
  
  
  

  
  local getNext = false;
  local getLasts = false;
  local countF = 0;
  local lines = {};
  local newLine = “”;
  local restOfFile;
  --local lineCt = 1;
  for line in hFile:lines() do
  print(“XOXOXOOXOXXO”)
   if(getNext) then
      for w in string.gmatch(line, “%S+”) do
       countF = countF + 1;
       if(countF == 4)then
        newLine = newLine…“1”;
       else
        newLine = newLine…w…" ";
       end
       
            
      end
      print("NUOVA LINEA ORA: “…newLine);
      lines[#lines + 1] = newLine;
      getNext = false;
      
   
   else
      if(line == precLvl) then
       lines[#lines + 1] = line;
       getNext = true;
         
      else
       lines[#lines + 1] = line;
      end
   end
   
   
   
  end
  
  for i = 1, #lines do
    print(”^^^^^^^^^^ "…lines[i] )
  end
  
  hFile:close();
  hFile = nil
  local updSave = io.open(pathSave,“w”);
  updSave:write(lines);
  
  

 end

As error i get: attempt to index local ‘updSave’ (a nil value)

This line failed to open the file:

local updSave = io.open(pathSave,"w");

Why? I don’t know.  That’s why I have a library that I know works.  Then I never have to figure it out again. :slight_smile:  

Lazy programmer tip: Solve once, re-use forever.

Hi, i used your library RoamingGamer  but i dont know i have empty table. I post my code now:

print( "File does not exist!CREATE IT" )   --local fileSave = io.open( pathSave, "w" );   localPathSave = system.pathForFile( "Save/Save.txt");   local readLocalSave = io.open( localPathSave, "r" );      tblSave = io.readFileTable( "Save.txt" , localPathSave );     print(" muovo -- LUNGHEZZA !!!!!!!!!! "..#tblSave)     for i=1,#tblSave do      print( "@@@@@@@\> "..tblSave[i] )     end

Length of table that should contains what  is in file is empty. What i wrong ??

Don’t mix normal io.* calls with my library.  Do one or the other.

Try this:

local testContent = "This is a test" io.writeFile( testContent, "myTest.txt" ) print( "Exists? ", io.exists( "myTest.txt" ) ) local contents = io.readFile( "myTest.txt" ) print( "Contains: ", contents )

Also, don’t mix in calls to system.pathForFile().  I do that already in my library.

You want to read and write a file without closing it in-between accesses? 
 
First of all, not closing the file is a bad idea.  You’re asking for complications.
 
Second, there isn’t actually true simultaneity in Corona.  You can consider your Lua code to be executing single-threaded and basically linearly.
 
If you merely need code to allow you to read, write, append, etc. a file … then get my io.* extension here:
 
https://github.com/roaminggamer/SSKCorona/tree/master/ssk/extensions
 
It includes these features:

-- base is optional in all cases and defaults to system.DocumentsDirectory -- other choices are: system.ResourceDirectory or system.TemporaryDirectory -- Function Syntax's: -- io.exists( fileName [, base] ) -- does file exist io.readFile( fileName [, base] ) -- read contents of file into a single string io.writeFile( dataToWrite, fileName [, base] ) -- write contents of single string to file io.appendFile( dataToWrite, fileName [, base] ) -- append contents of single string to file (create if needed) io.readFileTable( fileName [, base] ) -- read all lines in file into table, were each entry is a line from the file. Lines are separated by newline (\n)

To use it the library, simply download it and store it in your root folder (same level as main.lua), name the file " io_ext.lua" (not “io.lua”) then do this in main.lua.

require "io\_ext"

Ok, good!! Surely i will download this, thx ;). But for real i cannot understand why i get error on my. I poste here my code:

 function unlockLevel(precLvl)
 
     local pathSave = system.pathForFile( “Save.txt”, system.DocumentsDirectory );
  

– Determine if file exists
  
  
  --pathW = system.pathForFile( “Save.txt”,system.DocumentsDirectory);
  local hFile,errReadingSave = io.open(pathSave, “r”) --Reading.
  
  
  
  

  
  local getNext = false;
  local getLasts = false;
  local countF = 0;
  local lines = {};
  local newLine = “”;
  local restOfFile;
  --local lineCt = 1;
  for line in hFile:lines() do
  print(“XOXOXOOXOXXO”)
   if(getNext) then
      for w in string.gmatch(line, “%S+”) do
       countF = countF + 1;
       if(countF == 4)then
        newLine = newLine…“1”;
       else
        newLine = newLine…w…" ";
       end
       
            
      end
      print("NUOVA LINEA ORA: “…newLine);
      lines[#lines + 1] = newLine;
      getNext = false;
      
   
   else
      if(line == precLvl) then
       lines[#lines + 1] = line;
       getNext = true;
         
      else
       lines[#lines + 1] = line;
      end
   end
   
   
   
  end
  
  for i = 1, #lines do
    print(”^^^^^^^^^^ "…lines[i] )
  end
  
  hFile:close();
  hFile = nil
  local updSave = io.open(pathSave,“w”);
  updSave:write(lines);
  
  

 end

As error i get: attempt to index local ‘updSave’ (a nil value)

This line failed to open the file:

local updSave = io.open(pathSave,"w");

Why? I don’t know.  That’s why I have a library that I know works.  Then I never have to figure it out again. :slight_smile:  

Lazy programmer tip: Solve once, re-use forever.

Hi, i used your library RoamingGamer  but i dont know i have empty table. I post my code now:

print( "File does not exist!CREATE IT" )   --local fileSave = io.open( pathSave, "w" );   localPathSave = system.pathForFile( "Save/Save.txt");   local readLocalSave = io.open( localPathSave, "r" );      tblSave = io.readFileTable( "Save.txt" , localPathSave );     print(" muovo -- LUNGHEZZA !!!!!!!!!! "..#tblSave)     for i=1,#tblSave do      print( "@@@@@@@\> "..tblSave[i] )     end

Length of table that should contains what  is in file is empty. What i wrong ??

Don’t mix normal io.* calls with my library.  Do one or the other.

Try this:

local testContent = "This is a test" io.writeFile( testContent, "myTest.txt" ) print( "Exists? ", io.exists( "myTest.txt" ) ) local contents = io.readFile( "myTest.txt" ) print( "Contains: ", contents )

Also, don’t mix in calls to system.pathForFile().  I do that already in my library.