Author Topic: WRITE # and PRINT #  (Read 4460 times)

0 Members and 1 Guest are viewing this topic.

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
WRITE # and PRINT #
« on: July 30, 2021, 03:28:03 am »
Hi,
I must create some CSV files, but I have noticed that, if for example, I write:

PRINT #1, "hello1"
WRITE #1, "hello2"

hello2 is written on line 1, over hello1. In order to have on line 1 "hello1" and in line 2 "hello 2", I have to use or only PRINT # or only WRITE #.

Is there a way to combine both, or to use WRITE # in a way where is possible to write characters such "?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: WRITE # and PRINT #
« Reply #1 on: July 31, 2021, 10:04:22 am »
Fellippe explained pretty well here: https://www.qb64.org/forum/index.php?topic=3986.0

My reply #4 was about using WRITE # only for CSV files that I learned from vid.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: WRITE # and PRINT #
« Reply #2 on: August 01, 2021, 03:58:20 am »
It is an interesting issue I must decide to see last 3 video of Fellippe!

in the while this code seems to have no issue mixing INPUT and WRITE
Code: QB64: [Select]
  1. filename$ = "testfile.dat"
  2. x = 1: y = 2: z$ = "Three"
  3.  
  4. Open filename$ For Output As #1 'opens and clears an existing file or creates new empty file
  5. Print #1, x
  6. Write #1, y
  7. Print #1, z$
  8.  
  9.  
  10. Print "File created with data. Press a key!"
  11.  
  12. K$ = Input$(1) 'press a key
  13.  
  14. Open filename$ For Input As #2 'opens a file to read it
  15.  
  16. Input #2, a, b, c$
  17.  
  18.  
  19. Print a, b, c$
  20. Write a, b, c$
  21.  
  22.  
so it is better to see video on files to get the issue instead to wait to fall in it!
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: WRITE # and PRINT #
« Reply #3 on: August 01, 2021, 06:25:13 am »
Yeah wont see a difference until do this:
Code: QB64: [Select]
  1. Open "Test.txt" For Output As #1
  2. Print #1, "hello1A", "hello1B"
  3. Write #1, "hello2A", "hello2B"
  4.  
  5. Open "Test.txt" For Input As #1
  6.     Input #1, t$
  7.     Print t$
  8.  

Here is what Test.txt looks like:
Quote
hello1A       hello1B
"hello2A","hello2B"

  [ You are not allowed to view this attachment ]  
« Last Edit: August 01, 2021, 06:28:53 am by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: WRITE # and PRINT #
« Reply #4 on: August 01, 2021, 05:20:12 pm »
Hi Bplus
as before I have said "better to follow the video and get the issue than fall in the issue while you are building a program".

So I have followed the magnific video  of Fellippe "file part 1" and finding it very interesting I have saved the file codes of the different examples showed by Fellippe.
I post a zip with all these codes into it here because the original thread is locked. Only .BAS.
Programming isn't difficult, only it's  consuming time and coffee

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: WRITE # and PRINT #
« Reply #5 on: August 02, 2021, 04:31:42 am »
Fellippe explained pretty well here: https://www.qb64.org/forum/index.php?topic=3986.0

My reply #4 was about using WRITE # only for CSV files that I learned from vid.

I have seen the videos 1 and 2. Very interesting.

Yeah wont see a difference until do this:
Code: QB64: [Select]
  1. Open "Test.txt" For Output As #1
  2. Print #1, "hello1A", "hello1B"
  3. Write #1, "hello2A", "hello2B"
  4.  
  5. Open "Test.txt" For Input As #1
  6.     Input #1, t$
  7.     Print t$
  8.  

Here is what Test.txt looks like:
  [ You are not allowed to view this attachment ]

Speaking about CSV files, I found the problem. By your code I have seen that using WRITE #, after using PRINT #, the line used is the following.

In my code, it didn't work, because it was like that:

        PRINT #1, "In Windows settings the index separator character mus be " + CHR$(34) + CHR$(44) + CHR$(34) + "."
        WRITE #1, "BLA BLA BLA"

in this case, "BLA BLA BLA" (IN EXCEL) is not written in the following line, but in the next cell.

The problem is CHR$(44), that is the comma, that in my Windows settings is the index separator. The ame using "," instead of CHR$(44).

So the problem, that is avoidable simpling writing "comma" instead of ",", is how is it possible to write a "," in a CSV file that is visible in Excel as a comma.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: WRITE # and PRINT #
« Reply #6 on: August 02, 2021, 08:59:44 am »
I think commas enclosed by parenthesis would be OK, you have to say what Excel does with these:

Code: QB64: [Select]
  1. ' this outputs everything in 1 line, does Excel do same?
  2. Open "Test.txt" For Output As #1
  3. Write #1, "hello1A", "hello1B", "blah, blah, blah"
  4.  
  5.  
  6. ' this outputs a list, each item on separate line, does Excel do same?
  7. 'Open "Test.txt" For Output As #1
  8. 'Write #1, "hello1A"
  9. 'Write #1, "hello1B"
  10. 'Write #1, "blah, blah, blah"
  11. 'Close #1
  12.  
  13.  
  14. Open "Test.txt" For Input As #1
  15.     Line Input #1, t$
  16.     Print t$
  17.  

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: WRITE # and PRINT #
« Reply #7 on: August 02, 2021, 01:01:03 pm »
I think commas enclosed by parenthesis would be OK, you have to say what Excel does with these:

Code: QB64: [Select]
  1. ' this outputs everything in 1 line, does Excel do same?
  2. Open "Test.txt" For Output As #1
  3. Write #1, "hello1A", "hello1B", "blah, blah, blah"
  4.  
  5.  
  6. ' this outputs a list, each item on separate line, does Excel do same?
  7. 'Open "Test.txt" For Output As #1
  8. 'Write #1, "hello1A"
  9. 'Write #1, "hello1B"
  10. 'Write #1, "blah, blah, blah"
  11. 'Close #1
  12.  
  13.  
  14. Open "Test.txt" For Input As #1
  15.     Line Input #1, t$
  16.     Print t$
  17.  

All you have written works fine. using comma with PRINT #, as I was trying to do, it doesn't work, but with WRITE # it does. With WRITE # it seems that it is also possible to print ", using CHR$(34), so... I think to have get lost in a glass of water!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: WRITE # and PRINT #
« Reply #8 on: August 02, 2021, 05:35:37 pm »
Quote
I think to have get lost in a glass of water!

I don't think you have to but that is an interesting expression. :)

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: WRITE # and PRINT #
« Reply #9 on: August 02, 2021, 06:19:00 pm »
Hi QB64 coders
here the .BAS written by Fellippe in the video File Part2.


@bartok AND @bplus  here some difference in sentences and proverbs between english and italian languages:

Quote
Perdersi in un bicchiere d'acqua
To lose oneself in a glass of water/
To drown in a glass of water
To be easily discouraged.

Read more: https://www.lenntech.com/water-proverbs.htm#ixzz72QP1uj7I

here the links https://www.lenntech.com/water-proverbs.htm
https://hinative.com/en-US/questions/16507733
Programming isn't difficult, only it's  consuming time and coffee

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: WRITE # and PRINT #
« Reply #10 on: August 03, 2021, 02:34:11 am »
I don't think you have to but that is an interesting expression. :)

Yes, I also agree that I don't have too:DD