[Gambas-user] C Code character manipulation - alternatives

Doriano Blengino doriano.blengino at ...1909...
Wed May 20 15:17:05 CEST 2009


KhurramM ha scritto:
> The Code:
>
> PUBLIC SUB Main()
>
>    ' Declare AND initialize variables.
>    DIM c, count AS Integer
>    ' The values are initialized at Zero, by above command
>    
>    DIM c1 AS String
>  
>    ' READ , PRINT , AND count characters.
>    PRINT "Enter characters (Press Carriage Return Twice to quit): "
>    LINE INPUT c1
>    PRINT c1
>    count += Len(c1)
>    WHILE NOT (c1 = Eof)
>
>       LINE INPUT c1
>       count += Len(c1)
>       PRINT c1
>    WEND 
>
>    ' PRINT the number OF characters printed.
>    PRINT count & " characters printed."
>    
> END
>
> Gives me output:
>
> Enter characters (Press Carriage Return Twice to quit): 
> 657uyguig
> 657uyguig
>    uyt876896
>    uyt876896
>
>
>
> 21 characters printed.
>
> Is it OKAY, should I leave it here?
>
> I still dont get the feel of the C code here. I cant use ^Z to end the
> program and print output. :confused:
>   
Gambas and C are two very different beasts.

Looking at your source, I can say the following:

- LINE INPUT. This reads from stdin an entire line of text, terminated 
by an LF. It is different from INPUT because it reads a whole line, 
while INPUT tries to match number and words. It returns a string, which 
is different from C strings.

- WHILE NOT (c1 = Eof). Gambas does not require to embed tests in 
parenthesis. Better to write "while c1<>eof". Then, the EOF (end of 
file) is not a character, but a condition of a stream. A stream (or a 
file) is in EOF condition when there is no more data left to read. Sorry 
for this, but to see this as a character is a C stupid thing; it works, 
but it is inelegant. In the source you wrote "Press enter twice to 
quit", so I would test for an empty string: while c1<>"". Anyway, you 
can test for the end-of-file condition using Eof(), like you did. The 
Eof() is a function which tells if a stream is at end. It returns a 
boolean (true/false) value, and can not be compared to a string. If 
gambas does not gives you an error, then it is its fault. Gambas does 
not require parenthesis to invoke functions, so writing "Eof" is the 
same as writing "Eof()". If Eof is invoked with a parameter, like 
Eof(myfile), it tests the indicated stream; if invoked without 
parameters, it tests the standard input. I am not sure how it couples 
with input/line input; you could also try "while not eof" instead of 
"while c1<>Eof".

The original C source you provided was not totally correct, and so is 
not id gambas descendant.
Perhaps a better way to write the program could be:

   PRINT "Enter characters (Press Carriage Return Twice to quit): "
   do
     LINE INPUT c1
     if c1="" then break

     PRINT c1
     count += Len(c1)
   loop

   PRINT count & " characters printed."

This assumes to enter a null line to terminate. May be this also works 
(testing for eof):

   PRINT "Enter characters (press ^D to terminate): "
   while not eof
     LINE INPUT c1
     PRINT c1
     count += Len(c1)
   wend

   PRINT count & " characters printed."


First note that on unix, the eof character is not Ctrl-Z but Ctrl-D. 
After that, I don't know how ^D works when coupled with LINE INPUT, 
which reads a full line. I did'nt test, but I suspect that you have to 
press ^D and Enter; it could work, but it could not... it depends on how 
LINE INPUT is implemented.

Hope this helps - ask more if you need.

Best regards,
Doriano







More information about the User mailing list