can read text file Working on WIndows, on Android, cannot!

what could be the problem. .i have a “BOGGLE” type game where my database is stored in a “words.txt” file cointaining 2000 words… when i form words in the boggle game using windows simulator, corona is able to check if it exist, so im given points…

i try it on my android phone, and it does not recognize the word even if it exist in my text file(in the resource directory)  .this is frustrating knowing it can read my database textfile when im using windows, but when implemented on phone, it’s not! HELPPP

could you post the code showing the DB read logic?

*took the code from peach pellen

local function buildDictionary()

    local path = system.pathForFile(“wordlist.txt”,system.ResourceDirectory)

    local file = io.open(path,“r”)

    if file then

        for line in file:lines() do

            dictTable[line:upper()] = true

                  

        end

        io.close(file)

    end

–    return true

end

buildDictionary()

–function for checking if input is correct–

local function checkWord(word)

    local checkme = word:upper()

    if dictTable[checkme] then

            --print(“word exist”)

            return true

        else

        --print(“word is not in dictionary.”)

            return false

    end

    

end

—then my submit word mechanic looks something like

local function checkValidity(event)

         if event.phase == “began” then

                 --if checkWord() returns true

       

                    if checkWord(playerInput) then

                         print(“WORD IS VALID!”)

                          else

                          print(“WORD DOES NOT EXIST”)

        end

end

Do you have code in another module where you create your wordlist.txt file? If not, it won’t exist on the device. I’d suggest checking out the JSON tutorials as they are a bit more recent. The EGO stuff that Peach came up with is good, but it’s basically half the story:

http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/

http://coronalabs.com/blog/coronageek/corona-geek-hangout-58/

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

oh gawd, so you mean my ‘database access is flawed’? ok thnx. that was enlightening!  i only have a few hours before this app gets presented, . i wonder if i can port that to JSON easily :3  …but thnx, that was really helpful

how do i convert this ‘wordlist.txt’ containg:
 

APPLE

BEE

CAT

DOG

to a JSON readable file (that i will use as database)?

I’m not saying your database access isn’t necessarily flawed, I’m saying (from the code you’ve shown above) it doesn’t appear as though the database is being created at all. You’re going to want to reference those links I provided above. It might seem like a lot of reading, but it’s not that hard. If I can figure it out, anyone can!

im just wondering, it worked on her “BOGGLE” example. .  why shouldnt it work on my “BOGGLE-superclone” example :frowning:

First, as Panc Software says, make sure you’re creating your file. This function will create a new file if it doesn’t exist, but leave it if it does. So at the beginning of your app, call this with your contents:

[lua]

local createAndWrite = function(filename, contents)

    local path = system.pathForFile(filename, system.DocumentsDirectory)

    local readFile = io.open(path, “r”)

    if readFile then

        io.close(readFile)

        readFile = nil

        return false

    end

    local writeFile = io.open(path, “w”)

    if writeFile then

        writeFile:write(contents)

        io.close(writeFile)

        writeFile = nil

        return true

    end

end

[/lua]

By the way, here’s the JSON version of your list:

[lua]

[

  “APPLE”,

  “BEE”,

  “CAT”,

  “DOG”

]

[/lua]

  • Caleb

@panc,  my wordlist.txt is in my app folder along with the main.lua,settings.lua,images,etc. . i wrote the words line by line in notepad . .i called it using  local path = system.pathForFile(“wordlist.txt”,system.ResourceDirectory) as u can c in the buildDictionary() function

@caleb,   the program is working fine in my windows simulator. .everything is perfect. but the problem is that when i install the app on my android phone, it acts like it’s not reading the database from the “wordlist.txt” anymore. .

for example  when im on Windows simulator , and i form a word “gold” which is a word stored on my “wordslist.txt”, i get a score. but when i run in in my android phone, and form the world “gold”, nothing happens, it’s as if it doesnt recognize it… why doesnt it recognize the word “gold” when back in the Windows simulator it did and even triggered the score function.

Are you getting any messages in your console log on your device:  http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Are you checking to make sure when you read your file in that you are really reading it in and that it has the values you expect?  Adding prints in will help you determine this.

Rob

could you post the code showing the DB read logic?

*took the code from peach pellen

local function buildDictionary()

    local path = system.pathForFile(“wordlist.txt”,system.ResourceDirectory)

    local file = io.open(path,“r”)

    if file then

        for line in file:lines() do

            dictTable[line:upper()] = true

                  

        end

        io.close(file)

    end

–    return true

end

buildDictionary()

–function for checking if input is correct–

local function checkWord(word)

    local checkme = word:upper()

    if dictTable[checkme] then

            --print(“word exist”)

            return true

        else

        --print(“word is not in dictionary.”)

            return false

    end

    

end

—then my submit word mechanic looks something like

local function checkValidity(event)

         if event.phase == “began” then

                 --if checkWord() returns true

       

                    if checkWord(playerInput) then

                         print(“WORD IS VALID!”)

                          else

                          print(“WORD DOES NOT EXIST”)

        end

end

Do you have code in another module where you create your wordlist.txt file? If not, it won’t exist on the device. I’d suggest checking out the JSON tutorials as they are a bit more recent. The EGO stuff that Peach came up with is good, but it’s basically half the story:

http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/

http://coronalabs.com/blog/coronageek/corona-geek-hangout-58/

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

oh gawd, so you mean my ‘database access is flawed’? ok thnx. that was enlightening!  i only have a few hours before this app gets presented, . i wonder if i can port that to JSON easily :3  …but thnx, that was really helpful

how do i convert this ‘wordlist.txt’ containg:
 

APPLE

BEE

CAT

DOG

to a JSON readable file (that i will use as database)?

I’m not saying your database access isn’t necessarily flawed, I’m saying (from the code you’ve shown above) it doesn’t appear as though the database is being created at all. You’re going to want to reference those links I provided above. It might seem like a lot of reading, but it’s not that hard. If I can figure it out, anyone can!

im just wondering, it worked on her “BOGGLE” example. .  why shouldnt it work on my “BOGGLE-superclone” example :frowning:

First, as Panc Software says, make sure you’re creating your file. This function will create a new file if it doesn’t exist, but leave it if it does. So at the beginning of your app, call this with your contents:

[lua]

local createAndWrite = function(filename, contents)

    local path = system.pathForFile(filename, system.DocumentsDirectory)

    local readFile = io.open(path, “r”)

    if readFile then

        io.close(readFile)

        readFile = nil

        return false

    end

    local writeFile = io.open(path, “w”)

    if writeFile then

        writeFile:write(contents)

        io.close(writeFile)

        writeFile = nil

        return true

    end

end

[/lua]

By the way, here’s the JSON version of your list:

[lua]

[

  “APPLE”,

  “BEE”,

  “CAT”,

  “DOG”

]

[/lua]

  • Caleb

@panc,  my wordlist.txt is in my app folder along with the main.lua,settings.lua,images,etc. . i wrote the words line by line in notepad . .i called it using  local path = system.pathForFile(“wordlist.txt”,system.ResourceDirectory) as u can c in the buildDictionary() function

@caleb,   the program is working fine in my windows simulator. .everything is perfect. but the problem is that when i install the app on my android phone, it acts like it’s not reading the database from the “wordlist.txt” anymore. .

for example  when im on Windows simulator , and i form a word “gold” which is a word stored on my “wordslist.txt”, i get a score. but when i run in in my android phone, and form the world “gold”, nothing happens, it’s as if it doesnt recognize it… why doesnt it recognize the word “gold” when back in the Windows simulator it did and even triggered the score function.