Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Read+Write

Status
Not open for further replies.

Jamesis

Programmer
May 5, 2009
7
CA
I’m trying to write to a single line in a ASCII text file and apparently writeln() only works to append to a file.

What should I use to be able to write to a single line in a text file?? i.e Without opening up the entire file and rewriting it. Can I even do this?

Any help would be appreciated.
 
something like this:

-> add unit SysUtils to uses clause.

Code:
procedure AppendLineToFile(Line : String; Filename : String);

var FStream : TFileStream;

begin
 FStream := TFileStream.Create(Filename, fmOpenWrite or fmShareDenyNone);
 try
  Line := Line + #13#10; // append CRLF
  FStream.Write(Line[1], Length(Line));
 finally
  FreeAndNil(FStream);
 end;
end;

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
oops you only want to write one line

adapted code:

Code:
procedure WriteSingleLineToFile(Line : String; Filename : String);

var FStream : TFileStream;

begin
 if FileExists(Filename) then
  DeleteFile(Filename);
 FStream := TFileStream.Create(Filename, fmCreate);
 try
  FStream.Write(Line[1], Length(Line));
 finally
  FreeAndNil(FStream);
 end;
end;

I hope you get the idea :)

offcourse you can do this with the old counterparts (AssignFile, Rewrite, ...)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Isn't this going to delete my current file? I'm actually just trying to edit Line 1 of an existing file.

I'd like to preferably do this without opening the file because I need this to be efficient.

So my text file,

------- tmp.txt ------------
just writing some stuff hello
last night I went to the ...
asdfkj asdfkjak
----------------------------

Becomes,

------- tmp.txt -------------
I changed the 1st line
last night I went to the ...
asdfkj asdfkjak
------------------------------

Note all I did was edit the first line in the file and didn't effect anything else in the file.
 
no way. think about it!

the filesystem simply does not work like that.

if you want easy code to achieve this, one example:

Code:
procedure ChangeLineXInFile(Line : Integer; LineStr : String; Filename : String);

var StrList : TStringList;

begin
 StrList := TStringList.Create;
 try
  StrList.LoadFromFile(Filename);
  StrList[Line-1] := LineStr;
  StrList.SaveToFile(Filename);
 finally
  FreeAndNil(StrList);
 end;
end;

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Ahh Thanks. But you say that the file system can't work in what way? Are you saying that I must always open the entire contents? Thanks for the example you posted!

This file that I'm using should always maintain the same size. In other words.. if I edit line 2 (below) and there isn't enough space on this line it should start overwritting the line below it. Is this possible?? I'm actually trying to maintain the file size to prevent file fragmentation and also to log stuff in this fixed size file.

------------ tmp.txt ------
original line 1 some info
original line 2 some info
original line 3 some info
original line 4 some info
----------------------------
 
You may want to change the file format. You can view it as an array of characters from the start to the end of the file.

or

Create a database consisting of string records using database access tools to read & write.

What are you trying to store in the file since it is oK to overwrite the next line? Is there a maximum length string you have in mind?
 
Thank you! I learned a lot from this! I've finally decided to use a TFileStream with read/write and seek.

One more thing, I've figured out how to make my file bigger(I just write white space the file and it grows :)

But how can I make my file smaller? Can I write something from a position in my file till EOF that specifies my file ends here? Or is there another approach to this?
 
in text file you have the truncate command:


with TFileStream you can use the SetFilePointer/SetEndOfFile API calls:

this example assumes filesize <= 2GB, position is the position where you want to truncate the file:
Code:
if (SetFilePointer(FileStream.Handle, Position, 0, FILE_BEGIN) = Position then
 SetEndOfFile(FileStream.Handle)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Be careful with TFilestraem its a lot less forgiving of errors/changes in data formatting than the Simple Write and Writeln commands, especially if you are writing object properties.



Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top