Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

How To Handle FileWrite's Trailing CRLFs?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-02-2012, 01:26 PM
Junior Member
 
Join Date: Mar 2010
Posts: 21
Default How To Handle FileWrite's Trailing CRLFs?

Probably, it is normal (from FileWrite's point of view) to append yet another crlf to the end of a string each time it saves it. A simple test demonstrates such behaviour (open "some.txt" in NotePad++, play Bonham's 'Moby Dick' on "Open" and "Save" buttons and see how the trail of crlfs grows):

Code:
Sub cmdOpen_Click
    ErrorLabel(errFO)
FileOpen(c1,
"some.txt",cRead)
a = FileReadToEnd(c1)
FileClose(c1)
Return
    errFO:
    
Msgbox("Can't open it, dude!")
End Sub

Sub cmdSave_Click
    ErrorLabel(errFS)
FileOpen(c1,
"some.txt",cWrite)
FileWrite(c1,a)
FileClose(c1)
Return
    errFS:
    
Msgbox("Can't save it, dude!")
End Sub
If my code is wrong, what's the right code? Else, what's the most elegant (resource-saving) technique to trim crlf (during or after each save)?

Insofar, my workaround is as follows:
Code:
If StrAt(a,StrLength(a)-2)=Chr(13Then a=SubString(a,0,StrLength(a)-2)
This trims a string read from some.txt each time it is FileOpened. As a result, we always have only one extra crlf at the end of some.txt (no matter how many times the save button was fired).

Any other solutions (didn't find any on the forum yet)?

Last edited by Sir Isaac : 02-02-2012 at 01:33 PM.
Reply With Quote
  #2 (permalink)  
Old 02-02-2012, 02:29 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,732
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You are correct. FileWrite writes a string and adds end of line characters.
Your solution is a good solution. You can also use BinaryFile library and convert the string to bytes and write it with WriteBytes.
Reply With Quote
  #3 (permalink)  
Old 02-02-2012, 02:50 PM
Basic4ppc Expert
 
Join Date: May 2008
Location: Berkshire, UK
Posts: 810
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Quote:
Originally Posted by Sir Isaac View Post
Any other solutions (didn't find any on the forum yet)?
I think that the high-level answer is that FileWrite is a better match to FileRead because the latter just gives you the line content without new-line. So writing the same string with FileWrite puts back the new-line.

FileReadTo End does not match FileWrite so closely as you have discovered.

Mike.
Reply With Quote
  #4 (permalink)  
Old 02-02-2012, 04:33 PM
Junior Member
 
Join Date: Mar 2010
Posts: 21
Default Saving with WriteBytes

Thanks, Erel, mjcoon,

WriteBytes's an interesting option. I believe it should work faster (shouldn't it?), and indeed, it doesn't add crlf:
Code:
Sub Globals
    
Dim buffer(0As Byte
    a=
""
End Sub

Sub cmdOpen_Click
    ErrorLabel(errFO)
FileOpen(c1,
"some.txt",cRead)
a = FileReadToEnd(c1)
FileClose(c1)
    
Return
    errFO:
    
Msgbox("Can't open it, dude!")
End Sub

Sub cmdSaveByte_Click
    ErrorLabel(errFSB)
FileOpen(c1,
"some.txt",cRandom)
bin.New1(c1,
False)
buffer()=bin.StringToBytes(a)
bin.WriteBytes(buffer())
FileClose(c1)
    
Return
    errFSB:
    
Msgbox("Can't save it, dude!")
End Sub
And finally, is it okay to combine a text method (FileReadToEnd) for opening file with a binary method (WriteBytes) for saving it? In particular, wouldn't the dynamic array of bytes (buffer()) create a memory overhead when saving a significant amount of text (as it doubles the same data, I suspect)?

Last edited by Sir Isaac : 02-02-2012 at 06:15 PM.
Reply With Quote
  #5 (permalink)  
Old 02-03-2012, 08:29 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,732
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

I don't think that it will be faster. It will probably run at the same speed.
It does create an extra array. However if the text file is not huge then it will not really matter.

As long as you only write the string converted to bytes then you can safely read it with FileReadToEnd.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can B4A handle embedded URLs or hyperlinks? JohnInPA Basic4android Updates and Questions 3 11-17-2011 10:02 PM
How to get the System active window handle ? eiaitalia Questions (Windows Mobile) 0 02-12-2011 03:18 PM
How would you handle the downloading of large amount of data? moster67 Questions (Windows Mobile) 2 04-16-2009 06:27 PM
SaveCSV doesn't handle null values RB Smissaert Questions (Windows Mobile) 6 01-09-2009 02:49 PM
handle program with voice Georg Questions (Windows Mobile) 1 04-11-2008 04:42 AM


All times are GMT. The time now is 06:21 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0