write data to the top in file

shireeshn

Member
i have an urgent requirement,

i.e, i have a file which has a data, now i want to write the data to the top of the file. Can we do it ?

ex:-

file content:-
===========
abc
bbc
ccd

i have to write data (sssss) to file after writing it should be

now the file Content
===============
sssss
abc
bbc
ccd


this is a sample file , actull file content will have huge data in file.
 

TomBascom

Curmudgeon
Not really.

This is the same problem as in the editing example. You essentially have to re-write the file.
 

sdjensen

Member
Come on Tom, I know you know better. ;p (You will have to rewrite the file but you could have given out some examples) :)
Of course it can be done. It really depends on how much memory your have or how much time you are ready to spend on the task.​


Code:
[LEFT]DEFINE VARIABLE FromName AS CHARACTER NO-UNDO.[/LEFT]
DEFINE VARIABLE ToName AS CHARACTER NO-UNDO.
[LEFT]FromName = "c:\oldfile.txt".[/LEFT]
 
[LEFT]ToName = "c:\newfile.txt".[/LEFT]
 
 
[LEFT]DEFINE VARIABLE bb AS MEMPTR NO-UNDO. 

ASSIGN  
[LEFT]FILE-INFORMATION:FILE-NAME = FromName  
SET-SIZE(bb)               = FILE-INFORMATION:FILE-SIZE. 
INPUT FROM value(FromName) BINARY NO-CONVERT.
IMPORT bb.
INPUT CLOSE.
DEFINE VARIABLE newMem AS MEMPTR    NO-UNDO.
DEFINE VARIABLE newStr AS CHARACTER NO-UNDO.
newStr = "Extra line in top" + chr(13).
OUTPUT TO VALUE(ToName) BINARY NO-CONVERT.
PUT UNFORMATTED newstr SKIP.
OUTPUT CLOSE.
COPY-LOB FROM bb TO FILE toName APPEND.
SET-SIZE(bb) = 0.[/LEFT]
[/LEFT]

Or​

Code:
[LEFT]DEFINE VARIABLE FromName AS CHARACTER NO-UNDO.[/LEFT]
DEFINE VARIABLE ToName AS CHARACTER NO-UNDO.
DEFINE VARIABLE InLn AS CHARACTER NO-UNDO.
 
[LEFT]DEFINE STREAM iFile.[/LEFT]
 
[LEFT]DEFINE STREAM oFile.[/LEFT]
 
[LEFT]FromName = "c:\oldfile.txt".[/LEFT]
 
[LEFT]ToName = "c:\newfile.txt".[/LEFT]
 
 
[LEFT]INPUT stream iFile from value(FromName) .[/LEFT]
 
[LEFT]OUTPUT stream oFile to value(ToName) .
PUT STREAM oFile UNFORMATTED "extra line in top" SKIP.[/LEFT]
 
[LEFT]DO WHILE TRUE ON ENDKEY UNDO, LEAVE:[/LEFT]
 
[LEFT]IMPORT STREAM iFile UNFORMATTED Inln.[/LEFT]
 
[LEFT]IF inln = "" THEN

PUT STREAM oFile UNFORMATTED CHR(13).
[LEFT]ELSE 
PUT STREAM oFile UNFORMATTED InLn SKIP.
END.[/LEFT]
[/LEFT]

 
 
 
[LEFT]INPUT STREAM iFile CLOSE.[/LEFT]
 
[LEFT]OUTPUT STREAM oFile CLOSE.[/LEFT]
 

TomBascom

Curmudgeon
Well, yeah, but the ball game was getting started and it is essentially a variation on his question from last week...
 

shireeshn

Member
Well, yeah, but the ball game was getting started and it is essentially a variation on his question from last week...

:) tom, you misunderstood me. sorry it was not my intenstion,I know this way, but i thought can i get any other way to do, that is my intenston and requirement, i have posted this.

i have posted this boz , in this group there are lot of active devlopers , so i have posted, so i can get fast result.


:)
 

shireeshn

Member
yes i have tried this, and i got the result as expected.

In first prog i have found copy-LOB,i thing this for open edge. i am not able to work on it boz iam on 9.1D.

Any way thanks.
 

TomBascom

Curmudgeon
I'm not hurt. But I don't have infinite supplies of time either.

BTW, 9.1D is ancient, obsolete and unsupported. It lacks many useful features. Such as COPY-LOB. You should upgrade.

It is, essentially, the same question. You need to rewrite a file. This time instead of replacing some text you are inserting text at the start.

File Handling 101 -- there are only two things that you can easily do "in place":

1) Append to a file.
2) Replace a fixed amount of data with the same amount of data.

Everything else is most easily accomplished by reading the original, and rewriting the output.
Code:
/* rewrite.p
 *
 */

define stream inFile.
define stream outFile.

define variable i   as integer no-undo.
define variable txt as character no-undo.

define variable inFileName  as character no-undo format "x(30)".
define variable outFileName as character no-undo format "x(30)".

update inFileName outFileName.

input  stream inFile  from value( inFileName ).
output stream outFile to   value( outFileName ).

/* this is where you would insert text at the top of the file...
 */

repeat:

  txt = ?.

  import stream inFile unformatted txt.
  if txt = ? then leave.

  /* this would be a good place to edit the line, maybe using REPLACE()...
   */

  if txt = "" then
    put stream OutFile unformatted skip(1).
   else
    put stream outFile unformatted txt skip.

end.

input  stream inFile  close.
output stream outFile close.

return.
 
Top