www.digitalmars.com         C & C++   DMDScript  

D - No end in sight!

reply "Andrew Edwards" <crxace13 comcast.net> writes:
Can someone explain why this code does not count the last character of a
file? How would I go about correcting it?  If there is a better way of
doing it I would appreciate that advice also!

To Pavel:

    Will all the ctype functions be supported in stream.d? I couldn't use
both ctype and stream because the two conflict with each other.

Thanks in advance,
Andrew

import stream;
int main (char[][] args)
{
   int t_num    = 0;
   int t_ltr    = 0;
   int t_punct  = 0;
   int t_space  = 0;
   int t_char   = 0;

   File file = new File;
   file.open("edit1.txt");
   char ch;
   assert(file.readable);
   while(!file.eof())
   {
      file.scanf("%c", &ch);
      ++t_char;
      if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z"))
         { ++t_ltr; }
      else if (isdigit(ch))
         { ++t_num; }
      else if (iswhite(ch))
         { ++t_space; }
      else
         { ++t_punct; }
   }
   file.close();

   printf("There are %d numbers,\n", t_num);
   printf("%d letters, %d punctuation marks,\n", t_ltr, t_punct);
   printf("and %d spaces for a total of %d characters.\n", t_space,
t_char);
   return 0;
}
Sep 16 2002
parent "OddesE" <OddesE_XYZ hotmail.com> writes:
"Andrew Edwards" <crxace13 comcast.net> wrote in message
news:am5eqf$2u0n$1 digitaldaemon.com...
 Can someone explain why this code does not count the last character of a
 file? How would I go about correcting it?  If there is a better way of
 doing it I would appreciate that advice also!
<SNIP>
    File file = new File;
    file.open("edit1.txt");
    char ch;
    assert(file.readable);
    while(!file.eof())
You need to do a read on the file before eof() works? That is what causes this kind of trouble in C... -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
Oct 17 2002