arunkw
19th June 2003, 11:17
Hi All,
I am trying to append a string value "first.line.file" at the begining of the file, but I am not able to do, instead string value "first.line.file" gets appended at the end of file.
Here is the code
|***************************************************
long write.file.number
string first.line.file.rec(50),second.line.file.rec(50)
long return.seek
|* variable path and file are strings
write.file.number = seq.open(path&"/"&file,"a")
if write.file.number > 0 then
return.seek = seq.seek(0,0,write.file.number)
e = seq.puts(first.line.file.rec,write.file.number)
e = seq.close(write.file.number)
endif
|***************************************************
Please advice
Thanks and Regards
Arun Kumar
mark_h
19th June 2003, 16:25
I believe you want to open the file with mode "w" rather than "a".
Mark
estotz
19th June 2003, 16:56
Ya, probably because you opened in append mode.
You an also use function seq.rewind, which is equivalent to what you coded.
NvanBeest
19th June 2003, 18:51
For seq.rewind to work, you'll have to open it with a plus added to the mode, like "a+"
Regards,
Nico
NPRao
19th June 2003, 20:16
Mark:
I believe you want to open the file with mode "w" rather than "a".
Opening a file in "w" mode, makes the file size 0 and overwrites the existing contents. You will loose the file info.
Hence, if one likes to check the file existance one need to use -
fp = seq.open(dumpfpath,"r")
if fp < 0 then
mess("zmadms0036", 1, dumpfpath) |* File Opening Error: %1$s
choice.again()
endif
But if you like to check if a file exists and it can be write-able
fp = seq.open(fpath, "a")
if fp < 0 then
mess("zmadms0036", 1, fpath) |* File Opening Error: %1$s
e = seq.close(fp)
choice.again()
endif
Nico is correct -
For seq.rewind to work, you'll have to open it with a plus added to the mode, like "a+"
Refer to - seq.open() (http://www.baanboard.com/programmers_manual_baanerp_help_functions_directory_file_operations_seq_open)
"a" Open for writing. The file is created if it does not already exist. The current file position is placed at the end of the file.
The file position is placed at end of the file before every write statement, even if the previous file action was a seq.seek().
"a+" Same as "a", but the file can also be read.
arunkw
20th June 2003, 07:30
Experiment 1
I tried using "a+" instead of "a" but it did not worked
It gave same effect of appending the string to end of the file
Experiment 2
I tried using "w" instead of "a" but it did not worked
my file was overwritten with just a string present in the file, thus I lost all my previous content of file
Note: that the below code is equivalent, I got return.seek as sucess "0" (i.e Now the file pointer is at the begining of the file)
but effect is not seen in the file that is processed
return.seek = seq.seek(0,0,write.file.number)
return.seek = seq.rewind(write.file.number)
Thanks
Arun Kumar
mark_h
20th June 2003, 16:24
This worked for me:
fp = seq.open("./error.528","r+")
rc = seq.seek(0,0,fp)
rc = seq.puts("test",fp)
seq.close(fp)
You tried the others you should have tried "r+". :) Sorry bout the earlier post I should have read the help close.
Mark
thieuf
20th June 2003, 17:07
How about this,
read the file and copy the lines into a temp file, unlink the original file and recreate the file starting with the first line you want to put in and reading the rest from the temp file.
If you can rename a file then you only have to write the first line an read and add the lines from the original file. then delete the original and rename your temp file. (but i don't know if this is possible in Baan ....)
Good Luck
arunkw
23rd June 2003, 12:08
I have file which is very very large in size. so much that I can't edit it in vi or notepad
I have to append two of my own string at the start of the file
Now since I had/have faced lot of problem In doing that.
I am using other suggestion of creating a temporary file append my string first and then the whole content of the file on it
I am using this code to do it
long read.file,flat.file,temp.file
string first.line.file.rec(50)
string second.line.file.rec(50)
extern domain tcmcs.s999m read.line |* string of length 999
temp.file = seq.open(path&"/"&file&"stamped","a+")
if temp.file > 0 then
e = seq.puts(first.line.file.rec,temp.file)
e = seq.puts(second.line.file.rec,temp.file)
e = seq.close(temp.file)
endif
flat.file = seq.open(path&"/"&file,"r")
temp.file = seq.open(path&"/"&file&"stamped","a+")
if flat.file > 0 then
while not seq.eof(flat.file)
e = seq.gets(read.line,2048,flat.file)
e = seq.puts(read.line,temp.file)
endwhile
e = seq.close(flat.file)
e = seq.close(temp.file)
endif
I want to know if this is fool proof. I am asking this because
my file size are too large to view in vi or notepad.
Please advice
Thanks for the help guys
and golden quote from "NPRao":The file position is placed at end of the file before every write statement, even if the previous file action was a seq.seek().
is infact a key or should is say hindrance to solve this problem.
Anyways help me and advice me
Arun
thieuf
23rd June 2003, 13:57
Have you tried Ultra Edit to read the file.
This is a powerful editor.
Freeware versions (30 days) are available on the net.
Reagrds Thieuf
Juergen
23rd June 2003, 14:23
Hi Arun,
if you are on Unix have you try to use some other possibilities like
cat file_with_two_lines very_big_file > new_file
NPRao
24th June 2003, 00:04
Nevertheless interesting thread... :)
I think we should rename the thread from "Unable to append a string at start of the file " to "How to prepend a string at the beginning of the file".
I had a similar problem and I couldnt get a direct solution and I had to use 2 files to make it to work.
Arun,
You are now in the right path. I made a temporary file, put 2 lines of text and then read the other file and put all the text into this and move the new file over the old one.
Theif,
If you can rename a file then you only have to write the first line an read and add the lines from the original file. then delete the original and rename your temp file. (but i don't know if this is possible in Baan ....)
This is very much possible in BaaN tools using - Directory and file operations synopsis (http://www.baanboard.com/programmers_manual_baanerp_help_functions_directory_file_operations_synopsis)
Juergen,
I would prefer not to use the "cat" or OS specific commands if I can have alternatives in BaaN tools to make it OS independent code.
Refer to - Cat two files (http://www.baanboard.com/baanboard/showthread.php?s=&threadid=9996&highlight=append)
Mark,
This worked for me:
This actually does not work. Here is an example-
>cat np
hello1
hello2
Here is the code-
function main()
{
long fp
string buffer(256)
fp = seq.open("np","r+")
if fp < 0 then
mess("zmadms0036", 1, "/home/bsp/np")
|* File Opening Error: %1$s
exit(0)
endif
e = seq.seek(0,0,fp)
buffer = "test-1"
e = seq.puts(buffer,fp)
seq.close(fp)
}
>cat np
test-1
hello2
jaapzwaan
24th June 2003, 10:07
Note that the above code only works if all lines have equal length.
If the file contains the following lines:
hello
hello2
then the result will be like this:
test-1
ello2
This is because files are not regarded as lines, but just characters where the newline is not special.
So if N represents a newline charachter:
helloNhello2N
will be replaced by
test-1Nello2N
I hope this is clear.
Regards,
Jaap Zwaan
arunkw
25th June 2003, 09:22
Thank you all :) , For helping me out.
:o I had already tried out the options of using Unix commands of
cat, mv etc. to achieve my task. and it works very fine in unix.
But my stubborn customer did not accept the steps I had prescribed to him,
He blindly refused :rolleyes: saying that he doesn't want to execute any steps manually
because of security reasons, and lot of reasons such as taking up risk and responsibility.:confused:
That was the reason I had to develope a new program for him in baan.
I am keeping my fingures cross, till the time he exectues it.
You guys have helped me a lot.:)
Warm Regards
Arun
hardei
1st March 2013, 04:22
Hello Frnds,
I want to append a size of the file at the beginning of the file. I am getting file size from file.stat(), but appending it to the beginning of the file is becoming tough . I am using seq.seek(0,0,file.pointer) to point at file beginning, I am able to append size also, but there but some data is getting lost.
How to resolve it?
Thanks,
Hardei
Han Brinkman
1st March 2013, 14:09
Hardei,
You can't start writing simply at the beginning of the file. That will overwrite existing data.
IMHO you should write a new file with your value and add the content of the original file to it. After closing the file you can rename the new file to the old file.
Regards,
Han
hardei
2nd March 2013, 09:20
Dear Han,
I am trying to merge two files using cat. following is the syntax
ret = shell("cat",&file.1& ">"&file.2&,0)
But it is not working.
Hardei
mark_h
4th March 2013, 01:05
I find it easier to set up a command string so I can see it in debug mode before executing it (and don't forget the spaces):
cmd.str = "cat " & file.1 & " " & file.2 & " > " & file.3
ret=shell(cmd.str,0)
I will check how I do this in baan when I get to work tomorrow.