2016-06-01 16 views
2

Ich habe eine Elf-Datei, die Beispiel aufgerufen. Ich schrieb folgenden Code, der den Inhalt der Beispieldatei im binären Modus liest und dann wollte ich ihren Inhalt in einer anderen Datei namens example.binary speichern. Aber wenn ich das folgende Programm ausführe, zeigt es mir einen Segmentierungsfehler an. Was ist falsch an diesem Programm? Ich kann meinen Fehler nicht herausfinden.Lesen von 2 Byte zu einer Zeit aus einer Binärdatei

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

// typedef macro 
typedef char* __string; 

//Function prototypes 
void readFileToMachine(__string arg_path); 


int main(int argc, char *argv[]) { 

    __string pathBinaryFile; 

    if(argc != 2){ 
     printf("Usage : ./program file.\n"); 
     exit(1); 
    } 

    pathBinaryFile = argv[1]; 

    readFileToMachine(pathBinaryFile); 

    return EXIT_SUCCESS; 
} 

void readFileToMachine(__string arg_path){ 

    int ch; 
    __string pathInputFile = arg_path; 
    __string pathOutputFile = strcat(pathInputFile, ".binary"); 

    FILE *inputFile = fopen(pathInputFile, "rb"); 
    FILE *outputFile = fopen(pathOutputFile, "wb"); 

    ch = getc(inputFile); 

    while (ch != EOF){ 
     fprintf(outputFile, "%x" , ch); 
     ch = getc(inputFile); 
    } 

    fclose(inputFile); 
    fclose(outputFile); 

} 
+3

'strcat typedef (pathInputFile, ".binary"); 'überschreibt Speicher, der von Ihrem Programm nicht zugewiesen wurde. –

+0

Also, wie kann ich es reparieren? – user3646905

+2

@ user3646905 Lesen Sie ein gutes C-Lehrbuch oder folgen Sie einem Tutorial. Erfahren Sie mehr über Zeiger. –

Antwort

0

Änderung Ihrer typedef char * __charptr

void rw_binaryfile(__charptr arg_path){ 

    FILE *inputFile; 
    FILE *outputFile; 

    __charptr extension = ".binary"; 
    __charptr pathOutputFile = strdup(arg_path); 

    if (pathOutputFile != NULL){ 
     pathOutputFile = realloc(pathOutputFile, strlen(arg_path) + sizeof(extension)); 

     if (pathOutputFile != NULL){ 

      pathOutputFile = strcat(pathOutputFile, ".binary"); 

      inputFile = fopen(arg_path, "rb"); 
      outputFile = fopen(pathOutputFile, "wb"); 

      write_file(inputFile, outputFile); 

      } 
    } 
} 

void write_file(FILE *read, FILE *write){ 
    int ch; 
    ch = getc(read); 
    while (ch != EOF){ 
     fprintf(write, "%x" , ch); 
     ch = getc(read); 
    } 
} 
+0

Ihr Code ist [Undefined Behavior] (https://en.wikipedia.org/wiki/Undefined_behavior) Sie konnten mir nicht kopieren ....;) – LPs

3

Sie haben keinen Raum extention auf Weg zu verketten, so dass Sie Raum für das schaffen haben.

Eine Lösung könnte sein:

char ext[] = ".binary"; 
pathOutputFile = strdup(arg_path); 
if (pathOutputFile != NULL) 
{ 
    pathOutputFile = realloc(pathOutputFile, strlen(arg_path) + sizeof(ext)); 
    if (pathOutputFile != NULL) 
    { 
     pathOutputFile = strcat(pathInputFile, ext); 


     // YOUR STUFF 
    } 

    free(pathOutputFile); 
} 

Randbemerkung: typedef ein Zeiger keine gute Idee ist ...

Verwandte Themen