Results 1 to 31 of 31

Thread: [RESOLVED] How can read unicode file line 2? and how write to unicode file line 2?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Resolved [RESOLVED] How can read unicode file line 2? and how write to unicode file line 2?

    hi
    how can read and show a binary text file first line?
    and how can write to binary text file first line?(The way This is the first line of the file get to line Two)?
    Thanks

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can read binary text file first line? and how write to binary text first line

    Quote Originally Posted by r2du-soft View Post
    (The way This is the first line of the file get to line Two)?
    Thanks
    What does this mean


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How can read binary text file first line? and how write to binary text first line

    I'm still stumped by "binary text file" - which is it, binary or text?

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can read binary text file first line? and how write to binary text first line

    Me too but I was giving OP benefit of the doubt.

    I got the impression that it's a text file and the first line, what ever it is, he wants to read in as binary and show it in binary format (like a program dump) but he can just read it in as text into a string then bump through the string converting each byte to a hex string. I guess that's what he wants


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Question Re: How can read binary text file first line? and how write to binary text first line

    Quote Originally Posted by jmsrickland View Post
    What does this mean
    Quote Originally Posted by dilettante View Post
    I'm still stumped by "binary text file" - which is it, binary or text?

    hi guys
    i'm sorry,i should Give One example:

    think my binary text file like this:

    HTML Code:
    ; for 16-bit app support
    [fonts]
    [extensions]
    Kill file returned: :3:
    No Previous IE40, not an IE40 install.
    Add preclean, crypto and maybe DCOM
    OS is NT
    Installing OLEAUT.exe.
    Architecture is Intel.
    dispci.dll:  ============BEGIN DisplayClassInstaller============
    dispci.dll:  ============BEGIN OnSelectBestCompatDrv============
    dispci.dll:  ============END OnSelectBestCompatDrv==============
    dispci.dll:  DisplayClassInstaller: Returning 0xe000020e for DIF 0x17 and device VMBUS\{DA0A7802-E377-4AAC-8E77-0558EB1073F8}\{5620E0C7-8062-4DCE-AEB7-520C7EF76171}
    .
    .
    .

    1-Now i how can read first line and show this with msgbox?!? (Show first line: ; for 16-bit app support )
    2- i how can write in this binary file ? (write to first line)?
    (The way This is the first line of the file get to line Two) : example this:

    HTML Code:
    NOW I WRITE TO FIRST LINE!
    ; for 16-bit app support
    [fonts]
    [extensions]
    Kill file returned: :3:
    No Previous IE40, not an IE40 install.
    Add preclean, crypto and maybe DCOM
    OS is NT
    Installing OLEAUT.exe.
    Architecture is Intel.
    dispci.dll:  ============BEGIN DisplayClassInstaller============
    dispci.dll:  ============BEGIN OnSelectBestCompatDrv============
    dispci.dll:  ============END OnSelectBestCompatDrv==============
    dispci.dll:  DisplayClassInstaller: Returning 0xe000020e for DIF 0x17 and device VMBUS\{DA0A7802-E377-4AAC-8E77-0558EB1073F8}\{5620E0C7-8062-4DCE-AEB7-520C7EF76171}
    .
    .
    .

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How can read binary text file first line? and how write to binary text first line

    The point above was that either it is Binary or it is text. It can't be both

    For a text file you would normally use one of the text file methods like Line Input # for example

    For a Binary file then you would use a binary method like Get #

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can read binary text file first line? and how write to binary text first line

    As stated in the Declaration of Independence "all files are created equal..." which means all files are binary in their presence on the disk drive which means all file contain binary data. The fact that how you want to deal with this file is how you read the data. Either you read the data as text or you read it as binary.
    Last edited by jmsrickland; Jul 23rd, 2014 at 12:51 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Re: How can read binary text file first line? and how write to binary text first line

    thanks
    when i write new line in first line i see my file like this:

    Before Write:
    Name:  BeforeWrite.jpg
Views: 646
Size:  8.8 KB

    After Write:
    Name:  AfterWrite.jpg
