dcsimg
Results 1 to 18 of 18

Thread: Weird thing happenning when I try to save a file to disc

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    97

    Weird thing happenning when I try to save a file to disc

    I 'm trying to save some strings in disc files using vb6.
    My program reads from some data source and computes those strings.

    The code segment is:

    for i%=1 to 1000

    << read data - compute string >>

    filename = "file"+ltrim$(str$(i%))

    u$ = string
    if dir$(filename$)<>"" then kill filename$
    open filename$ for binary as #1
    put #1, 1, u$
    close

    next i%

    Now the first string is "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " and saves ok.
    The second is "1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " but is does n' go !

    I interrupt the program and check file1. It's ok.
    But file2 shows this thing:

    ‱†‱†‱†‱†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†‰†

    file3 shows:

    ‰†‰†‰†‰†′†′†′†′†‰†‰†‰†‰†‰†‰†‰†‰†‰†‱†‰†‱†‰†‱†‰†‱†‰†‱†‰†‱†‰†‰†‰†‰†‱†‱†‱†‱†

    and so on !!
    What's happening ?

    I also interrupt the program before the open command as well as after the close command and check the u$ in the immediate and it is as it should be !
    What have I done ???

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,663

    Re: Weird thing happenning when I try to save a file to disc

    Ok, when you execute that Put statement, you're getting Unicode-to-ASCII conversion. (That assumes your strings are typical variable length BSTR type strings.)

    Personally, I think there's something wrong with your string-builder code. The easiest way to determine that is to also output your strings to the immediate window (Debug.Print u$) and see what you're getting.

    EDIT1: Ahhh, I see you tried that. And the string is what you want in the immediate window?
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,663

    Re: Weird thing happenning when I try to save a file to disc

    Another idea. How are you checking what's in the files? Notepad? Re-reading them with VB6?

    Be sure to use Notepad, or the problem may be the way you're re-reading the files with VB6.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  4. #4
    New Member
    Join Date
    Sep 2019
    Posts
    4

    Re: Weird thing happenning when I try to save a file to disc

    [QUOTE=johnywalker;5413851]
    .
    .
    .
    u$ = string
    if dir$(filename$)<>"" then kill filename$
    open filename$ for binary as #1
    put #1, 1, u$
    close
    .
    .
    .
    Instead of using open Binary, use for Output
    Using For Output replaces any existing file so the kill isn't needed
    so like this
    .
    .
    Open filename$ For Output As #1
    Print #1,u$

    .
    .
    .

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    97

    Re: Weird thing happenning when I try to save a file to disc

    Well the checking was with notepad indeed.
    I also emptied the recycle bin because there were lots of files in it from the many experiments with this program (in case it was some disc full situation).
    No progress.

    But this thing was working some hours ago - it's some kind of little modification I was trying to do.
    I mean it was "read original data for two years" now it's supposed to "read original data for the last six months" - unrelated to the issue we are discussing.

    Now with the "open for output" seems to have worked for the first two. Let's see the lot ...
    Yep worked.


    But should work with "open for binary" just as well.
    The only kind of strange thing with this program is that it stores in memory some really big strings -the parent data set. 4.5 Mb.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    97

    Re: Weird thing happenning when I try to save a file to disc

    I still don't get what happened.
    Is the 4.5 Mb strings with which this routine is loaded causing the problem in the final save operation ?
    The program worked with "for output" but that's no excuse.

  7. #7
    New Member
    Join Date
    Sep 2019
    Posts
    4

    Re: Weird thing happenning when I try to save a file to disc

    Quote Originally Posted by johnywalker View Post
    I still don't get what happened.
    Is the 4.5 Mb strings with which this routine is loaded causing the problem in the final save operation ?
    The program worked with "for output" but that's no excuse.
    Best I can say is..

    Strings a re stored in VB as Byte..

    Read up on it at https://www.dreamincode.net/forums/t...file-handling/

    All I know is that in order to write data to a binary file you need to make sure it's string, and not the internal representation of the data (which is what it looked like your code was doing..), and which is why I recommended the "FOR OUTPUT" cause i KNOW it writes a string.. "FOR BINARY" makes assumptions about your data..

    Glad it worked. And after all, isn't that the goal?

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,426

    Re: Weird thing happenning when I try to save a file to disc

    Quote Originally Posted by mdickerson View Post
    Strings a re stored in VB as Byte..
    No. They aren't.

    They are normally stored as Unicode, typically 2 bytes per character but sometimes 4. You can jam ANSI, or UTF-8, or almost anything into a String but then you need to know what you are doing.

    Native VB String I/O normally expects ANSI/DBCS file data and transcodes to/from Unicode when reading and writing. Getting around that requires unusual effort.

  9. #9
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,797

    Re: Weird thing happenning when I try to save a file to disc

    Quote Originally Posted by dilettante View Post
    They are normally stored as Unicode, typically 2 bytes per character but sometimes 4.
    That sentence made me investigate.
    I was (and still am) quite convinced that the VB strings are always two bytes lenght.
    According to this post from Merri:

    Quote Originally Posted by Merri View Post
    Native VB6 strings are UTF-16, because Windows uses UTF-16. VB6 strings use two bytes per character.
    OK, UTF-16 is actually variable lenght, but VB6 does really use UTF-16 or UCS-2 instead?
    Last edited by Eduardo-; Sep 10th, 2019 at 02:30 PM.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,426

    Re: Weird thing happenning when I try to save a file to disc

    Run this program.

    The first sample has 3 characters (two of them quotation marks) yet measures as 4 characters by the Len() function.

    The second has 4 characters yet measures as 6: two quotes and 2 more 32-bit chars.

    Name:  sshot.png
