包含的是主体,然后执行十六进制和二进制转储,我主要关心的是主体。代码在C中。它编译没有错误。我被告知要添加更多详细信息,因为该帖子主要是代码,但我想我的意思是正确的。我正在寻找导致细分错误的原因。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xbd.h"
#include "xbd.c"
int main(int argc, char *argv[]){
int i;
int counter = 0;
char *addy;
char buffer[16];
//If the user wishes for binary output command arg 2 will be '-b'
if(strcmp(argv[1], "-b") == 0){
FILE *f = fopen(argv[2], "r");
addy = (char*) f;
//Check for valid file
if(f == NULL){
printf("Error: File Empty.\n");
return(-1);
}
//print starting address, faults before this print
printf("%p: ", (void*)&f);
while((i = fgetc(f)) != EOF){
//While there are contents in the file, dump in binary groups of 6 chars
if(counter == 6){
//print human readable string here
printf(" ");
for(int i = 0; i < 6; i++){
printf("%s", buffer[i]);
}
printf("\n");
printf("%p: ", (void*)&addy);
counter = 0;
}
buffer[counter] = binForm(i);
counter++;
addy++;
}
fclose(f);
}
//If not binary, output will be in Hex
else{
FILE *f = fopen(argv[1], "r");
//addy = (char*) f;
//Check for valid file
if(f == NULL){
printf("Error: File Empty.\n");
return(-1);
}
//Print starting address, faults before this print
printf("%p: ", (void*)&f);
while((i = fgetc(f)) != EOF){
//While file has contents, dump in hex groups of 16 chars
if(counter == 16){
printf(" ");
//print human readable string here
for(int i = 0; i < 16; i++){
printf("%s", buffer[i]);
}
printf("\n");
printf("%p: ", (void*)&addy);
counter = 0;
}
if(counter%2 == 1){
buffer[counter] = hexForm(i);
printf(" ");
}
else{
buffer[counter] = hexForm(i);
}
counter++;
addy++;
}
fclose(f);
}
}
char hexForm(int current_byte){
//Print hex digits for one byte
printf("%X", current_byte);
//If unprintable, convert to '.'
if(current_byte < 33)
current_byte = 46;
return (char)current_byte;
}
char binForm(int current_byte){
//Print binary digits for one byte
while (current_byte) {
if (current_byte & 1)
printf("1");
else
printf("0");
current_byte >>= 1;
}
//If unprintable, convert to '.'
if(current_byte < 33)
current_byte = 46;
return (char)current_byte;
}
为什么会出错?我尝试注释掉一些早期的东西,看看是否可以通过,但是前几行代码会导致段错误。该程序的目的是读取文件,然后根据命令行参数“ -b”将文本更改为十六进制或二进制编码的输出。
答案 0 :(得分:1)
我清理了您的编译器警告。似乎代码现在可以正常工作了。将来,请清除您的编译器警告,并学习使用gdb
或其他调试器。
此外,在使用argv []之前,必须检查以确保在命令行上实际提供了足够的参数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char hexForm(int current_byte);
char binForm(int current_byte);
int main(int argc, char *argv[]){
int i;
int counter = 0;
char *addy;
char buffer[16];
if(argc < 2 ) /* the actual correct usage here is left up to OP */
{
printf("Invalid parameters\n")
return(0);
}
//If the user wishes for binary output command arg 2 will be '-b'
if(strcmp(argv[1], "-b") == 0){
FILE *f = fopen(argv[2], "r");
addy = (char*) f;
//Check for valid file
if(f == NULL){
printf("Error: File Empty.\n");
return(-1);
}
//print starting address, faults before this print
printf("%p: ", (void*)&f);
while((i = fgetc(f)) != EOF){
//While there are contents in the file, dump in binary groups of 6 chars
if(counter == 6){
//print human readable string here
printf(" ");
for(int i = 0; i < 6; i++){
printf("%c", buffer[i]);
}
printf("\n");
printf("%p: ", (void*)&addy);
counter = 0;
}
buffer[counter] = binForm(i);
counter++;
addy++;
}
fclose(f);
}
//If not binary, output will be in Hex
else{
FILE *f = fopen(argv[1], "r");
//addy = (char*) f;
//Check for valid file
if(f == NULL){
printf("Error: File Empty.\n");
return(-1);
}
//Print starting address, faults before this print
printf("%p: ", (void*)&f);
while((i = fgetc(f)) != EOF){
//While file has contents, dump in hex groups of 16 chars
if(counter == 16){
printf(" ");
//print human readable string here
for(int i = 0; i < 16; i++){
printf("%c", buffer[i]);
}
printf("\n");
printf("%p: ", (void*)&addy);
counter = 0;
}
if(counter%2 == 1){
buffer[counter] = hexForm(i);
printf(" ");
}
else{
buffer[counter] = hexForm(i);
}
counter++;
addy++;
}
fclose(f);
}
}
char hexForm(int current_byte){
//Print hex digits for one byte
printf("%X", current_byte);
//If unprintable, convert to '.'
if(current_byte < 33)
current_byte = 46;
return (char)current_byte;
}
char binForm(int current_byte){
//Print binary digits for one byte
while (current_byte) {
if (current_byte & 1)
printf("1");
else
printf("0");
current_byte >>= 1;
}
//If unprintable, convert to '.'
if(current_byte < 33)
current_byte = 46;
return (char)current_byte;
}