I want to write a specific byte inside a .txt file, for example:
Text File
Something
Code
Som4thing
I want to do like that, but without overwriting the whole file using io.write().
I hope it was easy to understand my question.
First, you need to figure out what mode to open the file in. r won't let you write, so it's out. a and a+ will only ever let you write to the end, so they're out. w and w+ erase the whole file, so they're out. That leaves r+.
Next, you need to get to the right place in the file. The seek function does that. In your case, you want to go to 3 bytes past the beginning.
Finally, simply write your data and close the file.
local file = io.open('filename.txt', 'r+')
file:seek('set', 3)
file:write('4')
file:close()
Related
Let's say I have an UIImage cached in my Cache folder:
/.../Cache/Image Cache/<firstImage.id>
Now I want this folder to only ever have 10 image cached at a time, so if a new one comes in I want to take a file and replace the entire file and not just the contents of it. I.e.
/.../Cache/Image Cache/<firstImage.id> becomes
/.../Cache/Image Cache/<secondImage.id>.
As far as I can tell, replaceItem(at:withItemAt:backupItemName:options: only replaces the contents of the file but the file name remains the same. And I'm not too sure what replaceItem(at:withItemAt:backupItemName:options:resultingItemURL:) does even though it might be what I'm looking for (I don't know what an AutoreleasingUnsafeMutablePointer<NSURL?>? was but it sounded dangerous so I decided to leave it alone, specially since it has the word "unsafe" in it).
Is there a straightforward way of doing using an in-built function or is manually deleting the old file and adding the new file the best way? Please let me know.
Thanks in advance!
Does rails have a way to implement read streams like Node js for file reading?
i.e.
fs.createReadStream(__dirname + '/data.txt');
as apposed to
fs.readFile(__dirname + '/data.txt');
Where I see ruby has
file = File.new("data.txt")
I am unsure of the equivalent in ruby/rails for creating a stream and would like to know if this is possible. The reasons I ask is for memory management as a stream will be delivered piece by piece as apposed to one whole file.
If you want to read a file in Ruby piece-by-piece, there are a host of methods available to you.
IO#each_line/IO::foreach, also implemented in File to iterate over each line of the file. Neither reads the whole file into memory; instead, both simply read up until the next newline, return, and pause reading, barring a possible buffer.
IO#read/IO::read takes a length parameter, which allows you to specify for it to read up to length bytes from the file. This will only read that many, and not the whole thing.
IO::binread does the same as IO::read, but will open the file in binary mode.
IO#readpartial appears to be very similar or identical to IO#read, but is also worth looking at.
IO#getc and IO#gets both read from the file until they reach the end of what they'll return, as far as I can tell.
There are several more that I'm looking for right now.
I use the following to save screen output to a file
writefile("file.txt"),
tex(expression),
closefile()
The above sends the output of the tex() to the file automatically. which is all and well and what I want. (side-point: It also sends an annoying NIL line each time to the file, which I had to parse put later).
Now, when running the above code again, the file is appended to, which is not what I want. I want to either overwrite the file each time, or if there is a way to delete the file, so I can call delete on it before.
I looked at help and not able to find a command to delete a file, and I also see no option to tell writefile() to overwrite the file?
Is there an option or way around this? I am on windows 7, Maxima version: 5.36.1
Lisp: SBCL 1.2.7
I guess you are trying to capture the output of tex into a file. If so, here are a couple of other ways to do it:
tex (expr, destination);
where destination is either a file name (which is appended) or a stream, as created by opena or openw and closed by close. By the way, destination could be false, in which case tex returns a string.
with_stdout (destination, tex (expr));
where again destination is either a file name (which is appended or clobbered, as determined by the global flag file_output_append) or a stream.
with_stdout could be useful if you want to mix in some output not generated by tex, e.g., print("% some commentary");.
Can we rewrite a single line of a textfile and then save it and close it?
For example, i need to rewrite the first line, and keep all the others. Is there a function to do this or do i have to copy the whole file after changing a single line?
My file contains more than a thausand lines, and i only need to change the first line.
Example of file:
test;test1;test2
other;other;other
other;other;other
x1000
and then
something;something;something
other;other;other
other;other;other
x1000
See what i mean? I just want to keep my file like it is but change the first line. I could copy the whole file and paste it after i changed the first line but i wonder if there a method already included in delphi to only change a particular line in a text file. Thanks!
This is not possible. Files are stored linearly and do not support insertion. If your line was a fixed length, then you could overwrite it. However, you wish to replace the line with new content that is longer. That cannot be done. You'd need to re-write the entire file.
A database may be more appropriate for your needs than a text file.
I think that the easiest approach here would be using a TStringList in this way :
procedure InPlaceFileEdit(fFile : String);
begin
F:=TStringList.Create;
try
F.LoadFromFile(fFile);
F.Strings[0]:='something;something;something' ;// Change the contents of the first line
F.SaveToFile(fFile);
finally
F.Free
end
end;
Of course this is a trick that rewrite the file each time entirely.
I have a loop that each time has new data in an array. I would like to save that array in a .mat file after each insertion of the loop and I want the name of the file to change with the loop value. Say I go through my loop 5 times, I want to have 5 files
array_1.mat
array_2.mat
array_3.mat
array_4.mat
array_5.mat
To test my idea I wrote:
for A=1:10;
filename = sprintf('array_%d.mat', A)
save('-mat', filename, 'A');
endfor
after running this code in octave, I do get 5 files with the correct names but they don't seem to be .mat file, I can't load them again into octave. I have tried a lot of other small syntax changes and nothing seems to work. Can anyone tell me what i am doing wrong and/or give my a test example that changes the name of a.mat file with the loop variable.
Thanks
That works for me so I'm guessing your problem is on the load command. How are you loading the files? You should load array_5.mat