Page 1 of 1

Console question

Posted: Thu Aug 15, 2013 6:00 pm
by krzysztof
Hello everyone :). I'm new here. I'm just beginning to write simple programs in PureBasic. I did not want to create new topic, so I added my question here. In the Help concerning command "CloseConsole" is the example of code:

Code: Select all

For i = 0 To 4
    If OpenConsole()
      PrintN("This is console #"+Str(i))
      PrintN("Press return to close console")
      Input()
      CloseConsole()
    EndIf
  Next
It uses the loop If...EndIf, but is it really needed? This example and my first very simple programs operate both with the loop and without it. The console is created regardless of whether the program has a loop If...EndIf or not. Is If...EndIf really needed here, and if so, what is its influence on the functioning of the console?

Re: OpenConsole invisibly?

Posted: Thu Aug 15, 2013 8:00 pm
by Fred
Better create a new topic because your question has nothing to do with this topic.

Re: Console question

Posted: Thu Aug 15, 2013 8:15 pm
by TI-994A
krzysztof wrote:...concerning command "CloseConsole" is the example of code:

Code: Select all

For i = 0 To 4
    If OpenConsole()
      PrintN("This is console #"+Str(i))
      PrintN("Press return to close console")
      Input()
      CloseConsole()
    EndIf
  Next
It uses the loop If...EndIf, but is it really needed?
Hello krzysztof, and welcome to the PureBasic forum. To answer your question, the OpenConsole() function will work without the If/EndIf condition. However, in the event that the function fails, it returns a zero, and the If/EndIf condition will not execute the other instructions within it. In this example, the other instructions include the PrintN() function, which will not work unless OpenConsole() has been successfully called first. :wink:

Re: Console question

Posted: Sun Aug 18, 2013 11:24 pm
by krzysztof
First of all, many thanks for "TI-994A" for his answer and additional extended support.

I added a command to write calculated prime numbers to a text file, and it works almost fine. But there is a problem: the prime numbers are correctly calculated and stored in a file up certain values. But when I enter a value of 2000, instead od numbers in the file there are such weird symbols:" 㐠″㜴㔠″㤵㘠‱㜶㜠‱㌷㜠‹㌸㠠‹㜹ㄠ㄰ㄠ㌰ㄠ㜰ㄠ㤰ㄠ㌱ㄠ㜲ㄠㄳㄠ㜳". This is not normal in my opinion.
The second problem is with the part of code, which opens, then read content of the file, and display it on the screen. It does not work, but it should by me. What do You think about these two problems? Here is my code:

Code: Select all

If OpenConsole()  
  Define.i n, p, q, divide
  Print("Enter the limit ")
  n = Val(Input())
  CreateFile(0, "Prime_numbers.txt")  
  OpenFile(0, "Prime_numbers.txt")
  For p = 2 To n
    divide = 0
    For q = 2 To Sqr(p)
      If p % q = 0
        divide = 1
        Break
      EndIf
    Next q
    If divide = 0
      WriteString(0, Str(p) + " ")
    EndIf
  Next p
  CloseFile(0)
  PrintN("Ready. Calculated prime numbers are stored in Prime_numbers.txt file")
  
  ReadFile(0, "Prime_numbers.txt")
  ReadString(0)
  CloseFile(0)
  
  Print("Press any key to exit...")
  Repeat
  Until Inkey() <> ""
  CloseConsole()
EndIf
End

Re: Console question

Posted: Sun Aug 18, 2013 11:41 pm
by luis
Hi.

Createfile already give you the handle for the file. You don't need to use openfile just after it.
See the code example for createfile() in the help.

Readstring(0) return the contents of the file (see the help for it), you should do something like

text$ = Readstring(0)
Debug text$

or what you read just vanish away.

Suggestion: you should always check the return values if available of the commands you use, like you did with OpenConsole.
Especially when using files.

Re: Console question

Posted: Mon Aug 19, 2013 12:00 am
by krzysztof
Thanks for the answer. But weird symbols are still during n = 2000. And what can I use instead of debug command? Noto for debugger but for the console? I tried to use something like ReadString, but I obtain the error.

Re: Console question

Posted: Mon Aug 19, 2013 12:08 am
by luis
This works here.

Code: Select all

If OpenConsole() 
  Define.i n, p, q, divide
  Define t$
  
  Print("Enter the limit ")
  ; n = Val(Input())
  n = 2000
  If CreateFile(0, "Prime_numbers.txt") 
      ; OpenFile(0, "Prime_numbers.txt")
      For p = 2 To n
        divide = 0
        For q = 2 To Sqr(p)
          If p % q = 0
            divide = 1
            Break
          EndIf
        Next q
        If divide = 0
          WriteString(0, Str(p) + " ")
        EndIf
      Next p
      CloseFile(0)
  EndIf

  PrintN("Ready. Calculated prime numbers are stored in Prime_numbers.txt file")
 
  If ReadFile(0, "Prime_numbers.txt")
      t$ = ReadString(0)
      PrintN(t$) ; <- write to console
      CloseFile(0)
  EndIf
 
  Print("Press any key to exit...")
  Repeat
  Until Inkey() <> ""
  CloseConsole()
