-1

I am right now reading in like this:

    while (fscanf(in, "%c", infile) != EOF) 
    {
    ch = *infile;
    count++;
    ascii[ch]++;
    }

And making my frequency table like this:

    void frequency ()
    {
    unsigned long long i;
    for (i = 0; i < 255; i++)
    {
     if (ascii[i] != 0)
     {
      uniqueLetters++;
      if (i < 33)
     {
    printf("=%llu\t%lu\n", i, ascii[i]);
      }
     else if (i > 126)
  {
    printf("=%llu\t%lu\n", i, ascii[i]);
  }
  else printf("%c\t%lu\n", (int)(i), ascii[i]);
}
   } 
  printf("unique letters: %lu\n", uniqueLetters);
  }

(This is for a huffman encoding project and when I try to read in an entire file I completely miss anything above 126...)

user3366369
  • 13
  • 1
  • 8
  • **Do not `scanf()`.** It's horrible. Its usage is unintuitive, it has all sorts of subtle sources of error built in its behavior. Use `fgetc()` instead. – The Paramagnetic Croissant May 07 '14 at 14:58
  • Well, are you involving any types that don't go above 127? `char`, for instance, which might be `signed char` in your system? – Jon May 07 '14 at 14:59
  • I am using unsigned char because I have anything from 0-255 – user3366369 May 07 '14 at 15:07
  • Unrelated note: The organization of your code could use some work. Pick an indentation style and stick with it rigidly, as though lives are at stake. http://en.wikipedia.org/wiki/Indent_style – Chris Baker May 07 '14 at 17:57
  • I know how to organize it the format just gets wonky when I copy and past it onto this site – user3366369 May 07 '14 at 18:01

1 Answers1

1

Try fgetc:

FILE * fp = fopen(filename, "r");

int ch; // return type of fgetc is int
while ((ch = fgetc(fp)) != EOF)
    ascii[ch]++;
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • Thank you for your answer but that didn't work either – user3366369 May 07 '14 at 15:11
  • 1
    @user3366369 do you have any more details on why it didn't work? What were you expecting? What did you see? – R Sahu May 07 '14 at 16:05
  • When I read in from a file I get all characters except for characters above 126...it ends with getting a 'z' when there are 3 other characters after...for some reason it doesn't recognize things greater than 126 – user3366369 May 07 '14 at 16:09