Views: 599
Size:  4.1 KB


    Code:
    Open "c:\test.txt" For Binary As #1
    Put #1, , vbNewLine
    Close #1

    i see mt file format agait, i see format is unicode
    Attached Images Attached Images   
    Last edited by r2du-soft; Jul 22nd, 2014 at 03:38 PM.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Re: How can read binary text file first line? and how write to binary text first line

    guys i really need your help...

  10. #10
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: How can read binary text file first line? and how write to binary text first line

    Read a file:

    Code:
    Public Function loadFile(strPath as String) as String
    Dim ff As Long
    Dim line As String
    Dim tempSTR As String
    
    ff = FreeFile
    
    Open (strPath) For Input As #ff
    tempSTR = ""
    Do While Not EOF(ff)
           Line Input #ff, line
              tempSTR = tempSTR & line
           Loop
    Close #ff
    
    loadFile = tempSTR
    End Function
    
    'Usage:
    'Msgbox ( loadFile("C:\file.txt") )
    Write to file:
    Code:
    Public Sub writeFile(strPATH as String, strText as String)
    Dim ff As Long
    
    ff = FreeFile
    
    Open (strPATH)  For Output As #ff
        Print #ff, strText
    Close #ff
    
    End Sub
    
    'Usage: 
    'Call writeFile("C:\file.txt","Hello World")
    Copy-Paste to your code and change the Usage parts.
    Last edited by stum; Jul 23rd, 2014 at 10:41 AM.

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How can read binary text file first line? and how write to binary text first line

    Quote Originally Posted by jmsrickland View Post
    As stated in the Declaration of Independence "all files are created equal..." which means all files are binary in their presence on the disk drive. The fact that how you want to deal with this file is how you read the data. Either you read the data as text or you read it as binary.
    As it turns out... not so much.


    If a file contains "text" then that comes along with a whole host of issues specific to text.

    One of these is what line terminators are used. Typically these are CRLF pairs for a Microsoft (DOS or Windows) file, but bare LF is also very common and in rare cases bare CR can be used. Anything else is rare but also possible.

    The VB Line Input # will process either CRLF or CR properly, but not LF. VB's Print # always writes out CRLFs.

    Then you have the question of character encoding. We have Unicode in many flavors, and while UTF-16LE is standard for Windows you see a lot of UTF-8 and on occasion UTF-16BE. VB native I/O statements don't handle any of these, though the FSO's TextStream.ReadLine/.WriteLine will work with UTF-16LE.

    If you have ANSI, then there is the question of what codepage is used. VB native text I/O statements only work with ANSI using the current (user or system) codepage. FSO's TextStream methods can be used with these too. But if the file was written with another codepage you may see garbage when you read it back.

    And then you have the question of Ctrl-Z as "EOF." This is a DOS holdover you still see sometimes. VB's Line Input # will stop at a Ctrl-Z and signal EOF. The FSO will treat it as a text character.

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How can read binary text file first line? and how write to binary text first line

    We still don't really know what this thread is asking for.

    There isn't any practical way to insert lines of text at the beginning or middle of a file. You can overwrite existing data but there is nothing to magically "push down" existing data and make room for new data where it used to sit.

    If the file is Unicode text then you can't use Line Input # because that always treats the data as local ANSI and will try to convert each byte to a Unicode character. This is where the "doubling" comes from: each Unicode character gets treated as 2 characters because UTF-16LE is 2 bytes per character.

  13. #13
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How can read binary text file first line? and how write to binary text first line

    Quote Originally Posted by dilettante View Post
    If a file contains "text" then that comes along with a whole host of issues specific to text.
    .
    Are you referring to the binary data in the file that will look like text characters once processed correctly in the program...
    Last edited by jmsrickland; Jul 23rd, 2014 at 01:07 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Question Re: How can read binary text file first line? and how write to binary text first line

    thanks my friends for Description
    But Also my problem is not solved
    codes not work!
    Actually This code is different from The code that I need
    this is my test file: test.zip
    how can write to line 2?
    how can read line 2 and show that?
    Last edited by r2du-soft; Jul 23rd, 2014 at 02:25 PM.

  15. #15
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How can read binary text file first line? and how write to binary text first line

    What you have there is not a "binary file" by any stretch of the imagination. Instead this is clearly a UTF-16LE file with a BOM at the beginning using CRLF line delimiters.

    See the classic The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)


    An easy tool for fiddling with files of varying character encodings in VB6 in the ADO Stream object. This is fully documented in the MSDN Library documentation CDs that come with every legal copy of VB6. Look for the ADO Programmer's Guide. This technique works great except for very large files, where you'd want to spend more effort to avoid keeping all of the data in memory.


    It is still far from clear exactly what you are trying to accomplish here.

    This demo will display the file's "Line 2" (sort of boring since it is an empty line in your original file), then let you type a new Line 2 to be inserted above the original Line 2. Then after you insert the line try "opening" the file again and your new Line 2 will be displayed, allowing you to enter yet another new Line 2 to be inserted above that.

    You could easily modify this to replace the original Line 2 instead of inserting above it.
    Attached Files Attached Files

  16. #16
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: How can read binary text file first line? and how write to binary text first line

    readLine function returns a String containing the line number specified, in a file.
    writeLine sub, replaces a line within a file.

    Code:
    Private Sub Form_Load()
    'Read line:
    'MsgBox readLine("c:\test.txt", 2)
    
    'Write to line:
    'Call writeLine("C:\test.txt", 2, "hello world")
    End Sub
    
    Public Function readLine(strFilePath As String, lngLineNumber As Long) As String
    
    'if File not Found
    If Dir(strFilePath) = "" Then readLine = "File not Found": Exit Function
    
    'If File Found:
    Dim FileNo As Integer
        FileNo = FreeFile
    
    Dim tempStr As String
        
    Open strFilePath For Input As FileNo
    
        Dim i As Long
        For i = 1 To lngLineNumber
            Line Input #FileNo, tempStr
        Next i
    
    Close #FileNo
    
    readLine = tempStr
       
    End Function
    
    
    Public Sub writeLine(strFilePath As String, lngLineNumber As Long, strLineText As String)
    
    If Dir(strFilePath) = "" Then Exit Sub
    
    'If File Found:
    Dim FileNo As Integer
        FileNo = FreeFile
    
    Dim tempLine As Integer
        tempLine = 1
        
    Dim tempStr As String
    Dim tempData As String
       
       'read the Whole file and place data into tempData
       Open strFilePath For Input As FileNo
          Do
          
            Line Input #FileNo, tempStr
            
            If tempLine = lngLineNumber Then
                If tempLine = 1 Then tempData = strLineText Else tempData = tempData & vbCrLf & strLineText
            Else
                If tempLine = 1 Then tempData = tempStr Else tempData = tempData & vbCrLf & tempStr
            End If
            
            tempLine = tempLine + 1
            
          Loop Until EOF(FileNo)
        Close #FileNo
       
    'Write to the file
        FileNo = FreeFile
        Open strFilePath For Output As FileNo
    
            Print #FileNo, tempData
    
        Close #FileNo
    
    End Sub
    Copy-Paste to your code.

    Read line:
    'MsgBox readLine("c:\test.txt", 2)
    Write to line:
    'Call writeLine("C:\test.txt", 2, "hello world")
    Last edited by stum; Jul 24th, 2014 at 03:13 AM.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Re: How can read binary text file first line? and how write to binary text first line

    Quote Originally Posted by dilettante View Post
    What you have there is not a "binary file" by any stretch of the imagination. Instead this is clearly a UTF-16LE file with a BOM at the beginning using CRLF line delimiters.

    See the classic The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)


    An easy tool for fiddling with files of varying character encodings in VB6 in the ADO Stream object. This is fully documented in the MSDN Library documentation CDs that come with every legal copy of VB6. Look for the ADO Programmer's Guide. This technique works great except for very large files, where you'd want to spend more effort to avoid keeping all of the data in memory.


    It is still far from clear exactly what you are trying to accomplish here.

    This demo will display the file's "Line 2" (sort of boring since it is an empty line in your original file), then let you type a new Line 2 to be inserted above the original Line 2. Then after you insert the line try "opening" the file again and your new Line 2 will be displayed, allowing you to enter yet another new Line 2 to be inserted above that.

    You could easily modify this to replace the original Line 2 instead of inserting above it.
    Thanks dilettante
    Code Good Work,thanks
    i have a question: when i add a ocx in references,is possible not found in some windows?!and program error?!

    Quote Originally Posted by stum View Post
    readLine function returns a String containing the line number specified, in a file.
    writeLine sub, replaces a line within a file.

    Code:
    Private Sub Form_Load()
    'Read line:
    'MsgBox readLine("c:\test.txt", 2)
    
    'Write to line:
    'Call writeLine("C:\test.txt", 2, "hello world")
    End Sub
    
    Public Function readLine(strFilePath As String, lngLineNumber As Long) As String
    
    'if File not Found
    If Dir(strFilePath) = "" Then readLine = "File not Found": Exit Function
    
    'If File Found:
    Dim FileNo As Integer
        FileNo = FreeFile
    
    Dim tempStr As String
        
    Open strFilePath For Input As FileNo
    
        Dim i As Long
        For i = 1 To lngLineNumber
            Line Input #FileNo, tempStr
        Next i
    
    Close #FileNo
    
    readLine = tempStr
       
    End Function
    
    
    Public Sub writeLine(strFilePath As String, lngLineNumber As Long, strLineText As String)
    
    If Dir(strFilePath) = "" Then Exit Sub
    
    'If File Found:
    Dim FileNo As Integer
        FileNo = FreeFile
    
    Dim tempLine As Integer
        tempLine = 1
        
    Dim tempStr As String
    Dim tempData As String
       
       'read the Whole file and place data into tempData
       Open strFilePath For Input As FileNo
          Do
          
            Line Input #FileNo, tempStr
            
            If tempLine = lngLineNumber Then
                If tempLine = 1 Then tempData = strLineText Else tempData = tempData & vbCrLf & strLineText
            Else
                If tempLine = 1 Then tempData = tempStr Else tempData = tempData & vbCrLf & tempStr
            End If
            
            tempLine = tempLine + 1
            
          Loop Until EOF(FileNo)
        Close #FileNo
       
    'Write to the file
        FileNo = FreeFile
        Open strFilePath For Output As FileNo
    
            Print #FileNo, tempData
    
        Close #FileNo
    
    End Sub
    Copy-Paste to your code.

    Read line:
    'MsgBox readLine("c:\test.txt", 2)
    Write to line:
    'Call writeLine("C:\test.txt", 2, "hello world")
    Thanks stum
    read and write good work! but write have a problem in registry exports files!!!!
    when i write with : Call writeLine("C:\reg.reg", 2, "hello world") then when i open manually test.txt see this!!!!


    HTML Code:
    楗摮睯⁳敒楧瑳祲䔠楤潴⁲敖獲潩⸵〰਍敨汬潷汲൤嬊䭈奅䱟䍏䱁䵟䍁䥈䕎卜䙏坔剁居桓敥嵤਍㵀∢਍਍䡛䕋彙佌䅃彌䅍䡃义居体呆䅗䕒卜敨摥䅜瑮噩物獵൝∊畡潴煟慵慲瑮湩≥搽潷摲〺〰〰〰റ∊汏偤瑡档畎扭牥㴢睤牯㩤〰〰〰〰਍倢瑡档畎扭牥㴢睤牯㩤〰〰〰㔶਍搢湯彴桳睯慟瑵牯湵扟潬正㴢睤牯㩤〰〰〰〰਍搢湯彴桳睯慟彶湩敦瑣潩≮搽潷摲〺〰〰〰ര∊潤瑮獟潨彷楷彮湩敦瑣潩≮搽潷摲〺〰〰〰ര∊畱损畯瑮牥㴢睤牯㩤〰〰〰戲਍椢汤彥捳湡楮杮敟慮汢摥㴢睤牯㩤〰〰〰㄰਍猢牵湡扟瑡整祲江≵搽潷摲〺〰〰〰റ∊獵灢潲整瑣潩彮湥扡敬≤搽潷摲〺〰〰〰റ∊楨≤搽潷摲昺ぢ㐵戴റ∊牦敥癡㴢睤牯㩤〰〰〰〰਍渢獯扵捳楲瑰潩≮搽潷摲〺〰〰〰ര∊潬彷獵条彥潭敤㴢睤牯㩤〰〰〰〰਍挢敨正獟杩≮搽潷摲〺〰〰〰റ∊業彮慲≭搽潷摲〺〰㈰ㄷര∊牰瑯捥楴湯慟橤獵≴搽潷摲〺〰〰〰ഴ∊瑳牡畴彰潭彮湥扡敬≤搽潷摲〺〰〰〰റ∊汷敟慮汢摥㴢睤牯㩤〰〰〰㄰਍猢汦敟慮汢摥㴢睤牯㩤〰〰〰㄰਍猢牵湡江彣湥扡敬≤搽潷摲〺〰〰〰റ∊畳慲彮浭敟慮汢摥㴢睤牯㩤〰〰〰㄰਍搢湯彴桳睯獟汰獡≨搽潷摲〺〰〰〰റ∊瑳楲瑣损敬湡湩≧搽潷摲〺〰〰〰ര∊畧牡彤湥扡敬≤搽潷摲〺〰〰〰റ∊瑓牡畴偰慨敳㴢睤牯㩤〰〰㌰㝥਍攢敭杲湥祣浟摯≥搽潷摲〺〰〰〰ര∊牥桟獩潴祲㴢睤牯㩤〰〰〰〰਍唢敳乲浡≥∽噁ㄱ㥁㘴㐴ഢ∊慌瑳慆汩牵健慨敳㴢睤牯㩤〰〰㌰㝥਍倢獡睳牯≤栽硥〺ⱥ〰㐬ⰳ〰昬ⰵ〰㘬ⰴ〰ㄬⱤ〰㈬ⰸ〰㤬Ᵽ〰戬ⰵ〰਍਍䡛䕋彙佌䅃彌䅍䡃义居体呆䅗䕒卜敨摥䅜瑮噩物獵䅜敬瑲൝∊畡潴汣獯彥污牥≴搽潷摲〺〰〰〰റ∊桳睯扟污潯≮搽潷摲〺〰〰〰റ∊慢潬湯瑟浩潥瑵㴢睤牯㩤〰〰〰挰਍愢敬瑲瑟浩潥瑵㴢睤牯㩤〰〰〰㠷਍猢汩湥彴潭敤㴢睤牯㩤〰〰〰〰਍਍䡛䕋彙佌䅃彌䅍䡃义居体呆䅗䕒卜敨摥䅜瑮噩物獵䅜楶慲൝∊浶獟穩≥搽潷摲〺〰〱搱ര∊業彮慲≭搽潷摲〺〰㈰ㄷര∊業彮牬牟浡㴢睤牯㩤〰〰攴〲਍椢≤搽潷摲〺〰〰〰ര∊湥扡敬≤搽潷摲〺〰〰〰റഊ嬊䭈奅䱟䍏䱁䵟䍁䥈䕎卜䙏坔剁居桓敥層湁楴楖畲屳䱅൝∊潔慴䕬≌搽潷摲〺〰〰〰ര䀊∽ഢഊ嬊䭈奅䱟䍏䱁䵟䍁䥈䕎卜䙏坔剁居桓敥層湁楴楖畲屳湉潣灭敬整䱄嵳਍渢摯㈳灵⽤ば浮硭捶扲攵支ね㘰㍟弲㉬渮灵㴢䌢尺停潲牧浡䘠汩獥屜桓敥⁤湁楴楖畲屳䑜睯汮慯呤浥屰敜ね㘰㍟弲㉬渮灵ഢ∊潮㍤甲摰支爱祬栱攷杹⽸浥〰弶㈳江⸱畮≰∽㩃屜牐杯慲楆敬屳卜敨摥䄠瑮噩物獵屜潄湷潬摡敔灭屜浥〰弶㈳江⸱畮≰਍渢摯㈳灵⽤浡ㅷ慱搵煺汧支ね㘰㍟弲ぬ渮灵㴢䌢尺停潲牧浡䘠汩獥屜桓敥⁤湁楴楖畲屳䑜睯汮慯呤浥屰敜ね㘰㍟弲ぬ渮灵ഢ∊潮㍤甲摰甯灬

  18. #18
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: How can read binary text file first line? and how write to binary text first line

    upload the file youre trying to edit to here please

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Re: How can read binary text file first line? and how write to binary text first line

    Quote Originally Posted by stum View Post
    upload the file youre trying to edit to here please
    thanks stum

    reg.zip

  20. #20
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: How can read binary text file first line? and how write to binary text first line

    replace the line
    Print #FileNo, tempData

    with

    Write #FileNo, tempData
    on the writeLine sub.

    Code:
    Public Sub writeLine(strFilePath As String, lngLineNumber As Long, strLineText As String)
    
    If Dir(strFilePath) = "" Then Exit Sub
    
    'If File Found:
    Dim FileNo As Integer
        FileNo = FreeFile
    
    Dim tempLine As Integer
        tempLine = 1
        
    Dim tempStr As String
    Dim tempData As String
       
       'read the Whole file and place data into tempData
       Open strFilePath For Input As FileNo
          Do
          
            Line Input #FileNo, tempStr
            
            If tempLine = lngLineNumber Then
                If tempLine = 1 Then tempData = strLineText Else tempData = tempData & vbCrLf & strLineText
            Else
                If tempLine = 1 Then tempData = tempStr Else tempData = tempData & vbCrLf & tempStr
            End If
            
            tempLine = tempLine + 1
            
          Loop Until EOF(FileNo)
        Close #FileNo
       
    'Write to the file
        FileNo = FreeFile
        Open strFilePath For Output As FileNo
    
            Write #FileNo, tempData
    
        Close #FileNo
    
    End Sub
    Should solve.

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Re: How can read binary text file first line? and how write to binary text first line

    have a problem again!
    See This:

    Before Write:

    HTML Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Internet Download Manager]
    "FName"="Ghost0507"
    "LName"="/ Team REiS"
    "Email"="teamreis@mail.ru"
    "Serial"="22TH1-9R7NW-GAI4H-RXHLL"
    "AdvIntDriverEnabled2"=dword:00000001

    After Write:

    HTML Code:
    "ے‏Windows Registry Editor Version 5.00
    hello world
    [HKEY_LOCAL_MACHINE\SOFTWARE\Internet Download Manager]
    ""FName""=""Ghost0507""
    ""LName""=""/ Team REiS""
    ""Email""=""teamreis@mail.ru""
    ""Serial""=""22TH1-9R7NW-GAI4H-RXHLL""
    ""AdvIntDriverEnabled2""=dword:00000001
    "
    and after write not import that file!
    Last edited by r2du-soft; Jul 24th, 2014 at 04:43 AM.

  22. #22
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    764

    Re: How can read binary text file first line? and how write to binary text first line

    And that's why I never advocate the use of Write; it tries to be too "helpful" but just messes things up.

    Stick with Print, but add a ";" on the end to suppress the trailing Line break.

    Code:
       Print #FileNo, tempData ;
    Regards, Phill W.

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Re: How can read binary text file first line? and how write to binary text first line

    thanks mr Phill.W
    i test print with ; in end But there is still a problem! #21 = ("ے)

  24. #24
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How can read binary text file first line? and how write to binary text first line

    Quote Originally Posted by r2du-soft View Post
    Thanks dilettante
    Code Good Work,thanks
    i have a question: when i add a ocx in references,is possible not found in some windows?!and program error?!
    OCX? I didn't use any OCX. If you mean the reference to ADO 2.5, that should be good in any version of Windows as far back as Windows 98 and even in Windows 95 if IE 4.x or later was ever installed.

    Program error? What error in what program? We need more details.

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Re: How can read binary text file first line? and how write to binary text first line

    Quote Originally Posted by dilettante View Post
    OCX? I didn't use any OCX. If you mean the reference to ADO 2.5, that should be good in any version of Windows as far back as Windows 98 and even in Windows 95 if IE 4.x or later was ever installed.

    Program error? What error in what program? We need more details.
    no program not error!
    I said maybe not found that dll and then program error!

  26. #26
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How can read binary text file first line? and how write to binary text first line

    You can't use the text I/O statements (Line Input, Print) or private text I/O statements (Input, Write) with these files.

    As I said before, they're UTF-16LE encoded. Those statements assume ANSI encoding.

    The fact that any of this other code people are giving you appears to "work" at all is sheer luck. You are doing multiple implicit conversions from ANSI to Unicode and back while ignoring the BOM and treating it as data.

  27. #27
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: How can read binary text file first line? and how write to binary text first line

    Try this. Reads Unicode file and removes UTF16LE BOM:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
       Dim strText As String
       strText = UnicodeFile_Read_VB(App.Path & "\reg.reg")
       MsgBox strText
    End Sub
    
    'Purpose: Override Vb6 MsgBox with Unicode aware MsgBox. HelpFile/Context not supported.
    Function MsgBox(Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String) As VbMsgBoxResult
       MsgBox = CreateObject("WScript.Shell").Popup(Prompt, 0&, Title, Buttons)
    End Function
    
    Public Function UnicodeFile_Read_VB(ByVal sFileName As String) As String
    
       Dim FF               As Long
       Dim b()              As Byte
       Dim S                As String
    
       On Error GoTo ErrHandler
    
       FF = FreeFile
       Open sFileName For Binary Access Read As FF
       ReDim b(LOF(FF) - 1)
       Get FF, , b
       Close FF
       'Detect file encoding
       If b(0) = 255 And b(1) = 254 Then 'UTF-16LE BOM FF FE
          S = b
          S = Mid$(S, 2) 'Remove BOM
       Else
          S = StrConv(b, vbUnicode)   'ANSI file
       End If
    
       UnicodeFile_Read_VB = S
       Exit Function
       
    ErrHandler:
       Err.Raise Err.Number, "UnicodeFile_Read_VB", Err.Description
       
    End Function
    Name:  UnicodeFile_Read_Vb.png
