ISBN Verification Code 7-118-01984-4 pay attention to the difference between '4' and 4, use the length of fgets (STR, length, FP) with caution, or fcanf (FP, "% s limit

Source: Internet
Author: User
# Include <stdio. h>
# Include <stdlib. h>

Int Main ()
{
Char Table [ 11 ] = " 0123456789x " ;
Char STR [ 13 ];
Int I, j = 10 , Sum = 0 ;

File * FP1, * fp2;
If (FP1 = fopen ( " In.txt " , " R " ) = NULL ){
Printf ( " Cocould not open file! \ N " );
Exit ( 1 );
}

Fscanf (FP1," % S " , STR );
// Fgets (STR, 14, FP1); note that the length cannot be 13, because STR [13] is not '\ 0', it is better to use fscanf and fprintf.
Fclose (FP1 );

For (I = 0 ; I < 11 ; I ++ ){
If (STR [I]! = ' - ' ){
Sum + = (STR [I]- ' 0 ' ) * J --;
}
}

Sum % = 11 ;
Sum = 11 -Sum;

If (Fp2 = fopen ( " Out.txt " , " W " ) = NULL ){
Printf ( " Cocould not open file! \ N " );
Exit ( 1 );
}

If (Table [Sum] = STR [ 12 ]) { // Convert table 4 to '4' and then compare
Fprintf (fp2, " % S " , " Right " );
Printf ( " Right \ n " );
} Else {
STR [ 12 ] = Table [Sum];
Fprintf (fp2, " % S " , STR );
Printf (" % S \ n " , STR );
}

Fclose (fp2 );

Getchar ();
Return 0 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.