CString->GetLength() does not work

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

    #1

    CString->GetLength() does not work

    Hi, I have a question for CString. My C++ code is detailed as below,

    FILE *fp1;
    fp1=fopen("F:\\ aa.dat","rb");
    if(!feof(fp1))
    {
    // Step 1 :the following code is to read binary data from an
    existing file
    CString ss;
    fread(ss.GetBuf fer(1428),1428, 1,fp1);

    // Step 2: the following code is to test if the read action is
    successful
    fclose(fp1);
    fp1 = fopen("F:\\bb.d at","wb+");
    fwrite(ss.GetBu ffer(),1428,1,f p1);
    fclose(fp1);
    // Step 3:the following code is to test the GetLength function!
    int i;
    i = ss.GetLength();
    }
    I find step 1 & 2 are all successful, but when I monitor the i value after
    setp 3, I found the value is 0, could any body tell me why?

    Thanks in advance!
    Joseph


  • Eberhard Schefold

    #2
    Re: CString->GetLengt h() does not work

    Joseph schrieb:
    I find step 1 & 2 are all successful, but when I monitor the i value after
    setp 3, I found the value is 0, could any body tell me why?
    When you're done writing to the string after GetBuffer, call
    ReleaseBuffer (or use CStrBuf instead). Note that you need GetBuffer
    only when you write to the CString, so here
    fwrite(ss.GetBu ffer(),1428,1,f p1);
    where you only read the CString contents, GetBuffer is unnecessary.

    Comment

    • Joseph

      #3
      Re: CString->GetLengt h() does not work

      Well, even I do not use fread to GetBuffer(),ie remove step 2 as I decribed
      in my previous mail, I still can not get the CString length by GetLength()
      method!

      Joseph

      "Eberhard Schefold" <eberhard.schef old@de.bosch.co m????
      news:e9nh53$4m7 $1@news4.fe.int ernet.bosch.com ...
      Joseph schrieb:
      >
      >I find step 1 & 2 are all successful, but when I monitor the i value
      >after setp 3, I found the value is 0, could any body tell me why?
      >
      When you're done writing to the string after GetBuffer, call ReleaseBuffer
      (or use CStrBuf instead). Note that you need GetBuffer only when you write
      to the CString, so here
      >
      fwrite(ss.GetBu ffer(),1428,1,f p1);
      >
      where you only read the CString contents, GetBuffer is unnecessary.

      Comment

      • Eberhard Schefold

        #4
        Re: CString-&gt;GetLengt h() does not work

        Joseph wrote:
        Well, even I do not use fread to GetBuffer(),ie remove step 2 as I decribed
        in my previous mail, I still can not get the CString length by GetLength()
        method!
        And did you include the call to ReleaseBuffer?

        Comment

        • Joseph

          #5
          Re: CString-&gt;GetLengt h() does not work

          Sorry , do I need to call the ReleaseBuffer() method before GetLength() , or
          I misunderstood your meaning?

          FILE *fp1;
          fp1=fopen("F:\\ aa.dat","rb");
          if(!feof(fp1))
          {
          // Step 1 :the following code is to read binary data from an
          existing file
          CString ss;
          fread(ss.GetBuf fer(1428),1428, 1,fp1);

          // Step 2: the following code is to test if the read action is
          successful
          fclose(fp1);
          fp1 = fopen("F:\\bb.d at","wb+");
          fwrite(ss,1428, 1,fp1);
          fclose(fp1);
          // Step 3:the following code is to test the GetLength function!
          int i;
          i = ss.GetLength();
          }
          I changed Step 2 by your request, do you mean I still need to add
          ReleaseBuffer() call between step 2 & 3, or what else?

          Joseph


          "Eberhard Schefold" <eberhard.schef old@de.bosch.co m????
          news:e9ni0v$59g $1@news4.fe.int ernet.bosch.com ...
          Joseph wrote:
          >
          >Well, even I do not use fread to GetBuffer(),ie remove step 2 as I
          >decribed in my previous mail, I still can not get the CString length by
          >GetLength() method!
          >
          And did you include the call to ReleaseBuffer?

          Comment

          • Eberhard Schefold

            #6
            Re: CString-&gt;GetLengt h() does not work

            Joseph wrote:
            Sorry , do I need to call the ReleaseBuffer() method before GetLength() , or
            I misunderstood your meaning?
            From the documentation:

            If you use the pointer returned by GetBuffer to change the string
            contents, you must call ReleaseBuffer before using any other
            CSimpleStringT member methods.

            Comment

            • David Wilkinson

              #7
              Re: CString-&gt;GetLengt h() does not work

              Joseph wrote:
              Sorry , do I need to call the ReleaseBuffer() method before GetLength() , or
              I misunderstood your meaning?
              [snip]

              Joseph:

              Yes! Whenever you use GetBuffer you should call ReleaseBuffer.

              David Wilkinson

              Comment

              • Joseph

                #8
                Re: CString-&gt;GetLengt h() does not work

                Thanks David & Eberhard

                I read the CString documents and I changed my code as below, but it still
                does not work! I do not know why!

                Line1: char *p1 = new char[1428];
                Line2: fread(p1,1428,1 ,fp1);
                Line3: CString str1(p1);
                Line4: str1.ReleaseBuf fer();
                Line5: int i;
                Line6: i = str1.GetLength( );

                // I found the value for i is 1 ,not 1428. Why?

                but if I change line 2 to the following line ,it can work properly, why?

                strcpy(p1,"ABCD EFG"); //not fread(p1,1428,1 ,fp1);


                Could anybody give me a help?Help?

                Thanks in advance!

                Joseph


                "David Wilkinson" <no-reply@effisols. com????
                news:%23qJCnL%2 3qGHA.516@TK2MS FTNGP05.phx.gbl ...
                Joseph wrote:
                >
                >Sorry , do I need to call the ReleaseBuffer() method before GetLength() ,
                >or I misunderstood your meaning?
                >
                [snip]
                >
                Joseph:
                >
                Yes! Whenever you use GetBuffer you should call ReleaseBuffer.
                >
                David Wilkinson

                Comment

                • Joseph

                  #9
                  Re: CString-&gt;GetLengt h() does not work

                  Well , I got the root cause , it is because the file I read from is a binary
                  format file, thanks to you all!

                  Joseph

                  "Joseph" <josndrsn@yahoo .comдÈëÏûÏ¢
                  news:%23D1JATJr GHA.4120@TK2MSF TNGP03.phx.gbl. ..
                  Thanks David & Eberhard
                  >
                  I read the CString documents and I changed my code as below, but it still
                  does not work! I do not know why!
                  >
                  Line1: char *p1 = new char[1428];
                  Line2: fread(p1,1428,1 ,fp1);
                  Line3: CString str1(p1);
                  Line4: str1.ReleaseBuf fer();
                  Line5: int i;
                  Line6: i = str1.GetLength( );
                  >
                  // I found the value for i is 1 ,not 1428. Why?
                  >
                  but if I change line 2 to the following line ,it can work properly, why?
                  >
                  strcpy(p1,"ABCD EFG"); //not fread(p1,1428,1 ,fp1);
                  >
                  >
                  Could anybody give me a help?Help?
                  >
                  Thanks in advance!
                  >
                  Joseph
                  >
                  >
                  "David Wilkinson" <no-reply@effisols. com????
                  news:%23qJCnL%2 3qGHA.516@TK2MS FTNGP05.phx.gbl ...
                  >Joseph wrote:
                  >>
                  >>Sorry , do I need to call the ReleaseBuffer() method before GetLength()
                  >>, or I misunderstood your meaning?
                  >>
                  >[snip]
                  >>
                  >Joseph:
                  >>
                  >Yes! Whenever you use GetBuffer you should call ReleaseBuffer.
                  >>
                  >David Wilkinson
                  >
                  >

                  Comment

                  • David Wilkinson

                    #10
                    Re: CString-&gt;GetLengt h() does not work

                    Joseph wrote:
                    Thanks David & Eberhard
                    >
                    I read the CString documents and I changed my code as below, but it still
                    does not work! I do not know why!
                    >
                    Line1: char *p1 = new char[1428];
                    Line2: fread(p1,1428,1 ,fp1);
                    Line3: CString str1(p1);
                    Line4: str1.ReleaseBuf fer();
                    Line5: int i;
                    Line6: i = str1.GetLength( );
                    >
                    // I found the value for i is 1 ,not 1428. Why?
                    >
                    but if I change line 2 to the following line ,it can work properly, why?
                    >
                    strcpy(p1,"ABCD EFG"); //not fread(p1,1428,1 ,fp1);
                    >
                    >
                    Could anybody give me a help?Help?
                    >
                    Thanks in advance!
                    >
                    Joseph
                    >
                    >
                    "David Wilkinson" <no-reply@effisols. com????
                    news:%23qJCnL%2 3qGHA.516@TK2MS FTNGP05.phx.gbl ...
                    >
                    >>Joseph wrote:
                    >>
                    >>
                    >>>Sorry , do I need to call the ReleaseBuffer() method before GetLength() ,
                    >>>or I misunderstood your meaning?
                    >>
                    >>[snip]
                    >>
                    >>Joseph:
                    >>
                    >>Yes! Whenever you use GetBuffer you should call ReleaseBuffer.
                    >>
                    >>David Wilkinson
                    >
                    >
                    >
                    Joseph:

                    1. You only need ReleaseBuffer() if you have called GetBuffer().

                    2. You should not be using "new" here. Never new unless you have to.

                    Line1: char p1[1428];
                    Line2: fread(p1, 1428, 1, fp1);
                    Line3: CString str1(p1);
                    Line4: int i = str1.GetLength( );

                    or

                    Line1: CString str1;
                    Line2: char* p1 = str1.GetBuffer( 1428);
                    Line3: fread(p1, 1428, 1, fp1);
                    Line4: str1.ReleaseBuf fer();
                    Line5: int i = str1.GetLength( );

                    or

                    Line1: char p1[1428];
                    Line2: fread(p1, 1428, 1, fp1);
                    Line3: int i = strlen(p1);

                    But, as you seem to have noticed, all these codes depend on the
                    existence of a null character in the string.

                    David Wilkinson

                    Comment

                    Working...