What I want to be able to do is write a string to the end of every line in a file. Right now i have a NSFileHandle writing to the file but only at the end of the file. How can I set it to the end on the line?
You will need to read the stream and search for whatever line delimiter is being used in your file, usually 0x0a (nl), 0x0d (cr), or sometimes cr/lf.
If the file is small enough to read into memory, you could read it into an NSString, use componentsSeparatedByString: (with your delimiter), and then use componentsJoinedByString: (with your end of line addition and the delimiter) to create the aggregate result.
Even if you seek to the end of a line, if you write to the file at the point, you will overwrite the start of the next line. You can't insert text into a file by seeking to a spot and writing.
Your best option is to write out to a temporary file. Open the original file for reading. Read each line and write it to the new file. Then write the text you want to add to the end of the line. Repeat this for each line.
Once you have written all of the new lines to the new file, delete the old file and rename the new file.
Related
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()
I am trying to figure out how to take a string that is the file contents of a mp4 file and write a properly formatted mp4 file. Currently I am just throwing the string into a file and slapping a .mp4 extension on it, but this resulting file cannot be played by any video players (I am assuming because of all the missing meta data).
def write_mp4(mp4_string)
file = File.new('movie.mp4', 'w')
file.puts(mp4_string)
file.close
end
For context, I am doing this in a Ruby on Rails application, not sure if that changes anything. Please help thanks.
Use "wb" mode, which will suppress EOL conversions and set the encoding properly
Use write, not puts, as the latter inserts an extra EOL
You could use a shortcut: File.write('movie.mp4', mp4_string, mode: 'wb') - or even File.binwrite('movie.mp4', mp4_string).
Of course, make sure the string actually contains a correct file before - for example, if mp4_string.encoding doesn't return #<Encoding:ASCII-8BIT>, you probably done goofed somewhere before the writing step, too :)
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 collection of daily MP3s that I'm expected to upload to a web server once a month — they're named with a consistent format (ex. 10-17-11 Always Expect the Best # 1.mp3) and for each file, I have to generate an .m3u file with the URL to the web-server link. At the moment, I manually create each .m3u file and save it in relation to the MP3.
There must be a way to generate the .m3u files automatically — they usually are in the format of http://url/audio/2011/10-17-11_.mp3. I've created a plain text list of each filename on a separate line — if possible, I'd like to take that list and parse it into individual .m3u files.
I'm not sure what I should be using to do this — Python, Ruby, maybe just AppleScript?
Any help is greatly appreciated. Thanks!
you probably could do it with any of those, i'll give you a few pointers with python:
with open("my_file","r") as fin: ## open a file for reading
i = 0
for line in fin: ## iterate through all lines
newline = line + line.split(" ")[0] ## create a new line
with open("output"+str(i),"w") as fout: ## open a file for writing
fout.write(newline) ## write...
i+=1
This script reads a file and for each line appends the first word and writes it to its own file.
You can modify the loop code to aggregate several lines each time and write them to a file. And you can extract information from the line you read and use it to construct the new line.
I have a txt file.how can I place some text among that and don't overwrite it? because when I use for example f.puts "aaaaaaaaaaaaaaa" Ruby overwrites my txt file .
thanks
You need to open it in append mode
File.open("file.txt", "a+"){|f| f << "aaaaaaaaaaaaaaaaaaa" }
Check out your previous question
File opening mode in Ruby
If you're asking how to insert text into the middle of an existing file, as below, you can't:
Original file first half, Original File second half
becomes:
Original file first half, Inserted text, Original File second half
You need to make a new file, copy the first half of the original into it, then write the new text, then copy the rest of the original file.
You've got to set the mode for the file when using open on a file.
There are more details here : http://www.ruby-doc.org/core/classes/IO.html#M000889