Views: 745
Size:  19.3 KB
    Last edited by DrUnicode; Jul 24th, 2014 at 03:28 PM. Reason: Screenshot

  28. #28

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Re: How can read binary text file first line? and how write to binary text first line

    Quote Originally Posted by DrUnicode View Post
    Try this. Reads Unicode file and removes UTF16LE BOM:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
       Dim strText As String
       strText = UnicodeFile_Read_VB(App.Path & "\reg.reg")
       MsgBox strText
    End Sub
    
    'Purpose: Override Vb6 MsgBox with Unicode aware MsgBox. HelpFile/Context not supported.
    Function MsgBox(Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String) As VbMsgBoxResult
       MsgBox = CreateObject("WScript.Shell").Popup(Prompt, 0&, Title, Buttons)
    End Function
    
    Public Function UnicodeFile_Read_VB(ByVal sFileName As String) As String
    
       Dim FF               As Long
       Dim b()              As Byte
       Dim S                As String
    
       On Error GoTo ErrHandler
    
       FF = FreeFile
       Open sFileName For Binary Access Read As FF
       ReDim b(LOF(FF) - 1)
       Get FF, , b
       Close FF
       'Detect file encoding
       If b(0) = 255 And b(1) = 254 Then 'UTF-16LE BOM FF FE
          S = b
          S = Mid$(S, 2) 'Remove BOM
       Else
          S = StrConv(b, vbUnicode)   'ANSI file
       End If
    
       UnicodeFile_Read_VB = S
       Exit Function
       
    ErrHandler:
       Err.Raise Err.Number, "UnicodeFile_Read_VB", Err.Description
       
    End Function
    Name:  UnicodeFile_Read_Vb.png
Views: 745
Size:  19.3 KB
    thanks DrUnicode
    now just need a code for write to line 2 ,i how can write in line 2?
    thanks

  29. #29
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: How can read binary text file first line? and how write to binary text first line

    Added writing to Line 2 and saving as Unicode "Reg2.reg"

    Note: Be careful what you add to reg file. If it is a comment then prefix it with semicolon ";".

    Code:
    Option Explicit
    
    Private Sub Form_Load()
       Dim strText As String
       Dim vSplit() As String
       
       strText = UnicodeFile_Read_VB(App.Path & "\reg.reg")
       vSplit = Split(strText, vbCrLf)
       'Change second line:
       vSplit(1) = ";Hello World"
       strText = Join(vSplit, vbCrLf)
       UnicodeFile_Write_VB App.Path & "\reg2.reg", strText
       MsgBox strText
    End Sub
    
    'Purpose: Override Vb6 MsgBox with Unicode aware MsgBox. HelpFile/Context not supported.
    Function MsgBox(Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String) As VbMsgBoxResult
       MsgBox = CreateObject("WScript.Shell").Popup(Prompt, 0&, Title, Buttons)
    End Function
    
    'Purpose: Note that sFileName must be ANSI. If SplitDelimeter, will Split into array.
    Public Function UnicodeFile_Read_VB(ByVal sFileName As String) As String
    
       Dim FF               As Long
       Dim b()              As Byte
       Dim S                As String
    
       On Error GoTo ErrHandler
    
       FF = FreeFile
       Open sFileName For Binary Access Read As FF
       ReDim b(LOF(FF) - 1)
       Get FF, , b
       Close FF
       'Detect file encoding
       If b(0) = 255 And b(1) = 254 Then 'UTF-16LE BOM FF FE
          S = b
          S = Mid$(S, 2) 'Remove BOM
       Else
          S = StrConv(b, vbUnicode)   'ANSI file
       End If
    
       UnicodeFile_Read_VB = S
       Exit Function
       
    ErrHandler:
       Err.Raise Err.Number, "UnicodeFile_Read_VB", Err.Description
       
    End Function
    
    Public Sub UnicodeFile_Write_VB(ByVal sFileName As String, _
       ByVal vVar As String)
    
       Dim FF               As Long
       Dim b()              As Byte
       Dim FSO
       
       On Error GoTo ErrHandler
       
       Set FSO = CreateObject("Scripting.FileSystemObject")
       FSO.CreateTextFile sFileName, True, False
       FF = FreeFile
       Open sFileName For Binary Access Write As #FF
       Put #FF, , CInt(&HFEFF) 'UTF16LE BOM
       b = vVar
       Put #FF, , b
       Close #FF
       
       Exit Sub
       
    ErrHandler:
       Err.Raise Err.Number, "UnicodeFile_Write_VB", Err.Description
    End Sub
    Name:  UnicodeFile_Read_Vb.png
Views: 713
Size:  21.3 KB
    Last edited by DrUnicode; Jul 24th, 2014 at 07:10 PM. Reason: More info

  30. #30

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Location
    Iran
    Posts
    108

    Re: How can read binary text file first line? and how write to binary text first line

    Thanks Again
    and end of question:
    how can read just line 2 and show?!

  31. #31
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: How can read binary text file first line? and how write to binary text first line

    Read Line 2:

    Code:
    Private Sub Command1_Click()
       Dim strText As String
       Dim vSplit() As String
       
       strText = UnicodeFile_Read_VB(App.Path & "\reg2.reg")
       vSplit = Split(strText, vbCrLf)
       MsgBox vSplit(1)
    End Sub
    Name:  Line2.png
Views: 518
Size:  8.0 KB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width