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.
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 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");.
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.
I'd like to get a file last modified time in Delphi.
Normally something like FileAge() would do the trick, only the problem is: if I overwrite *File A* with File B using CopyFile, File A's modified date is not updated with current overwrite time as it should(?)
I get that: CopyFile also copy file attributes, but I really need to get the modified date that also works when a file is overwritten.
Is there such function? My whole application relies on modification time to decide whether or not I should proceed with files!
EDIT Just to clarify: I'm only monitoring the files. It's not my application who's modifying them.
The documentation for CopyFile says:
File attributes for the existing file are copied to the new file.
Which means that you cannot use base your program on the last modified attribute of the file, or indeed any attribute of the file. Indeed there are all sorts of ways for the last modified attribute of the file to change. It can in fact go backwards in time.
Instead I suggest that you use ReadDirectoryChangesW to keep track of modifications. That will allow you to receive notifications whenever a file is modified. You can write your program in an event based manner based on the ReadDirectoryChangesW API.
If you can't use ReadDirectoryChangesW and the file attributes, then you'll have to base your decisions on the contents of the file.
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