gpt4 book ai didi

找不到段错误。在任何一种情况下都发生在第一次打印之前

转载 作者:太空宇宙 更新时间:2023-11-04 03:12:45 25 4
gpt4 key购买 nike

包括 main,然后是十六进制和二进制转储的实现,我主要关心的是 main。代码在 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”将文本更改为十六进制或二进制编码输出。

最佳答案

我清理了你的编译器警告。似乎代码现在按预期工作。将来,清理编译器警告并学习使用 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;
}

关于找不到段错误。在任何一种情况下都发生在第一次打印之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54775577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com