Thread: Problem while copying the contents of file to a buffer.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    32

    Problem while copying the contents of file to a buffer.

    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.

    Here in this particular code the Copy_file_to_buf fucntion not copying the whole file into the buffer.
    Code:
    puts("Copying dummyfile contents");
    
        Copy_file_to_buf("dummyfile");
            puts(test_buf);
    program related files:http://tinyurl.com/6mmzk8a
    (Unable to add attachments)

    filename: preprocessor.c
    Code:
    #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);
    
        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
    Code:
    #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;
    }
    Last edited by chibi.coder; 03-31-2012 at 01:19 AM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Looks basically ok to me. Are you seeing that some of the file is read in but not all of it? I did a quick test and saw the same behaviour. Trying to read more data got me an EPIPE error. On Windows at least the fix was to change the "r" to "rb" in the fopen call. Fread is meant to work with binary files.

    I think you have the byte-per-element and size of data structure the wrong way round in calloc(), fread() etc. It should be number of elements followed by size of element. Of course since these are size 1 it perhaps doesn't matter, but it might not be helping.

    Hope that gets you somewhere. If not, it'd be helpful to know:
    - the effect of putting the reads in a while loop until all the data has been read. Result could well be an infinite loop so suggest having a debugger connected
    - the return value of ferror after each fread call
    - the return value of fread (no. of elements read)
    - Does the program crash, or segfault?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Are you seeing that some of the file is read in but not all of it?
    Acutal size of the is 117406C
    But ftell in the copy_file_to_buf function showing it as 114689.

    - the return value of ferror after each fread call
    - the return value of fread (no. of elements read)
    fread_ret:0
    ferror_ret:0

    Does the program crash, or segfault?
    No.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is a reason that ftell() returns a long int. It is fairly routine for a file size to exceed what can be represented in an int.

    Also check the value of ftell() to see if an error occurred.

    With modern operating systems, file sizes can sometimes exceed what can be represented as a long. In that case, you will need to use system-specific functions to retrieve the file size ..... and allocating a buffer large enough to hold the contents may well fail. Although the technique is system dependent, you will probably be better off using memory-mapped files rather than trying to allocate a buffer large enough to hold a large file.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Code:
    inputFile=fopen(fileName,"r");    
    fseek(inputFile,0,2);
    sizeofFile=ftell(inputFile);
             perror("ftell():");    sizeofFile++;
    It is giving a 'perror' message as
    "ftell():No such file or directory."

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Hmmm. I'm stumped then! Wasn't expecting that. grumpy makes a good point about large files. I reckon you'd get out-of-heap malloc failures quite a time before you overflowed an int. Certainly you're not close to that yet with this particular file.

    Quote Originally Posted by chibi.coder
    fread_ret:0
    I guess it reads sizeofFile the first time then doesn't read any more after that. I expect if you manually bumped the sizeofFile value then it'd read it all out fine (which means the fread return value is probably not very interesting).

    Quote Originally Posted by chibi.coder
    "ftell():No such file or directory."
    Whaaat? No, I've really no idea what's going on here. Think it might be worth reiterating my suggestion to use binary mode ("rb" not "r") in your fopen call, as you pasted it again with just "r". Just saying in case you missed my original comment

    Hmmmm. Hopefully someone else will come along with some good ideas. If not, I guess I'd go hunting on the net for known bugs/limits of ftell on my OS/compiler. Maybe try to find out if it's specific to that file, or happens with any file of that sort of size.

    Sorry I can't help. Maybe best to pursue a more large-file-proof way anyway.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Think it might be worth reiterating my suggestion to use binary mode ("rb" not "r") in your fopen call, as you pasted it again with just "r". Just saying in case you missed my original comment
    I did try "rb" mode in fopen function and the results were same(forgot to mention it in earlier posts).

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by chibi.coder View Post
    I did try "rb" mode in fopen function and the results were same(forgot to mention it in earlier posts).
    If you are on a windows os then openmode "rb" should give you the right filesize.
    if "the results were same" means that perror retuns "No such file or directory" then I would print argv[1]. Calling your executable with the full path to the file could help too.
    Kurt
    EDIT:
    just noticed that your comments tell
    Code:
    /*   agrv[1]=preprocessor
    *    argv[2]=test.c
    but you call your function with argv[1]
    Last edited by ZuK; 03-31-2012 at 06:38 AM.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    bump.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-09-2011, 02:56 PM
  2. buffer has extra contents in gtk+ 2.0
    By MK27 in forum Linux Programming
    Replies: 5
    Last Post: 08-04-2008, 11:57 AM
  3. copying contents of array1 into array2
    By richdb in forum C Programming
    Replies: 7
    Last Post: 04-05-2006, 05:33 AM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM