In the code below, I want to remove the white space character from 2 places only, from the end of 1st line and from the end of last line. How can I optimize it ?Platform : Linux , gcc compiler    int remove_special_chars(char *file_path, char *dest_file, int flag){    PRINT_FUN_NAME    struct stat st;    unsigned char *fileData=NULL;    FILE *fp = NULL, *dest_file_fp = NULL, *intermidiate_fp = NULL;    char tmp_str[256] = {0}, str_buffer[100] = {0};    int file_size = 0, count = 0, read_len = 0;    /* I have taken 3 files here file_path is        the source file from which i am reading        data and intermidiate_path is the 2nd file       in which  iam storing data after removing        the space from 1st line. Then iam reding        the intermidiate file and removing the space       from last line and finally storing it into        3rd file i.e is dest_file. */                  if ((fp = fopen(file_path, r)) == NULL) {        DPRINTF(File Opening Failed!\n);        return -1;    }    if((intermidiate_fp = fopen(INTERMEDIATE_FILE_PATH, wb+)) == NULL) {        DPRINTF(File Opening Failed!\n);        return -1;    }    if((dest_file_fp = fopen(dest_file, wb)) == NULL) {        DPRINTF(File Opening Failed!\n);        return -1;    }    while(fgets(tmp_str, sizeof(tmp_str), fp) != NULL) {        DPRINTF(tmp_str : %s\n, tmp_str);        fputs(tmp_str, intermidiate_fp);        if(count == 0) {            rewind(intermidiate_fp);             strncpy(str_buffer, tmp_strstrlen(tmp_str));            str_buffer[strlen(str_buffer) - 1] = '\0';            DPRINTF(str_buffer : %s\n, str_buffer);            fputs(str_buffer, intermidiate_fp);            count ++;        }     }    rewind(intermidiate_fp);    stat(INTERMEDIATE_FILE_PATH, &st);    if (st.st_size == 0) {        return (-2);    }    file_size = st.st_size;    DPRINTF(File size : %d\n, file_size);    fileData = (unsigned char *)malloc(sizeof(char)*file_size);    if(fileData == NULL) {        DPRINTF(Unable to allocate size for fileData!\n);        return -1;    }    if ((read_len=fread(fileData, 1, file_size, intermidiate_fp)) == 0) {        DPRINTF(Reading Failed!\n);        free(fileData); fileData=NULL;        return (-3);    }    fileData[(file_size-1)] = '\0';    DPRINTF(File Data : %s\n, fileData);    fputs((char*)fileData, dest_file_fp);    fclose(dest_file_fp); fclose(fp);    fclose(intermidiate_fp);    return 0;}
Trimming whitespace from the beginning and ending of a file
optimization;c;linux