EndIf
End

output

Code: Select all

Enter the limit Ready. Calculated prime numbers are stored in Prime_numbers.txt
file
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 1
07 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 2
23 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 3
37 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 4
57 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 5
93 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 7
19 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 8
57 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 9
97 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 10
97 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 12
23 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 13
21 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 14
59 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 15
71 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 16
93 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 18
11 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 19
49 1951 1973 1979 1987 1993 1997 1999
Press any key to exit...

Re: Console question

Posted: Mon Aug 19, 2013 1:44 am
by krzysztof
Thank You very much for the answer. Now the prime numbers are correctly shown on the screen (2, 5, 7, 11...), but when I opened the generated txt file, there are inside STILL weird symbols (i.e. ㄠ″㜱ㄠ‹㌲㈠‹ㄳ㌠‷ㄴ㐠″㜴㔠″㤵㘠‱㜶㜠‱㌷㜠), NOT prime numbers. Maybe it is a bug of PureBasic or my system?

Re: Console question

Posted: Mon Aug 19, 2013 2:43 am
by citystate
it could be that your system is defaulting to a non-ASCII coding
try this and see if it changes anything:

Code: Select all

If OpenConsole() 
  Define.i n, p, q, divide
  Define t$
  
  Print("Enter the limit ")
  ; n = Val(Input())
  n = 2000
  If CreateFile(0, "Prime_numbers.txt") 
      ; OpenFile(0, "Prime_numbers.txt")
      For p = 2 To n
        divide = 0
        For q = 2 To Sqr(p)
          If p % q = 0
            divide = 1
            Break
          EndIf
        Next q
        If divide = 0
          WriteString(0, Str(p) + " ",#PB_Ascii)
        EndIf
      Next p
      CloseFile(0)
  EndIf

  PrintN("Ready. Calculated prime numbers are stored in Prime_numbers.txt file")
 
  If ReadFile(0, "Prime_numbers.txt")
      t$ = ReadString(0)
      PrintN(t$) ; <- write to console
      CloseFile(0)
  EndIf
 
  Print("Press any key to exit...")
  Repeat
  Until Inkey() <> ""
  CloseConsole()
EndIf
End
also, when you have an unrelated question, we've gotten into the habit of starting a new thread for each - it makes it easier for people to help you :wink:

Re: Console question

Posted: Mon Aug 19, 2013 4:51 am
by TassyJim
When I run the above code, my text editor thinks the text file is UNICODE and has non-ANSI characters.
It all looks OK and standard ASCII when viewed with a hex editor.

Altering the code to add a CR every 10 numbers results in a text file that can be opened normally.
Putting the CR after 22 numbers is still OK but after 23 numbers causes problems.

I use metapad but Notepad has the same problem.
Wordpad does read the file correctly.

Having the CR's means that t$ = ReadString(0) does not read the full file.


Jim


PB 5.20b10 x86 W7

Re: Console question

Posted: Mon Aug 19, 2013 5:11 am
by TassyJim
Results of more tests:
Printing a CR anywhere in the file before the 23rd number stops notepad and metapad thinking the file is UNICODE.
Even as the first characters in the file is OK.

The problem is nothing to do with PureBasic but I would love to know why the text editors are getting screwed.

Jim

Re: Console question

Posted: Mon Aug 19, 2013 1:32 pm
by luis
TassyJim wrote: The problem is nothing to do with PureBasic but I would love to know why the text editors are getting screwed.
Not everyone. Maybe they are compiled in unicode and they assume any input must be unicode.
Writing a BOM to the start of the file should make them happy.

Code: Select all

#file$ = "c:\Prime_numbers.txt"

If OpenConsole()
  Define.i n, p, q, divide
  Define t$
 
  Print("Enter the limit ")
  n = Val(Input())
  
  If CreateFile(0, #file$)  
      WriteStringFormat(0, #PB_UTF8) ; <- write BOM
      For p = 2 To n
        divide = 0
        For q = 2 To Sqr(p)
          If p % q = 0
            divide = 1
            Break
          EndIf
        Next q
        If divide = 0
          WriteString(0, Str(p) + " ")
        EndIf
      Next p
      CloseFile(0)
  EndIf

  PrintN("Ready. Calculated prime numbers are stored in Prime_numbers.txt file")
 
  If ReadFile(0, #file$)
      ReadStringFormat(0) ; <- skip BOM
      t$ = ReadString(0)
      PrintN(t$) 
      CloseFile(0)
  EndIf
 
  Print("Press any key to exit...")
  Repeat
  Until Inkey() <> ""
  CloseConsole()
EndIf
End
Or if the editor can switch mode and you know your input is ASCII, just manually instruct it to change mode.

Re: Console question

Posted: Mon Aug 19, 2013 5:56 pm
by krzysztof
Thank all of You very much for extended help ad support!