We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Preceding Space when using Print command to write variables to a File.

markhilbert
markhilbert asked
on
Medium Priority
221 Views
Last Modified: 2010-04-07
Hi all,

I'm having a little trouble writing a text file with information from a vb program this is a sample of my code.

a=1
b=2.5
c=37

Open file.txt For Output As #1
Print #1, "Name"
Print #1, a; b; c
Close #1

I want the output file have a, b & c all on one line and seperated by spaces, not commas. Which this does, however it puts a space in the line before the variable "a" is printed, I can't get rid of the space. Name doesn't have a space before it when it is printed, so I really don't know why its insisting on putting the space there.

Output looks like this:

Name
 1 2.5 7



Using the write command gets rid of the space however it places a comma between each variable.



Thanks.

Mark
Comment
Watch Question

Brian MulderFreelance
Top Expert 2006

Commented:
Hello markhilbert,

you can use a trim around the variables like
----------
Sub t()
a = 1
b = 2.5
c = 37

Open "c:\file.txt" For Output As #1
Print #1, "Name"
Print #1, Trim(a); Trim(b); Trim(c)
Close #1

End Sub
----------

hope this helps a bit
bruintje
Commented:
Try this:

a=1
b=2.5
c=37

Open file.txt For Output As #1
Print #1, "Name"
Print #1, a & " " & b & " " & c
Close #1

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Freelance
Top Expert 2006
Commented:
rereading the question you can also try

Sub t()
a = 1
b = 2.5
c = 37

Open "c:\file.txt" For Output As #1
Print #1, "Name"
Print #1, Trim(a) & Chr(32); Trim(b) & Chr(32); Trim(c) & Chr(32)
Close #1

End Sub

Author

Commented:
Thanks to both of you, both solutions worked perfectly.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.