新手..谁能帮我解释下这个程序阿。
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define READ 0
#define WRITE 1
int handle_args(int argc, char* argv[])
{
if (argc == 2) {
return READ;
}
else if (argc == 3) {
return WRITE;
}
else {
printf("Useage: bv filename\n bv filename 01001010101000101\n");
exit(0);
}
}
void handle_read(char* filename) /*读*/
{
FILE *fp = fopen(filename, "rb");
int c;
int bit = 0x000000ff;
int i = 8;
char b[2048];
int j = 0;
int k;
fread(&c, 1, sizeof(char), fp);
while (!ferror(fp) && !feof(fp)) {
printf("%02x ", c & bit);
k = 8;
while (k-- > 0) {
if (c >> k & 0x00000001) {
b[j++] = '1';
}
else {
b[j++] = '0';
}
}
b[j++] = ' ';
if (--i == 0) {
b[j++] = 0;
printf("%s\n", b);
j = 0;
i = 8;
}
fread(&c, 1, sizeof(char), fp);
}
fclose(fp);
if (i) {
while (i--) {
printf(" ");
}
b[j] = 0;
printf("%s\n", b);
}
}
void handle_write(char* filename, char* str) /*写*/
{
int i;
char c = 0;
int b = 8;
FILE *fp = fopen(filename, "wb");
for (i=0; i<strlen(str); i++) {
if (str[i] == '0') {
c = c << 1;
b--;
}
else if (str[i] == '1') {
c = c << 1;
c += 1;
b--;
}
if (b == 0) {
fwrite(&c, 1, sizeof(c), fp);
c = 0;
b = 8;
}
}
if (b != 8) {
while (b--) {
c = c << 1;
}
fwrite(&c, 1, sizeof(c), fp);
}
fclose(fp);
}
int main(int argc, char* argv[])
{
int method;
if ((method = handle_args(argc, argv)) == READ) {
handle_read(argv[1]);
}
else {
handle_write(argv[1], argv[2]);
}
return 0;
}
哪位大侠帮我加下注释阿。看不懂T_T.自学第13天..