file size error - urgent

Discussion in 'C Programming' started by Imran, Aug 10, 2004.

  1. Imran

    Imran Guest

    Hello All

    Am trying to read a huge txt file and write to a file back.

    pls see the code

    int main()
    {
    char* pData;

    //write to some file

    delete pData;

    }
    bool GetData( char* &pData)
    {



    char pTempFileLocation="C:\\temp\\test.txt";


    //read back the data from the temp file
    FILE *fp = fopen(pTempFileLocation,"r+t");


    //This Code is to find the size of the file
    //simplest way
    fseek(fp,0L,SEEK_END);

    //now file poitner in the last poisition.
    //so pos will have the size :)
    long pos = ftell(fp);

    //move the file pointer back to begin
    //for reading..
    fseek(fp,0L,SEEK_SET);*/

    //allocate the data with the pos
    pData = new char[pos];

    //Just Inializing the pData to null
    //for safe side initialization
    memset(pData,0,pos);

    //read the data back and update the passes variable
    if(fp)
    fread(pData, sizeof( char ),pos,fp);
    else
    // file not found
    return false;
    fclose(fp);

    return true;
    }


    The problem is, I am getting the filesize correctly in the pos var. And am
    allocatiin the pData correctly.
    but while writing back to new file, it writes some more junk characters.Y it
    is so.

    File size is > 3MB.

    THanks in Adv
    Imran, Aug 10, 2004
    #1
    1. Advertising

  2. Imran wrote on 10/08/04 :
    >
    > Am trying to read a huge txt file and write to a file back.
    >
    > pls see the code
    >
    > int main()
    > {
    > char* pData;
    >
    > //write to some file
    >
    > delete pData;


    Not a C word.

    you want

    news:comp.lang.c++

    >
    > }
    > bool GetData( char* &pData)


    I confirm...

    Please learn to make the difference between C and C++. Ther are
    different languages.

    BTW, The 'urgent' word may have an unexpected side effect...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
    Emmanuel Delahaye, Aug 10, 2004
    #2
    1. Advertising

  3. Imran <> spoke thus:

    > bool GetData( char* &pData)


    You're either using a C++ compiler or you didn't post your real code.
    Either way, you have a problem WRT this newsgroup.

    > {
    > char pTempFileLocation="C:\\temp\\test.txt";


    Clearly not your real code. Do post it, pray.

    > //read back the data from the temp file


    And you seem to be using a C++ compiler...

    > //allocate the data with the pos
    > pData = new char[pos];


    Yep. Get thee to comp.lang.c++!

    > The problem is, I am getting the filesize correctly in the pos var. And am
    > allocatiin the pData correctly.
    > but while writing back to new file, it writes some more junk characters.Y it
    > is so.


    Perhaps you'd like to post the code where you do that? I bet you're
    missing a '\0' on the end of the string you write to the file.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Aug 10, 2004
    #3
  4. Imran

    SM Ryan Guest

    # //read back the data from the temp file
    # FILE *fp = fopen(pTempFileLocation,"r+t");

    Why not do something like
    if (!fp) perrror(pTempFileLocation);
    so that if the fopen is failing, you can get some error message.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    No pleasure, no rapture, no exquiste sin greater than central air.
    SM Ryan, Aug 11, 2004
    #4
  5. Imran wrote:
    [...]
    > FILE *fp = fopen(pTempFileLocation,"r+t");
    >
    > //This Code is to find the size of the file
    > //simplest way
    > fseek(fp,0L,SEEK_END);
    >
    > //now file poitner in the last poisition.
    > //so pos will have the size :)
    > long pos = ftell(fp);

    [...]

    Well, ignoring the fact that you appear to have C++ code, and not C, my
    understanding is that on a text file (as noted by your "r+t" on fopen),
    the number of bytes gotten with fread() is not necessarily the same as
    the poisition of EOF. (In fact, under DOS/Windows, you are virtually
    guaranteed that fread will get less bytes than your fseek/ftell combo
    tells you are there.) This is because end-of-line will be returned as
    a single '\n' character even though it may be stored on disk as more
    than a single byte.

    For example, a text file on Windows containing a single line of the
    word "foobar" will contain 8 or 9 bytes according to fstat or the
    fseek/ftell combo.

    It will either be:

    66 6f 6f 62 61 72 0d 0a

    or

    66 6f 6f 62 61 72 0d 0a 1a

    In either case, fread will only get 7 characters:

    66 6f 6f 62 61 72 0a

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Kenneth Brody, Aug 11, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tiewknvc9
    Replies:
    6
    Views:
    660
    Chris Uppal
    Oct 1, 2006
  2. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,564
    Michael Jung
    May 25, 2008
  3. Keith Thompson

    Re: File Size - Big File Size

    Keith Thompson, Oct 1, 2009, in forum: C Programming
    Replies:
    6
    Views:
    290
    Phil Carmody
    Oct 3, 2009
  4. Michael Tsang

    Re: File Size - Big File Size

    Michael Tsang, Oct 4, 2009, in forum: C Programming
    Replies:
    2
    Views:
    319
    Keith Thompson
    Oct 4, 2009
  5. Nobody

    Re: File Size - Big File Size

    Nobody, Oct 5, 2009, in forum: C Programming
    Replies:
    10
    Views:
    2,025
    Flash Gordon
    Oct 10, 2009
Loading...

Share This Page