Check Bits for Ascii Files

Authors

Publication

Pub Details

Date

Pages

See all articles from QL Hacker's Journal 8

I’ve been scanning throught some old issues of Dr Dobb’s Journal trying to set the spark to a new programming idea. I was reading one article on architecture ( and / or gates, etc) and the word parity leaped from the page.

I had a sudden flash back to my college days where I was introduced to the concept of parity and check bits. My mind was mulling on what applications I could put parity and check bits to use.

It then dawned me that ascii characters are 7 bits, but stored in 8 bit bytes. In a true ascii file, the last bit is always set to 0 and wasted. Hey, I could use the 8th bit as a check bit. If a the 7 other bits total up to an even number the parity bit can be set to 1, else it would be set to 0. Neat idea, but what use would it have?

How about error checking when sending files bewteen computers via the serial port. After the transfer the file can be checked for parity errors. With disk systems there is always the chance that the drive will not read the disk properly and return a bad bit ( albeit a VERY small chance). Parity checking can keep you from suffering from the horror :-).

Anyhow, the program was a good mental and programming exercise. I prefer to write short programs that can be concieved, written, tested, and made running in a day.

The program has three functions: Add the check bits (A), Remove the check bits (R), and Check the check bits (C). The C option is used to check the file to make sure no errors have creeped in.

/* Check Bit for Ascii Files
This program takes a 7-bit ascii file and
converts the 8th bit to a check bit.
Even characters have the check bit set to
one and odd characters have it set to zero.

This program has three modes:
A : add the check bits to the file
R : remove the check bits and return file
to pure ascii
C : check file for errors using the check bits

*/

#include <stdio_h>

main()
{

char c, file1[30],file2[30];
int arg, check, errors, fd1, fd2;

check = 0;
errors = 0;

printf("Enter A, R, or C : ");
arg = getchar();
putchar(arg);

if ( arg == 'A' || arg == 'a' )
{ check = 'A'; }
if ( arg == 'R' || arg == 'r' )
{ check = 'R'; }
if ( arg == 'C' || arg == 'c' )
{ check = 'C'; }
if ( check == 0 )
{ printf("\nNot a Valid Character\n");
abort(-1);
}

printf("\nEnter Input File: ");
gets(file1);

fd1 = fopen(file1,"r");
if ( fd1 == NULL)
{ printf("\nCould Not Open File\n");
abort(-1);
}

if ( check == 'A' || check == 'R' )
{ printf("\nEnter Output File: ");
gets(file2);

fd2 = fopen(file2,"w");
if ( fd2 == NULL)
{ printf("\nCould Not Open File\n");
abort(-1);
}
}

while (( c = getc(fd1)) != EOF )
{ if ( check == 'A' )
{ if ( c >= 128 )
printf("\nIgnoring non-ascii character
#%d",c);
if ( c % 2 == 0) c = c+128;
}

if ( check == 'R' || check == 'C' )
{ if ( c > 128 )
{ c = c - 128;
if ( c % 2 != 0 ) errors++;
}
else if ( c % 2 == 0) errors++;
}

if ( check == 'A' || check == 'R' )
putc(c,fd2);
}

fclose(fd1);
if ( check == 'A' || check == 'R' )
fclose(fd2);

if ( check == 'C' || check == 'R' )
printf("\nTotal Errors = %d",errors);

}

Products

 

Downloadable Media

 

Image Gallery

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

Scroll to Top