Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 6101

Error writes wrong buffer to file using ftell and fseek (C)

$
0
0

I wrote a code that reading line by line from a text file and store the value in a variable called buffer using fgets, after reading each line I am sending the buffer to a function that suppose to replace the content of the buffer as described below, and then it gets written to a target text file.

function description (I am on ubuntu os)

I have a text file containing fake assembly code, the goal of the function is to save all macros defined in the file in a data structure and then coping the macro content to the target file whenever it used without the macro decleration.

for example: for source.as
it will delete the macro decleration

mcr m_mcr inc r2 mov A,r1endmcr 

and write the macro content in every place where the macro name is mentioned in the source file

..m_mcr..

after running the function the target file will look like this

.. inc r2 mov A,r1..

My code works well when using only one macro in the source.as but when defining two the second one is get copied with more gunk to it.

Can someone please help me figure out why it is heppening because I am debugging for hours.

here is the function that preformes the algorithm:

void processLine(char *buffer, FILE* source){    char* token = NULL;    char* copy_of_buffer = NULL;    static char* macro_name;    static long start_macro_position;    static long end_macro_position;    long last_position = 0;    long content_length = 0;    macroTable* macro;    size_t bytes_read = 0;    /* checking if copy_of_buffer allocated successfully */    copy_of_buffer = (char*)malloc(strlen(buffer) + 1);    FAIL_IF_MSG(copy_of_buffer == NULL, ERROR_ALLOCATION_FAILED);    strcpy(copy_of_buffer, buffer); /* copying the current line to not modify it */    token = strtok(copy_of_buffer, " \t\n\f\r");    /* checking if token got string (skipping empty lines) */    RETURN_IF(token == NULL);    /* checking if the is_mcr flag is on indicating that we are inside of a macro */    if (is_mcr) {        /* checking if we reached the end of the macro decleration */        if (strcmp(token, "endmcr") == 0)        {            end_macro_position = ftell(source) - strlen(buffer);            is_mcr = false;            /* adding new macro to the macro table */            insertMacro(macro_name, start_macro_position, end_macro_position);            free(macro_name);            /* reseting the macro positions to defualt values */            start_macro_position = 0;            end_macro_position = 0;        }        /* deleting the line content before writing it to the file */        memset(buffer, '\0', strlen(buffer));    }    else {        /* seacrhing if using a defined macro */        macro = lookupMacro(token);        /* if macro exists in the token name starting coping the content to .am file */        if (macro != NULL) {            last_position = ftell(source); /* saving the last position we read from */            fseek(source, macro->start_position, SEEK_SET); /* going to the start position of the macro in the file */            content_length = macro->end_position - macro->start_position; /* calulating the content length need to be copied */            if (MAX_LINE_LENGTH < content_length) {                /* allocating more memory */            } else {                /* deleting the macro name from the buffer */                memset(buffer, '\0', strlen(buffer));                /* reading the macro value to the buffer */                bytes_read = fread(buffer, 1, content_length, source);                /* checking if the content was read successfully */                if (bytes_read != content_length) {                    FAIL_IF_MSG(true, ERROR_READING_FROM_FILE);                }                /* returning the the last position we were */                fseek(source, last_position, SEEK_SET);            }        } else if (strcmp(token, "mcr") == 0) {            token = strtok(NULL, " \t\n\f\r");            FAIL_IF_MSG(token == NULL, DECLERING_MACRO_WITHOUT_NAME);            /* checking if copy_of_buffer allocated successfully */            macro_name = malloc(strlen(token) + 1); /* + 1 for null terminator */            FAIL_IF_MSG(macro_name == NULL, ERROR_ALLOCATION_FAILED);            strcpy(macro_name, token);            is_mcr = true;            start_macro_position = ftell(source);            /* deleting the line where the mcr decleration was found from the file */            memset(buffer, '\0', strlen(buffer));        }    }    free(copy_of_buffer);}

when running on file.as

.define sz = 2MAIN:   mov r3, LIST[sz]LOOP:   jmp L1    mcr m_mcr        cmp r3, #sz    bne END    endmcr    prn #-5    mov STR[5], STR[2]    sub r1, r4    m_mcrL1: inc K    bne LOOPEND:    hlt.define len = 4STR:    .string "abcdef"LIST: .data 6, -9, lenK:  .data 22

the generated file.am

.define sz = 2MAIN:   mov r3, LIST[sz]LOOP:   jmp L1    prn #-5    mov STR[5], STR[2]    sub r1, r4        cmp r3, #sz    bne ENDL1: inc K    bne LOOPEND:    hlt.define len = 4STR:    .string "abcdef"LIST: .data 6, -9, lenK:  .data 22

but when running on a file.as with two macros

.define sz = 2MAIN:   mov r3, LIST[sz]LOOP:   jmp L1    mcr m_mcr        cmp r3, #sz    bne END    endmcr    prn #-5    mcr new_mcr    mov r1, r3    endmcr    mov STR[5], STR[2]    sub r1, r4    m_mcr    new_mcrL1: inc K    bne LOOPEND:    hlt.define len = 4STR:    .string "abcdef"LIST: .data 6, -9, lenK:  .data 22

the generated file.am

.define sz = 2MAIN:   mov r3, LIST[sz]LOOP:   jmp L1    prn #-5    mov STR[5], STR[2]    sub r1, r4        cmp r3, #sz    bne END    mov r1, r3z    bne ENDL1: inc K    bne LOOPEND:    hlt.define len = 4STR:    .string "abcdef"LIST: .data 6, -9, lenK:  .data 22

this is the output I am getting when when printing the macros I saved, as you can see the information is correct

name: m_mcr, start: 62, end: 85, lengh: 23name: new_mcr, start: 115, end: 127, lengh: 12

Viewing all articles
Browse latest Browse all 6101

Trending Articles