Thread: header file compile error

  1. #1
    Unregistered
    Guest

    header file compile error

    As part of a project I have been provided with a header file that contains a function call. When I try to compile my program with this error file I get four error messages. Three are "conversion may lose significant digits in function search_file" and the last is a "cannot compile pre-compiled header: code in header" I assume the last one is caused by the previous three? As it is the weekend I can't contact my tutors so any advice would be appreciated. The complete header file is listed below with the lines shown by the compiler as causing the problem shown.
    #include "stdlib.h"
    #include "string.h"

    #define IDXSIZE sizeof(RECORD_IDX)
    #define RECSIZE sizeof(RECORD_DATA)

    typedef struct
    {
    char key[7];
    char desc[20];
    short supp_code;
    long free_stock;
    short min_stock;
    char movement_date[7];
    float price;
    } RECORD_DATA;


    typedef struct
    {
    char key[7];
    unsigned long rec_num;
    } RECORD_IDX;

    static RECORD_DATA *search_file(char *key);

    static RECORD_DATA *search_file(char *key)
    { /*THIS LINE IDENTIFIED AS CANNOT COMPILE PRE-COMPILED HEADER: CODE IN HEADER*/
    int compare(const void *a, const void *b);

    static RECORD_DATA record;
    static RECORD_IDX index,
    *idx_list,
    *entry_ptr;

    char path[80] = "STCKMAST.IDX";
    register long recs;

    FILE *fp_idx,
    *fp_dat;

    /* Open the index file, calculate the number of record entries */


    if((fp_idx = fopen(path, "rb"))!=NULL)
    {
    fseek(fp_idx, 0L, SEEK_END);
    if(ftell(fp_idx) > 2000)
    {
    fclose(fp_idx);
    fp_idx = NULL;
    }
    }

    if(fp_idx==NULL)
    {
    strcpy(path,"A:\\STCKMAST.IDX");
    if((fp_idx = fopen(path,"rb"))!=NULL)
    {
    fseek(fp_idx,0L,SEEK_END);
    if(ftell(fp_idx)>2000)
    {
    fclose(fp_idx);
    fp_idx = NULL;
    }
    }

    if(fp_idx == NULL)
    {
    printf("\nUnable to open file \"STCKMAST.IDX\" - \n");
    printf("\nPlease enter pathname for this file - eg ");
    printf("<C:\\PROJECT\\PROGS>\n\n>> ");
    fflush(stdin);
    fgets(path,80,stdin);
    fflush(stdin);
    if(path[strlen(path)-1]=='\n')
    path[strlen(path)-1] = 0;
    if(!*path)
    {
    printf("\nNo path entered, Program abandoned\n");
    exit(1);
    }
    else
    {
    if(path[strlen(path)-1] == '\\')
    strcat(path,"STCKMAST.IDX");
    else
    strcat(path,"\\STCKMAST.IDX");
    if((fp_idx = fopen(path,"rb"))==NULL)
    {
    printf("File not found on path %s\n",path);
    printf("Program abandoned\n\n");
    exit(1);
    }
    }
    }
    }

    fseek(fp_idx,0L,SEEK_END);
    recs = ftell(fp_idx)/IDXSIZE;

    /* Use malloc(), create a list */
    idx_list = (RECORD_IDX *)malloc(recs * IDXSIZE);/*CAUSES FIRST ERROR MSG*/
    rewind(fp_idx);
    fread(idx_list, IDXSIZE * recs, 1, fp_idx);/*CAUSES SECOND ERROR MSG*/
    fclose(fp_idx);


    strcpy(index.key, key);
    entry_ptr = (RECORD_IDX *) bsearch(&index, idx_list, recs, IDXSIZE, compare);
    /*LINE ABOVE CAUSES FINAL SIGNIFICANT DIGIT ERR MSG*/
    if(entry_ptr)
    index = *entry_ptr;
    else
    index.key[0] = '\0';
    free(idx_list);

    if( strcmp(index.key, "\0") )
    {
    strcpy(&path[strlen(path)-3],"DAT");
    if((fp_dat = fopen(path,"rb"))==NULL)
    {
    printf("\nUnable to open file \"STCKMAST.DAT\" - \n");
    printf("Program abandoned\n\n");
    exit(1);
    }
    fread(record.key,sizeof(char),1,fp_dat);
    fseek(fp_dat,0L,SEEK_END);
    if(record.key[0] > '9'|| ftell(fp_dat) > 6000)
    {
    printf("Incorrect data file loaded\n\n");
    printf("Program abandoned\n\n");
    exit(1);
    }
    fseek(fp_dat, RECSIZE * index.rec_num, SEEK_SET);
    fread(&record, RECSIZE, 1, fp_dat);
    if(ferror(fp_dat))
    {
    printf("Unable to access data file\n\n");
    printf("Program abandoned\n\n");
    exit(1);
    }
    fclose(fp_dat);
    return &record;
    }

    return NULL;
    }

    int compare(const void *a, const void *b)
    {
    return (strcmp ( ((RECORD_IDX *)a)->key,
    ((RECORD_IDX *)b)->key) );
    }

    Thanks

  2. #2
    Unregistered
    Guest

    amendment

    In the opening paragraph I mention running an "error file", sorry op error should be header file.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm not impressed by your tutors if they gave you this code....

    You do not put code in header files - period.

    In your case, you seem to be using vc++, which is the only compiler I know which uses pre-compiled headers.

    The short-term fix would be to turn off the pre-compiled headers feature in your compiler.

    > idx_list = (RECORD_IDX *)malloc(recs * IDXSIZE);/*CAUSES FIRST ERROR MSG*/
    Here are the types
    recs is long, and the parameter to malloc is size_t (which would seem to be an alias for unsigned long in your case).

    The easiest fix in all these cases is to write
    (size_t)recs

    Are you sure they're errors ( and not just warnings )

  4. #4
    Unregistered
    Guest

    messages

    Yes, my mistake, they are warnings and not errors but unless they are fixed I cannot compile my prog.
    Thanks for your advice.

  5. #5
    Unregistered
    Guest

    More help

    I changed the datatype of recs to size_t which removed two of the warnings, I then typecast the line "recs = ftell(fp_idx)/IDXSIZE;" so it is now "recs = (size_t)ftell(fp_idx)/IDXSIZE;" which removed the final significant digit warning. The code in header warning still remains. As I understand the last post this cannot be removed without transfering all the code to the main program? Is this the case or am I misunderstanding something?
    Does the above changes seem OK or should I be doing something different?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > without transfering all the code to the main program?
    It needs to be transferred to a .c file for sure

    Whether you add this to a single source file containing all your code, or a separate .c file containing just this code is up to you.

    Your other changes look OK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM