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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing without deleting in an existing ASCII file 1

Status
Not open for further replies.

GerritGroot

Technical User
Nov 3, 2006
291
ES
Hi,

Imagine that I've got a text file with, let's say, a hundred lines and I want to write something on line 34 without deleting the other lines.

Can this be done without rewriting all other lines?

I tried with combinations of ACCESS='APPEND' and then a REWIND(), but that deletes all stuff underneath the specific line (in this case 34)

Thanks,

Gerrit

P.S.: My file is ASCII, and not of a fixed format
 
You can do it with ACCESS='DIRECT' but then it has to be handled as a binary file. Text is just a variant of binary where the terminator is a LF. Just set a record length of 1 so every character is a record. If you know which character position the line 34 is in, you can jump straight there.

The problem is whether what you're writing is the same length as what is already there. If it is, then all well and good. If it isn't, you have a problem: you will still need to rewrite the remaining lines, shifting them either up or down.
 
Works!!

I didn't even have to know at which character position line 34 was, just counting the LF's was enough to get to 34-1

I used the transfer function to write exactly one byte (and not to overcomplicate things) as the original variable was a 4 byte integer.

Thanks,

Gerrit

P.S.: There seemed to be a CR as well as a LF at the end of each line. Is this usual??
(My textpad editor only showed the CR)
 
It depends on whether it is windows or linux.

On linux, you will just get an LF.

On windows it is CRLF but if you stop at the CR, you won't be at the end of line that's why I said the terminator is LF. In the editor CRLF is shown as 1 character even though it is actually 2. Windows files have problems if the CR is present and the LF is missing or if you have a mix of CR, LF and CRLF. If you look at a hex dump of the file, you will see 0D 0A.

Simple way to do a hex dump on windows

Pop up a cmd prompt
Type debug filename
Type d
It will show you 128 bytes
Type d again
It will show you the next 128 bytes
Type q to quit

There are fancy tools like hexedit but if you just want to have a look, the debug command is always there to be abused.
 
Hm, nice tool. I had to download S32EVNT1.DLL to get it working, but anyway. It also works slightly different in my case, I had to give "dd" to get the next 128 bytes, and next "ddd" and so on (at least it seemed so)

If you're working a lot with the cmd, you might as well like:


which makes life a lot easier.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top