file size error - urgent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Imran

    #1

    file size error - urgent



    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 pTempFileLocati on="C:\\temp\\t est.txt";


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


    //This Code is to find the size of the file
    //simplest way
    fseek(fp,0L,SEE K_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,SEE K_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


  • Emmanuel Delahaye

    #2
    Re: file size error - urgent

    Imran wrote on 10/08/04 :[color=blue]
    >
    > 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;[/color]

    Not a C word.

    you want

    news:comp.lang. c++
    [color=blue]
    >
    > }
    > bool GetData( char* &pData)[/color]

    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"

    Comment

    • Christopher Benson-Manica

      #3
      Re: file size error - urgent

      Imran <imran.ar@in.bo sch.com> spoke thus:
      [color=blue]
      > bool GetData( char* &pData)[/color]

      You're either using a C++ compiler or you didn't post your real code.
      Either way, you have a problem WRT this newsgroup.
      [color=blue]
      > {
      > char pTempFileLocati on="C:\\temp\\t est.txt";[/color]

      Clearly not your real code. Do post it, pray.
      [color=blue]
      > //read back the data from the temp file[/color]

      And you seem to be using a C++ compiler...
      [color=blue]
      > //allocate the data with the pos
      > pData = new char[pos];[/color]

      Yep. Get thee to comp.lang.c++!
      [color=blue]
      > 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.[/color]

      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)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • SM Ryan

        #4
        Re: file size error - urgent

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

        Why not do something like
        if (!fp) perrror(pTempFi leLocation);
        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.

        Comment

        • Kenneth Brody

          #5
          Re: file size error - urgent

          Imran wrote:
          [...][color=blue]
          > FILE *fp = fopen(pTempFile Location,"r+t") ;
          >
          > //This Code is to find the size of the file
          > //simplest way
          > fseek(fp,0L,SEE K_END);
          >
          > //now file poitner in the last poisition.
          > //so pos will have the size :)
          > long pos = ftell(fp);[/color]
          [...]

          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> |
          +-------------------------+--------------------+-----------------------------+


          Comment

          Working...