How do you print the text from a file in Lua? - file

I would like to be able to print the text from a file. When I use print(io.open(filename,r)), it gives me the file name.

io.open returns a file handle, to read the content, you need io.read:
local f = io.open(filename,r)
print(f:read("*a"))
Learn about IO from The I/O Library.

Related

directing file output to the console

I'm working with a C-function foo(FILE* file) that outputs some text to a file.
Rather than write to a .txt file, I'd like foo to write to the console.
Is there a way to pass 'the console' as a FILE*? (And if so, how) ?
(foo is part of a library and I can't edit the source code directly)
There's a special file object called stdout that will write to the console (assuming the shell didn't redirect anything).
foo(stdout);

Read/Write highscore from a file (Lua - Corona SDK)

Here's my issue: I have a file with the highscore written in it (just the first line, no nicknames, just the highscore), I need to read that line and compare it with the actual score obtained in the game session, if the score is higher, overwrite the file with the new value, but if I try to read it I get a null value... Seems like i'm not reading it the right way. What's wrong with my code?
Thanks for the help!
local path = system.pathForFile( "data.sav", system.DocumentsDirectory )
local file = io.open( path, "w+" )
highscore_letta = file:read("*n")
print(highscore_letta)
if (_G.player_score > tonumber(highscore_letta)) then
file:write(_G.player_score)
end
io.close( file )
I had this problem myself. I found out that if you open a file in "w+" mode, the current contents are deleted, so that you can write new contents. So to read and write you have to open the file twice. First, you open the file in "rb" mode and get the file contents, then close it. Then you reopen it in "wb" mode, write the new number, and close it.
In Windows, you need "b" in the file mode. Otherwise, the strings that you are reading and writing may be modified in unexpected ways: for instance, a newline ("\n") may be replaced with carriage return–newline ("\r\n").
The file modes that Lua supports are borrowed from the C language. (I found a description on page 305 of what I guess is a draft of the C specification.) I think the Lua manual sort of assumes that you will know what these modes mean, as an experienced C programmer would, but to me it wasn't at all obvious.
Thus to read a number and then write a new one:
local filepath = "path/to/file"
-- Create a file handle that will allow you to read the current contents.
local read_file = io.open(filepath, "rb")
number = read_file:read "*n" -- Read one number. In Lua 5.3, use "n"; the asterisk is not needed.
read_file:close() -- Close the file handle.
local new_number = 0 -- Replace this with the number you actually want to write.
-- Create a file handle that allows you to write new contents to the file,
-- while deleting the current contents.
write_file = io.open(filepath, "wb")
write_file:write(new_number) -- Overwrite the entire contents of the file.
write_file:flush() -- Make sure the new contents are actually saved.
write_file:close() -- Close the file handle.
I created a script to do these operations automatically, as they're somewhat annoying to type every time.
The mode "r+" or "r+b" is supposed to allow you to read and write, but I couldn't get it to work when the original contents are longer than the new contents. If the original contents are "abcd", four bytes, and the new contents are "efg", three bytes, and you write at offset 0 in the file, the file will now have "efgd": the last byte of the original contents is not deleted.

Python - Reading from a file, any file

I want to make a function that reads from a txt file, thats given by the user on a function. I know how to open a specific file, on python, but I don't know how to make it any file on a function.
For example I want do something like this:
Read_board(irock.txt) --- irock.txt, can be any other fale, it's the argument that the function recieves. Read the line on the file and return it.
This is my final code
def le_tabuleiro(txt):
text = ((open(txt, 'r')).readline())
print text
Thanks everyone.
You need to read from the file object, not its name. Try this:
def read_board(f):
ficheiro = open(f, "r")
line = ficheiro.readline()
return line
Also note that f is a string containing the file name so pass the variable unquoted to open().
One other thing worth mentioning is that you can/should use a with statement to open the file. This will ensure that the file is properly closed once the function returns (or if there is an exception):
def read_board(f):
with open(f, "r") as ficheiro:
return ficheiro.readline()

Can I pass text file as an argument in a main function in C?

I am wondering if I could pass file as an argument in a main function? I mean not a name of the file but file itself.
Not unless something external (e.g. a bash script) reads in the file and adds it as an argument. If there is a binary 0 in the file, that would be interpreted as the end of string.
You can achieve something similar using input redirection, where the contents of a file is redirected to stdin, e.g.
myprogram < myTextFile
You would then be able to read the contents of the file by reading from stdin.
You could, as long as there's no 0 bytes in the file.
Also, you shouldn't.
If you want to know how to do that, it depends on the operating system.

Search a pattern in .gz file in C

I want to read a .gz file (text.gz) with 300MB length and search a pattern in it. I opened the text file in a binary format using fopen with "rb" and stored it in a buffer. When I search a pattern that I know it exists in the text, the result is wrong. When I debug the program, the elements of the buffer are different from what I expect. Do I have to read and store these kind of files in other ways??????
You might try using zlib and gzread to read the file.
http://zlib.net/manual.html
Try this.
gunzip -c file.gz | grep <pattern>
If the program is exiting and failing to read the file, a real common problem is that you don't close the file in Notepad or whatever is using it and the FileIO fails due to not being able to access the file. Make sure you don't have anything with that file open before you test your program.

Resources