[Gambas-user] C Code character manipulation - alternatives
Doriano Blengino
doriano.blengino at ...1909...
Thu May 21 08:39:45 CEST 2009
KhurramM ha scritto:
> Thanks Sir.
>
> You have given me tips better then the book I am following on C. :working:
>
> My two queries:
> 1> Can I implement ctrl+D or ^D, which I am not able to here, as in C?
> 2> The program works with INPUT and LINE INPUT. But Line Input also takes
> blank space as input, while INPUT ends the program over here. And I use
> string here, but in C it was integer?
>
> --------------------------------------------------- Just for reference:
> Well I made my program final as:
>
> PUBLIC SUB Main()
>
> ' Declare AND initialize variables.
> DIM c, count AS Integer
>
> DIM c1 AS String
>
> ' READ , PRINT , AND count characters.
> PRINT "Enter characters (or Press Carriage Return to quit): "
> LINE INPUT c1
> count += Len(c1)
>
> WHILE (c1 <> "") ' This is the best solution
> PRINT c1
> LINE INPUT c1
> count += Len(c1)
> WEND
>
> ' PRINT the number OF characters printed.
> PRINT count & " characters printed."
>
> END
>
I put the following code in a program; the code is invoked by pressing
Button1:
PUBLIC SUB Button1_Click()
' Declare AND initialize variables.
DIM c, count AS Integer
DIM c1 AS String
' READ , PRINT , AND count characters.
PRINT "Enter characters (or Press Carriage Return to quit): "
WHILE NOT Eof
LINE INPUT c1
PRINT c1
count += Len(c1)
WEND
' PRINT the number OF characters printed.
PRINT count & " characters printed."
END
Running the program inside the IDE, and using the internal console, no
EOF is generated, but your version (which tested for empty lines)
worked. Then I made an executable (Project menu -> Generate ->
Executable) and launched it from a terminal. It runs correctly; you
repeatedly type some text + ENTER, or you type Ctrl-D. The behaviour is
correct and, as I was supposing, no Ctrl-D is detected inside the line
input, but it is if you type ^D alone. As the routine is invoked every
time Button1 is pressed, I tried to press Button1 again: after the first
^D no more characters are detected. This makes sense, because you cannot
go past the EOF...
Then I launched the following shell command: echo "ciao" |./dand.gambas
Doing so, the standard input for the program is redirected elsewhere
(the output of the echo command). The program works as expected - it
counts 4 characters and senses the EOF correctly.
Now, I suppose you are disappointed by INPUT and LINE INPUT, which are
multi-char statements. If you want to process single characters, then
you must use the READ statement (perhaps there is also a INKEY$) -- see
the docs. Anyway, gambas does not have chars like C - you can use
strings (of length 1); the ASC() function returns the numerical code of
a character, and CHR() returns a char (a string of len 1) by giving its
ascii code. There is no character to designate EOF - you must use the
Eof() function.
If you want to be a real gambas programmer, stop to put parenthesis
around tests, like "while (c1<>...)"! :-)
If you have a goal in mind, perhaps could be better you tell it, so we
can help you more precisely.
Cheers,
--
Doriano Blengino
"Listen twice before you speak.
This is why we have two ears, but only one mouth."
More information about the User
mailing list