ASCII code goes into minus

This is a discussion on ASCII code goes into minus within the C Programming forums, part of the General Programming Boards category; i want to read a binary file character by character and want to get ASCII code for each of these ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    ASCII code goes into minus

    i want to read a binary file character by character and want to get ASCII code for each of these characters. but when i display the ASCII code, most of the values are in minus. is there any way to not get the ASCII numbers in minus or am i perhaps doing something wrong?

    thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Perhaps you are, but without seeing some code, who knows what the exact solution would be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    6
    Here is the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
      FILE * InputFile;
      FILE * OutputFile;
      char c;
      char fc[3];
      
      InputFile = fopen("Test.mp3","rb");
      OutputFile = fopen("Test.txt","wb");
      if (InputFile == NULL) {
        perror ("Error while opening input file");
      } else if (OutputFile == NULL) {
        perror ("Error while creating output file");
      } else {
        while (!feof(InputFile)) {
          c = fgetc (InputFile);
          sprintf(fc,"%.3d",c); // 3-digit ASCII code, as 005, 068
          fputs(fc,OutputFile); // putting string fc into the file
         }
      }
      fclose(InputFile);
      fclose(OutputFile);
      printf("Done.\n");
      return 0;
      
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    Make c an int.
    Make fc a larger array, 3 isn't enough to hold 3 chars and a \0
    See the FAQ about using feof() in a control loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    6
    Thanks, repaired and working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. covert ascii code to character
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 09:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21