Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. using the 'Print' statement in VB

using the 'Print' statement in VB

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestionloungeworkspace
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nebbish
    wrote on last edited by
    #1

    I've got a string variable that goes through heavy string manipulation that may or may not leave the string with a 'carriage return - line feed' at the end. It is declared like this: Dim buf as String Dim filenum as Integer Now i'm trying to output this string into a file and this is JUST ONE of the ways i've tried: filenum = FreeFile Open "newfile.txt" For Output As filenum Print #filenum, buf Close filenum Now here's my problem: No matter how i try this, after opening the file in an external editor I see that a 'carriage return - line feed' has been added to the end. I can't seem to avoid this and it's DRIVING ME CRAZY! When 'buf' has a final character of '.', then when I open the file in my editor and press <Ctrl>+<End>, I expect the cursor to fall to the right of the '.' not on the blank line below. And before you ask, the editor I'm using doesn't do anything extra like ensuring a blank last line (like the VB environment). I've tried various combinations of: opening the file in binary or text mode, opening for input or random, using the 'Print' or 'Put' function. Can anybody tell me how to output a string to a file WITHOUT adding a blank last line? thanks, nebbish Smart, Sexy, Sane. Pick two.

    P 1 Reply Last reply
    0
    • N nebbish

      I've got a string variable that goes through heavy string manipulation that may or may not leave the string with a 'carriage return - line feed' at the end. It is declared like this: Dim buf as String Dim filenum as Integer Now i'm trying to output this string into a file and this is JUST ONE of the ways i've tried: filenum = FreeFile Open "newfile.txt" For Output As filenum Print #filenum, buf Close filenum Now here's my problem: No matter how i try this, after opening the file in an external editor I see that a 'carriage return - line feed' has been added to the end. I can't seem to avoid this and it's DRIVING ME CRAZY! When 'buf' has a final character of '.', then when I open the file in my editor and press <Ctrl>+<End>, I expect the cursor to fall to the right of the '.' not on the blank line below. And before you ask, the editor I'm using doesn't do anything extra like ensuring a blank last line (like the VB environment). I've tried various combinations of: opening the file in binary or text mode, opening for input or random, using the 'Print' or 'Put' function. Can anybody tell me how to output a string to a file WITHOUT adding a blank last line? thanks, nebbish Smart, Sexy, Sane. Pick two.

      P Offline
      P Offline
      Pablo75
      wrote on last edited by
      #2

      try with : write #filenum, buf Pablo75

      N 1 Reply Last reply
      0
      • P Pablo75

        try with : write #filenum, buf Pablo75

        N Offline
        N Offline
        nebbish
        wrote on last edited by
        #3

        Well thanks for the idea Pablo75, but the 'Write' statement added a double quote at the beginning and again at the end of the file. Essentially the whole file was enclosed in one set of double-quotes. And by the way, for anybody else whose reading this thread as well, I've had success when opening the file in binary mode and using the 'Put' function EXCEPT when I am replacing an existing (text) file with a string that is shorter. In those cases, opening the file in binary does not reset the length of the file, so the file ends up being my new string followed by the old contents of the file that existed beyond the length of the new string. Can I open an existing file in binary and somehow reset the file length? discarding the old contents entirely? thanks, nebbish Smart, Sexy, Sane. Pick two.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups