自己写的一个不能正常运行的程序,请高手指教。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
const char LIST[] = "0123456789abcdefghijklmnopqrstuvwxyz";
int main(int argc, char *argv[])
{
char buffer[100];
unsigned int count = 0;
unsigned int j,i,c;
int fd;
fd = open(argv[1],O_RDONLY,S_IRUSR|S_IWUSR);
while((read(fd,&c,1) )) {
if ((c >= '0') && (c <= '9'))
buffer[count++] = LIST[c-'0'];
else if ((c >= 'a') && (c <= 'z'))
buffer[count++] = LIST[c-'a'+10];
else if ((c >= 'A') && (c <= 'Z'))
buffer[count++] = c;
else if (c == EOF) {
for (i = 0; i <= count; i++)
printf("%02x ",buffer[i]);
for (i = 0; i <= (48-count*3); i++)
printf(" ");
printf("|| ");
buffer[count] = '\n';
for (i = 0; i <= count; i++)
printf("%c",buffer[i]);
printf("\n");
return 0;
}
else {
if (count <= 3)
count = 0;
else {
for (i = 0; i <= count; i++){
printf("%02x ",buffer[i]);
if (((i%16) == 15) || (i == count)){
for (j = 0; j <= (48-(i%16)*3); j++)
printf(" ");
printf("|| ");
for (j = (i-(i%16)); j <= i; j++)
printf("%c",buffer[j]);
printf("\n");
}
}
count = 0;
}
}
}
}
这是我在ubuntu下自己写的一个程序,目的是把以第一个命令行参数为名字的文件里的特定的十六进制表示出来,顺便把字符也打印出来。#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
const char LIST[] = "0123456789abcdefghijklmnopqrstuvwxyz";
int main(int argc, char *argv[])
{
char buffer[100];
unsigned int count = 0;
unsigned int j,i,c;
int fd;
fd = open(argv[1],O_RDONLY,S_IRUSR|S_IWUSR);
while((read(fd,&c,1) )) {
if ((c >= '0') && (c <= '9'))
buffer[count++] = LIST[c-'0'];
else if ((c >= 'a') && (c <= 'z'))
buffer[count++] = LIST[c-'a'+10];
else if ((c >= 'A') && (c <= 'Z'))
buffer[count++] = c;
else if (c == EOF) {
for (i = 0; i <= count; i++)
printf("%02x ",buffer[i]);
for (i = 0; i <= (48-count*3); i++)
printf(" ");
printf("|| ");
buffer[count] = '\n';
for (i = 0; i <= count; i++)
printf("%c",buffer[i]);
printf("\n");
return 0;
}
else {
if (count <= 3)
count = 0;
else {
for (i = 0; i <= count; i++){
printf("%02x ",buffer[i]);
if (((i%16) == 15) || (i == count)){
for (j = 0; j <= (48-(i%16)*3); j++)
printf(" ");
printf("|| ");
for (j = (i-(i%16)); j <= i; j++)
printf("%c",buffer[j]);
printf("\n");
}
}
count = 0;
}
}
}
}
可是在我修改了最后一个else之后就运行不了了。我想问题只能出在倒数最后一个else里的代码里了,但是也不一定。反正我是找不出哪里有错,编译也好好的,但运行完后程序什么也不显示就退出了。请高手指教。