Sams Teach Yourself C in 21 Days

(singke) #1
Secret Messages 307

51:
52: fclose(fh);
53: printf( “\n\nFile decoded to screen.\n” );
54: }
55: }
56: else /* assume coding to file. */
57: {
58:
59: fh = fopen(argv[1], “w”);
60: if( fh <= 0 )
61: {
62: printf( “\n\nError creating file...” );
63: rv = -3; /* set return value */
64: }
65: else
66: {
67: printf(“\n\nEnter text to be coded. “);
68: printf(“Enter a blank line to end.\n\n”);
69:
70: while( gets(buffer) != NULL )
71: {
72: if( buffer[0] == 0 )
73: break;
74:
75: for( ctr = 0; ctr < strlen(buffer); ctr++ )
76: {
77: ch = encode_character( buffer[ctr], val );
78: ch = fputc(ch, fh); /* write the character to file
*/
79: }
80: }
81: printf( “\n\nFile encoded to file.\n” );
82: fclose(fh);
83: }
84:
85: }
86: return (rv);
87: }
88:
89: int encode_character( int ch, int val )
90: {
91: ch = ch + val;
92: return (ch);
93: }
94:
95: int decode_character( int ch, int val )
96: {
97: ch = ch - val;
98: return (ch);
99: }

LISTINGT&R 4 continued

20 448201x-T&R4 8/13/02 11:12 AM Page 307

Free download pdf