Insert text into existing file - ruby-on-rails

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

Related

Write specific bytes inside a file using lua

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()

Delphi rewriting a single line

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.

How to add a string to a pot file with .php?

A lot of my website content is stored in a database for this reason I created a script that goes through the content stored in the database and extracts strings that must be added to my .pot file. (which already contains other strings).
I created a function called "add_string($s)" that should add one string to the .pot file but I'm not sure how to do it.
function add_string($s)
{
// Add string to .pot file?
}
How can this be done?
Thanks
A pot file is just a text file in certain format.
You can use file_put_contents with the FILE_APPEND flag to add text to a file.
With regards to the format of the data argument of the file_put_contents call, you will have
lines referencing files starting with #:, lines referencing instructions to translators starting with #. and then lines starting with msgid with the message id and msgstr the message value. You will want to include the newline \n after each line.
See here for an example of the pot file format.
You will probably want to keep track of your strings in PHP data structure as you write them to file or before you write them to file, to avoid duplicating message values in the template.

Set NSFileHandle seekToFileOffset to end of line

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.

filename..extension // Paperclip reprocess

Some kind of problem I can't resolveā€¦
In some app, a method called on :before_create was prefixing the file's extension with a double-dot (ex. /images/13402/medium/hey-1..jpg)
The problem is fixed for the new ones, but nothing occurs when I apply a reprocess! on the old ones; and I'd like to know if anyone could help about it
Reprocess / refresh only takes your original image and recreates the defined styles in your model class. So if your original image contains a file path with double dots in it, these are also applied to the generated styles. You have to clean up your original files and the stored file paths in your model records.
The only way I know would be to write a little script to modify this. Basically
foreach image
strip out double dots from original file name
rename file
store new file path in model record
end
And then rake paperclip:refresh

Resources