Operating System Used:Ubuntu 11.04
Compiler Used: GCC

Program related files:Git Hub Link

I'm trying a implement program which will do a job, same as that of CPP (The C preprocessor) when I compile a .c file.

In this particular code Copy_file_to_buf function not copying the whole file into the buffer.
Acutal size of the is 117406C,but ftell in the copy_file_to_buf function showing it as 114689.

puts("Copying dummyfile contents");
test_buf=Copy_file_to_buf("dummyfile");
puts(test_buf);

Filename: preprocessor.c

#include"myheader.h"

/*   agrv[1]=preprocessor
*    argv[2]=test.c
*
*   Program on PREPROCESSOR
*
*   Steps:
* 1.Removal of comments.
* 2.Inclusion of headerfiles.
* 3.Macro substitution.
*     a.function like arguments
*     b.Stringification
*     c.Concatenation
* 4.Conditional compilation
*     a.#Ifdef
*     b.#If
*     c.#defined
*     d.#Else
*     e.#Elif

*/


int
main(int argc, char *argv[])
{
   char *source_buf,*subBuf,*rmc_buf,*test_buf;
   char **main_header_names,**sub_header_names;
   int main_header_count,sub_header_count;

    source_buf=(char *)Copy_file_to_buf(argv[1]);//...
    rmc_buf=removeComments(source_buf);//...
    main_header_names=(char **)getMainHeaderNames(rmc_buf);//...
    source_buf=(char *)Copy_file_to_buf(argv[1]);//...
    rmc_buf=removeComments(source_buf);//...
    main_header_count=mainHeaderCounter(rmc_buf);//...
    printf("Main Header Count=%d",main_header_count);//...
    includeHeaders(main_header_names,main_header_count);

    subBuf=(char *)Copy_file_to_buf("pre.i");//...
    sub_header_names=(char **)getSubHeadersNames(subBuf);//...

    subBuf=(char *)Copy_file_to_buf("pre.i");//...
    sub_header_count=subHeadersCounter(subBuf);//...

    WriteSubHeadersToFile(sub_header_count,sub_header_names,"dummyfile");//...

    puts("Copying dummyfile contents");

    test_buf=Copy_file_to_buf("dummyfile");
    puts(test_buf);

    /*test_buf=removeComments(test_buf);
    puts(test_buf);
    sub_header_names=(char **)getSubHeadersNames(test_buf);
    test_buf=(char *)Copy_file_to_buf("dummyfile");
    sub_header_count=subHeadersCounter(test_buf);

    WriteSubHeadersToFile(sub_header_count,sub_header_names,"dummyfile2");
    printf("Line:%d   File:%s",__LINE__,__FILE__);
    */

return 0;
}

Filename:CopyFile.c

#include"myheader.h"

//Copying input file data into source_buf

char * Copy_file_to_buf(char *fileName)
{
    FILE *inputFile;
    int sizeofFile;
    char *source_buf;
    puts("In Copy_file_to_buf");

    inputFile=fopen(fileName,"r");
    fseek(inputFile,0,2);
    sizeofFile=ftell(inputFile);
    sizeofFile++;
    fseek(inputFile,0,0);
    source_buf=calloc(1,sizeofFile);  
    fread(source_buf,sizeofFile,1,inputFile);
    printf("SIZE OF THE FILE=%d",sizeofFile);
    //sleep(5);
    fclose(inputFile);

    return source_buf;
}

EDIT:
There is no data loss when I copied the contents of dummyfile to a buffer using same program but I've written copy_file_to_buf program seperately in temp.c file.

temp.c

#include<stdio.h>
main(int argc,char **argv)
{
    FILE *inputFile;
    int sizeofFile, rc;
    char *source_buf;
    fprintf(stderr, "In Copy_file_to_buf\n");
    sleep(1);

    inputFile=fopen(argv[1],"r");
    if (!inputFile) {
         fprintf(stderr, "Oops, failed to open inputfile \"%s\"\n", argv[1] );
         return NULL;
         }

    fseek(inputFile,0,SEEK_END);
    sizeofFile=ftell(inputFile);

    fseek(inputFile,0,SEEK_SET);
    source_buf=calloc(1,1+sizeofFile);
    rc = fread(source_buf,sizeofFile,1,inputFile);
    /* now check rc */
    fprintf(stderr, "Size of the file=%d; fread returned %d.\n", sizeofFile, rc);
    //sleep(5);
    fclose(inputFile);
    source_buf[sizeofFile] = 0;
    puts(source_buf);
    return source_buf;
}

Looks like the fseek() and ftell() aren't working as expected for the above( copyfFile.c) code.

Edited 4 Years Ago by slashqb

1) I don't see where you are displaying the size of the buffer, only the size of the file from ftell().
2) The calculated size of the file from the copy function is lost so you really have no idea how big the buffer is.
3) The buffer you allocated is never free'd causing a memory leak.
4) The fseek() calls are different. I understand they should mean the same thing, but you want to test oranges to oranges, not oranges to tangerines.

Thanks for the reply.

1) I don't see where you are displaying the size of the buffer, only the size of the file from ftell().

The return value of the ftell() is the size of the buffer(size of the buffer), right?

I've even tried fstat() and lseek(), but the results were same.

2) The calculated size of the file from the copy function is lost so you really have no idea how big the buffer is.

size of the file is lost? I've assigned the ftell() 's return value to sizeofFIle.

3) The buffer you allocated is never free'd causing a memory leak.

I've intentionally did not free'd.(I wanted to use same buffer for future purpose)

4) The fseek() calls are different. I understand they should mean the same thing, but you want to test oranges to oranges, not oranges to tangerines.
what do you mean by fseek() calls are different? This is the first time I'm hearing it from you.

Could you please elaborate it more. I still don't understand the mistake in my code.(Is it because of copying large file to buffer?)

This article has been dead for over six months. Start a new discussion instead.