Read And Write File In PowerBuilder
Source Code
n_func_file from nonvisualobject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | forward global type n_func_file from nonvisualobject end type end forward global type n_func_file from nonvisualobject autoinstantiate end type forward prototypes public function integer readfile (string as_file, ref blob rblob_file) public function integer readtext (string as_file, ref string rs_text) public function integer readlines (string as_file, ref string rs_line[]) public function integer writefile (string as_file, blob ablob_file) public function integer writetext (string as_file, string as_text) public function integer appendtext (string as_file, string as_text) public function integer appendfile (string as_file, blob ablob_file) end prototypes public function integer readfile (string as_file, ref blob rblob_file);//==================================================================== // Function: n_func_file.readfile() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_file // reference blob rblob_file //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/02/23 //-------------------------------------------------------------------- // Usage: n_func_file.readfile ( string as_file, ref blob rblob_file ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Long ll_len Long ll_loop,i Int li_filenum,li_read Blob lblob_read,lblob_file ll_len = FileLength(as_file) ll_loop = ll_len / 32765 If Mod(ll_len,32765) <> 0 Then ll_loop += 1 li_filenum = FileOpen(as_file,streammode!,Read!,lockread!) If li_filenum < 0 Then Return li_filenum For i = 1 To ll_loop li_read = FileRead(li_filenum,lblob_read) If li_read < 0 Then Return li_read lblob_file += lblob_read Next FileClose(li_filenum) rblob_file = lblob_file Return 0 end function public function integer readtext (string as_file, ref string rs_text);//==================================================================== // Function: n_func_file.readtext() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_file // reference string rs_text //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/02/23 //-------------------------------------------------------------------- // Usage: n_func_file.readtext ( string as_file, ref string rs_text ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Long ll_len Long ll_loop,i Int li_filenum,li_read String ls_read,ls_file ll_len = FileLength(as_file) ll_loop = ll_len / 32765 If Mod(ll_len,32765) <> 0 Then ll_loop += 1 li_filenum = FileOpen(as_file,streammode!,Read!,lockread!) If li_filenum < 0 Then Return li_filenum For i = 1 To ll_loop li_read = FileRead(li_filenum,ls_read) If li_read < 0 Then Return li_read ls_file += ls_read Next FileClose(li_filenum) rs_text = ls_file Return 0 end function public function integer readlines (string as_file, ref string rs_line[]);//==================================================================== // Function: n_func_file.readlines() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_file // reference string rs_line[] //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/02/23 //-------------------------------------------------------------------- // Usage: n_func_file.readlines ( string as_file, ref string rs_line[] ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Int li_filenum,li_read String ls_read,ls_line[] Long i li_filenum = FileOpen(as_file,LineMode!,Read!,LockRead!) If li_filenum < 0 Then Return li_filenum Do While FileRead(li_filenum,ls_read) >= 0 i += 1 ls_line[i] = ls_read Loop FileClose(li_filenum) rs_line = ls_line Return 0 end function public function integer writefile (string as_file, blob ablob_file);//==================================================================== // Function: n_func_file.writefile() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_file // value blob ablob_file //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/02/23 //-------------------------------------------------------------------- // Usage: n_func_file.writefile ( string as_file, blob ablob_file ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Long ll_len Long ll_loop,i Int li_filenum,li_write Blob lblob_write ll_len = Len(ablob_file) ll_loop = ll_len / 32765 If Mod(ll_len,32765) <> 0 Then ll_loop += 1 li_filenum = FileOpen(as_file,StreamMode!,Write!,LockWrite!,Replace!) If li_filenum < 0 Then Return li_filenum For i = 1 To ll_loop lblob_write = BlobMid(ablob_file,(i - 1)*32765 + 1,32765) li_write = FileWrite(li_filenum,lblob_write) If li_write < 0 Then Return li_write Next FileClose(li_filenum) Return 0 end function public function integer writetext (string as_file, string as_text);//==================================================================== // Function: n_func_file.writetext() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_file // value string as_text //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/02/23 //-------------------------------------------------------------------- // Usage: n_func_file.writetext ( string as_file, string as_text ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Long ll_len Long ll_loop,i Int li_filenum,li_write String ls_write ll_len = Len(as_text) ll_loop = ll_len / 16382 If Mod(ll_len,16382) <> 0 Then ll_loop += 1 li_filenum = FileOpen(as_file,StreamMode!,Write!,LockWrite!,Replace!) If li_filenum < 0 Then Return li_filenum For i = 1 To ll_loop ls_write = Mid(as_text,(i - 1)*16382 + 1,16382) li_write = FileWrite(li_filenum,ls_write) If li_write < 0 Then Return li_write Next FileClose(li_filenum) Return 0 end function public function integer appendtext (string as_file, string as_text);//==================================================================== // Function: n_func_file.appendtext() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_file // value string as_text //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/02/23 //-------------------------------------------------------------------- // Usage: n_func_file.appendtext ( string as_file, string as_text ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Long ll_len Long ll_loop,i Int li_filenum,li_write String ls_write ll_len = Len(as_text) ll_loop = ll_len / 16382 If Mod(ll_len,16382) <> 0 Then ll_loop += 1 li_filenum = FileOpen(as_file,StreamMode!,Write!,LockWrite!,Append!) If li_filenum < 0 Then Return li_filenum For i = 1 To ll_loop ls_write = Mid(as_text,(i - 1)*16382 + 1,16382) li_write = FileWrite(li_filenum,ls_write) If li_write < 0 Then Return li_write Next FileClose(li_filenum) Return 0 end function public function integer appendfile (string as_file, blob ablob_file);//==================================================================== // Function: n_func_file.appendfile() //-------------------------------------------------------------------- // Description: //-------------------------------------------------------------------- // Arguments: // value string as_file // value blob ablob_file //-------------------------------------------------------------------- // Returns: integer //-------------------------------------------------------------------- // Author: PB.BaoGa Date: 2022/02/23 //-------------------------------------------------------------------- // Usage: n_func_file.appendfile ( string as_file, blob ablob_file ) //-------------------------------------------------------------------- // Copyright (c) PB.BaoGa(TM), All rights reserved. //-------------------------------------------------------------------- // Modify History: // //==================================================================== Long ll_len Long ll_loop,i Int li_filenum,li_write Blob lblob_write ll_len = Len(ablob_file) ll_loop = ll_len / 32765 If Mod(ll_len,32765) <> 0 Then ll_loop += 1 li_filenum = FileOpen(as_file,StreamMode!,Write!,LockWrite!,Append!) If li_filenum < 0 Then Return li_filenum For i = 1 To ll_loop lblob_write = BlobMid(ablob_file,(i - 1)*32765 + 1,32765) li_write = FileWrite(li_filenum,lblob_write) If li_write < 0 Then Return li_write Next FileClose(li_filenum) Return 0 end function on n_func_file.create call super::create TriggerEvent( this, "constructor" ) end on on n_func_file.destroy TriggerEvent( this, "destructor" ) call super::destroy end on |
Example
w_main from window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | forward global type w_main from window end type type mle_1 from multilineedit within w_main end type type lb_1 from listbox within w_main end type type cb_8 from commandbutton within w_main end type type cb_7 from commandbutton within w_main end type type cb_6 from commandbutton within w_main end type type cb_5 from commandbutton within w_main end type type cb_4 from commandbutton within w_main end type type cb_3 from commandbutton within w_main end type type cb_2 from commandbutton within w_main end type type cb_1 from commandbutton within w_main end type type sle_1 from singlelineedit within w_main end type type st_1 from statictext within w_main end type end forward global type w_main from window integer width = 2254 integer height = 1412 boolean titlebar = true string title = "Read And Write File Text" boolean controlmenu = true boolean minbox = true boolean maxbox = true boolean resizable = true long backcolor = 67108864 string icon = "AppIcon!" boolean center = true mle_1 mle_1 lb_1 lb_1 cb_8 cb_8 cb_7 cb_7 cb_6 cb_6 cb_5 cb_5 cb_4 cb_4 cb_3 cb_3 cb_2 cb_2 cb_1 cb_1 sle_1 sle_1 st_1 st_1 end type global w_main w_main on w_main.create this.mle_1=create mle_1 this.lb_1=create lb_1 this.cb_8=create cb_8 this.cb_7=create cb_7 this.cb_6=create cb_6 this.cb_5=create cb_5 this.cb_4=create cb_4 this.cb_3=create cb_3 this.cb_2=create cb_2 this.cb_1=create cb_1 this.sle_1=create sle_1 this.st_1=create st_1 this.Control[]={this.mle_1,& this.lb_1,& this.cb_8,& this.cb_7,& this.cb_6,& this.cb_5,& this.cb_4,& this.cb_3,& this.cb_2,& this.cb_1,& this.sle_1,& this.st_1} end on on w_main.destroy destroy(this.mle_1) destroy(this.lb_1) destroy(this.cb_8) destroy(this.cb_7) destroy(this.cb_6) destroy(this.cb_5) destroy(this.cb_4) destroy(this.cb_3) destroy(this.cb_2) destroy(this.cb_1) destroy(this.sle_1) destroy(this.st_1) end on type mle_1 from multilineedit within w_main integer x = 1120 integer y = 620 integer width = 910 integer height = 644 integer taborder = 60 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 boolean vscrollbar = true boolean autohscroll = true boolean autovscroll = true borderstyle borderstyle = stylelowered! end type type lb_1 from listbox within w_main integer x = 69 integer y = 620 integer width = 910 integer height = 644 integer taborder = 50 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 boolean vscrollbar = true borderstyle borderstyle = stylelowered! end type type cb_8 from commandbutton within w_main integer x = 1367 integer y = 340 integer width = 672 integer height = 112 integer taborder = 40 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Write Append Blob Text" end type event clicked;n_func_file ln_file String ls_text String ls_file Int li_ret Blob lblb_text ls_file = sle_1.Text ls_text = mle_1.Text If IsNull(ls_file) Or Len(Trim(ls_file)) = 0 Then Return If IsNull(ls_text) Or Len(Trim(ls_text)) = 0 Then Return lblb_text = blob(ls_text, EncodingUTF8!) li_ret = ln_file.appendfile( ls_file, lblb_text) If li_ret <> 0 Then MessageBox("Warning", "Write File Error") Return End If end event type cb_7 from commandbutton within w_main integer x = 1367 integer y = 212 integer width = 672 integer height = 112 integer taborder = 30 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Write Blob Text" end type event clicked;n_func_file ln_file String ls_text String ls_file Int li_ret Blob lblb_text ls_file = sle_1.Text ls_text = mle_1.Text If IsNull(ls_file) Or Len(Trim(ls_file)) = 0 Then Return If IsNull(ls_text) Or Len(Trim(ls_text)) = 0 Then Return lblb_text = blob(ls_text, EncodingUTF8!) li_ret = ln_file.writefile( ls_file, lblb_text) If li_ret <> 0 Then MessageBox("Warning", "Write File Error") Return End If end event type cb_6 from commandbutton within w_main integer x = 640 integer y = 340 integer width = 672 integer height = 112 integer taborder = 30 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Write Append Text" end type event clicked;n_func_file ln_file String ls_text String ls_file Int li_ret ls_file = sle_1.Text ls_text = mle_1.Text If IsNull(ls_file) Or Len(Trim(ls_file)) = 0 Then Return If IsNull(ls_text) Or Len(Trim(ls_text)) = 0 Then Return li_ret = ln_file.appendtext(ls_file, ls_text) If li_ret <> 0 Then MessageBox("Warning", "Write File Error") Return End If end event type cb_5 from commandbutton within w_main integer x = 645 integer y = 212 integer width = 672 integer height = 112 integer taborder = 30 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Write Text" end type event clicked;n_func_file ln_file String ls_text String ls_file Int li_ret ls_file = sle_1.Text ls_text = mle_1.Text If IsNull(ls_file) Or Len(Trim(ls_file)) = 0 Then Return If IsNull(ls_text) Or Len(Trim(ls_text)) = 0 Then Return li_ret = ln_file.writetext( ls_file, ls_text) If li_ret <> 0 Then MessageBox("Warning", "Write File Error") Return End If end event type cb_4 from commandbutton within w_main integer x = 142 integer y = 468 integer width = 448 integer height = 112 integer taborder = 40 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Read Blob Text" end type event clicked;n_func_file ln_file String ls_text String ls_file Int li_ret, li_i Blob lblb_text mle_1.Text = "" ls_file = sle_1.Text If IsNull(ls_file) Or Len(Trim(ls_file)) = 0 Then Return li_ret = ln_file.readfile( ls_file, lblb_text) If li_ret <> 0 Then Return ls_text = String(lblb_text, EncodingUTF8!) mle_1.Text = ls_text end event type cb_3 from commandbutton within w_main integer x = 1989 integer y = 40 integer width = 146 integer height = 112 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "...." end type event clicked;string ls_path, ls_file int li_rc ls_path = sle_1.Text li_rc = GetFileSaveName ( "Select File", ls_path, ls_file, "*.*", "All Files (*.*),*.*" ) IF li_rc = 1 Then sle_1.Text = ls_path End If end event type cb_2 from commandbutton within w_main integer x = 137 integer y = 336 integer width = 448 integer height = 112 integer taborder = 30 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Read Text" end type event clicked;n_func_file ln_file String ls_text String ls_file Int li_ret, li_i mle_1.Text = "" ls_file = sle_1.Text If IsNull(ls_file) Or Len(Trim(ls_file)) = 0 Then Return li_ret = ln_file.readtext( ls_file, ls_text) If li_ret <> 0 Then Return mle_1.Text = ls_text end event type cb_1 from commandbutton within w_main integer x = 142 integer y = 212 integer width = 448 integer height = 112 integer taborder = 20 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" string text = "Read Lines" end type event clicked;n_func_file ln_file String ls_text[] String ls_file Int li_ret, li_i lb_1.reset() ls_file = sle_1.Text If IsNull(ls_file) Or Len(Trim(ls_file)) = 0 Then Return li_ret = ln_file.readlines( ls_file, ls_text) If li_ret <> 0 Then Return For li_i = 1 To UpperBound(ls_text) lb_1.additem( ls_text[li_i]) Next end event type sle_1 from singlelineedit within w_main integer x = 238 integer y = 44 integer width = 1742 integer height = 104 integer taborder = 10 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 string text = "pbfunc.txt" borderstyle borderstyle = stylelowered! end type type st_1 from statictext within w_main integer x = 69 integer y = 64 integer width = 169 integer height = 64 integer textsize = -10 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 long backcolor = 67108864 string text = "File:" boolean focusrectangle = false end type |
Find Projects On Github click here
Good Luck!