• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2716
  • Last Modified:

To write into a text file.

Through Powerbuilder 7 scripting
I want to insert this line
kse_code,rate_date,open_rate,high_rate,low_rate,close_rate,turnover
into a text file namely 12052006.txt lying in the folder d:\download at the very begining of the file.

After getting answer, I would ask two more (should be simple 4 expert) questions , because I hope the one who gives the answer would be able to answer those also.

0
Mateen
Asked:
Mateen
  • 4
  • 2
2 Solutions
 
berXpertCommented:
Hi Mateen,

You can use a Blob to store your data file, then replace the content of original file with your line + blob content.

Here's the code in PB 7. You can download the sample object from my web page

http://berxpert.googlepages.com/powerbuilder

//------------------------
// Code start here
//------------------------
integer li_FileNum, loops, i
long flen, bytes_read, new_pos
blob b, tot_b

String ls_file
ls_file = "d:\\download\\12052006.txt"

// Set a wait cursor
SetPointer(HourGlass!)

// Get the file length, and open the file
flen = FileLength(ls_file)
li_FileNum = FileOpen(ls_file, StreamMode!, Read!, LockRead!)

// Determine how many times to call FileRead
IF flen > 32765 THEN
            IF Mod(flen, 32765) = 0 THEN
                  loops = flen/32765
            ELSE
                  loops = (flen/32765) + 1
            END IF
ELSE
            loops = 1
END IF

// Read the file into a Blob
new_pos = 1

FOR i = 1 to loops
            bytes_read = FileRead(li_FileNum, b)
            tot_b = tot_b + b
NEXT

FileClose(li_FileNum)


// Now Blob TOT_B has all file in memory


// Open same file to write your new line
li_FileNum = FileOpen(ls_file, LineMode!, Write!, LockWrite!, Replace!)

FileWrite(li_FileNum, "kse_code,rate_date,open_rate,high_rate,low_rate,close_rate,turnover")
FileClose(li_FileNum)


// Now open same file to append previous content from Blob
li_FileNum = FileOpen(ls_file, StreamMode!, Write!, LockWrite!, Append!)

// Write Blob TOT_B to the file
new_pos = 1

FOR i = 1 to loops
            b = BlobMid(tot_b, new_pos, 32765 )
            new_pos += 32765
            FileWrite(li_FileNum, b )
NEXT

FileClose(li_FileNum)


//------- END

I hope it helps you

BerX
0
 
sandeep_patelCommented:
Hi,

Instead of this long method why don't you try FileSeek function. It allows you to move pointer to anywhere in file and then you can directly read or write at that position.

see the help of FileSeek function.

so above code will be

//------------------------
// Code start here
//------------------------
integer li_FileNum, loops, i
long flen, bytes_read, new_pos
blob b, tot_b

String ls_file
ls_file = "d:\\download\\12052006.txt"

// Set a wait cursor
SetPointer(HourGlass!)

// Open same file to write your new line
li_FileNum = FileOpen(ls_file, LineMode!, Write!, LockWrite!, Replace!)

FileSeek (li_fileNum, 0, FromBeginning! )

FileWrite(li_FileNum, "kse_code,rate_date,open_rate,high_rate,low_rate,close_rate,turnover")
FileClose(li_FileNum)

Regards,
Sandeep
0
 
MateenAuthor Commented:
Hi Sandeep

Your code added the line, but removed the rest of the contents.

Hi berXpert

Your code worked.


Hi both
<<After getting answer, I would ask two more (should be simple 4 expert) questions , because I hope the one who gives the answer would be able to answer those also.>>

I thought it would be a matter of a few lines, but now I feel that the matters are not so simple. Hence I decided to use another thread for my next wish. The question would contain the link of this page.

After asking question, I would paste the linke here.

I hope , I am not violating EE rule.



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sandeep_patelCommented:
Hi,

sorry in the sample code i have given in that i opened the file in 'Replace' mode to write just change that with 'Append' it will work.....

li_FileNum = FileOpen(ls_file, LineMode!, Write!, LockWrite!, Append!)

Regards,
Sandeep
0
 
MateenAuthor Commented:
Hi sandeep,

Absolutely ok now.

You may please have a look at

http://www.experts-exchange.com/Programming/Programming_Languages/PowerBuilder/Q_21849140.html

Thanks and regards
0
 
MateenAuthor Commented:
Hi sandeep
There is a small problem in the code.

integer li_FileNum, loops, i
long flen, bytes_read, new_pos
blob b, tot_b

String ls_file
ls_file = "d:\\download\\12052006.txt"

// Set a wait cursor
SetPointer(HourGlass!)

// Open same file to write your new line
li_FileNum = FileOpen(ls_file, LineMode!, Write!, LockWrite!, Append!)
FileSeek (li_fileNum, 0, FromBeginning! )
FileWrite(li_FileNum, "kse_code,rate_date,open_rate,high_rate,low_rate,close_rate,turnover")
FileClose(li_FileNum)

Result:
Line is being added at the top,
But it is also deleting half of the line from second line.

When I first ran, it was ok.

Could u please figure out , what I am missing.

Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now