Views: 52
Size:  4.0 KB


    They are displayed two ways here because GDI renders them one way and RichEdit renders them another way. That becomes more apparent with other characters like the "poo" and other emoticons:

    Name:  sshot alt.png
Views: 54
Size:  3.8 KB


    UCS-2 either died with Win9x or with both Win9x and NT 4.0, but for NT starting in at least Win2K if not before it was gone. UCS-2 has been history for a very long time.

    People like to put on that clown nose to look pedantic, not realizing it has the opposite effect.
    Attached Files Attached Files
    Last edited by dilettante; Sep 10th, 2019 at 09:43 PM.

  11. #11
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,797

    Re: Weird thing happenning when I try to save a file to disc

    That's interesting.
    As long as VB handles it as if they were always two bytes per character, I see it as a matter of rendering.
    My concern was if we could rely on the relation LenB/Len = 2 for the String type, and I see we can.

    Thank you dilettante for the sample and clarification

  12. #12
    New Member
    Join Date
    Sep 2019
    Posts
    4

    Re: Weird thing happenning when I try to save a file to disc

    All in all I guess it's cool. We got an answer even if it was just to prove me wrong and to make someone else "superior".. I love it when people correct me..

    All I know is that my code works when I write it.. Whether It works because I'm an ignorant idiot that got lucky (for 20+ years running..), or because I know what I'm doing matters not.. The code works and the client's happy.. What more is there?

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,426

    Re: Weird thing happenning when I try to save a file to disc

    The 4-byte Unicode characters are rare and few fonts bother to support them. Most of the time we can just ignore those cases and assume 2-byte characters. That's safe in most normal programs.

  14. #14
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,277

    Re: Weird thing happenning when I try to save a file to disc

    Quote Originally Posted by Eduardo- View Post
    That's interesting.
    As long as VB handles it as if they were always two bytes per character, I see it as a matter of rendering.
    My concern was if we could rely on the relation LenB/Len = 2 for the String type, and I see we can.
    Just FYI, since you mentioned a posting by Merri indicating 2 bytes per character....

    In another thread by Merri (if memory serves), he opted for 4 bytes per character as safety (oversizing a buffer) before sending it to WideCharToMultiByte/MultiByteToWideChar API. I'm assuming that "safety net" was to address any possibility of 4-byte characters being returned and ensuring buffer was large enough.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  15. #15
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,797

    Re: Weird thing happenning when I try to save a file to disc

    Quote Originally Posted by LaVolpe View Post
    Just FYI, since you mentioned a posting by Merri indicating 2 bytes per character....

    In another thread by Merri (if memory serves), he opted for 4 bytes per character as safety (oversizing a buffer) before sending it to WideCharToMultiByte/MultiByteToWideChar API. I'm assuming that "safety net" was to address any possibility of 4-byte characters being returned and ensuring buffer was large enough.
    We should know how he measured the number of "characters".
    Because if VB's Len function always returns LenB/2, then the number of real characters becomes more like a matter of how many glyphs the system uses to render the "characters".
    We might consider that the 4 bytes characters are two characters drawn with only one glyph.
    Am I right?

  16. #16
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,797

    Re: Weird thing happenning when I try to save a file to disc

    I wonder how AscW and ChrW can work with 4 bytes characters.

  17. #17
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,426

    Re: Weird thing happenning when I try to save a file to disc

    They don't as far as I can tell, at least not properly.

    The problem is those functions were never updated from the form they had in 32-bit VB4, which would have been the first VB supporting Unicode, back then UCS-2. We never got our VB7 which probably would have addressed that though we have no evidence of that today.

    If your code wanted to use them it would have to manually deal with treating the 32-bit characters as two 16-bit characters.

  18. #18
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,797

    Re: Weird thing happenning when I try to save a file to disc

    Ok.-

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width