Write string to file

User avatar
ErikJan
BronzeLounger
Posts: 1250
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Write string to file

Post by ErikJan »

String is: "TEST" [WITH the quotes]

If I do: Print #1, String

The file contains: TEST [Without the quotes]

Am I messing things up or is that normal? If the latter is true,how do I get the quotes transferred correctly???

User avatar
HansV
Administrator
Posts: 78507
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Write string to file

Post by HansV »

Can you post the code that you use to set the string variable?

I tried the following, and it worked OK:

Code: Select all

Sub TestString()
    Dim f As Long
    Dim s As String
    f = FreeFile
    Open "test.txt" For Output As #f
    s = """Test"""
    Print #f, s
    Close #f
End Sub
Result:
x816.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1250
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Write string to file

Post by ErikJan »

Sorry (formulated my question not clear enough again)..., the variable with quotes is a variable which I read from another file... That one contains the single quotes already.

So, I create a text file with "Test" in there (including the quotes). (So I do that either manually or by writing it out as """Test""").

Now the file if there. I read the content into a variable (say 's'). The variable now indeed contains "Test" (with one quote to the left and one to the right as I wanted).

Now it looks like if I now create another text file and do:

print #1, s

The content of that new file is Test (I lost the quotes)

User avatar
HansV
Administrator
Posts: 78507
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Write string to file

Post by HansV »

That is to be expected. The Input instruction treats the quotes around the string as string delimiters. After reading the variable, you'll see "Test" if you hover the mouse pointer over the variable, but the actual contents of the variable are just the word Test without the quotes. You'll have to add quotes around the string again.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1250
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: Write string to file

Post by ErikJan »

So if I'm losing the quotes in the input, I should probably "Line Input" then, right?

User avatar
HansV
Administrator
Posts: 78507
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Write string to file

Post by HansV »

Yes, indeed.
Best wishes,
Hans