Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

CString to char with unknown buffer length

Virtua KnightVirtua Knight Member Posts: 78
I am writing a program which needs to be able to read an unknown amount of data and store it in a CString. I use GetBuffer() and ReleaseBuffer() to convert the data I read from the file through fgets(), but I don't know how to write a program that will allow the buffer size to expand to accomodate all of the data. Any help would be appreciated.

"Who is General Failure and why is he reading my hard disk?!"

Comments

  • grahamojgrahamoj Member Posts: 366
    : I am writing a program which needs to be able to read an unknown amount of data and store it in a CString. I use GetBuffer() and ReleaseBuffer() to convert the data I read from the file through fgets(), but I don't know how to write a program that will allow the buffer size to expand to accomodate all of the data. Any help would be appreciated.
    :
    : "Who is General Failure and why is he reading my hard disk?!"
    :

    hiya you could try this..

    CString mystr;
    sprintf( mystr,"%s", my_buffer);

    so get the data you want and store it in 'my_buffer' and then use the sprinft line above.
    that will put the data in a CString not specified in size..

    hope it helps..
    grahamoj.

  • zenboyzenboy Member Posts: 26
    If you want to use a CString, you do not have to specify its length. That the beautiful thing. Example: You have 'UserFirstName' in my_buffer and you want to make your CString equal my_buffer.

    CString strUserName;
    // ** put my_buffer into the CString
    strUserName = my_buffer;


    You could add the users last name to this, lets say its another char buffer lastName_buffer , but it could also be another CString.

    // ** add a space to the string
    strUserName += " ";
    // ** add the last name to the string
    strUserName += lastName_buffer;

    if you want to get the length of this CString and store it in nLength you would use:

    int nLength = strUserName.GetLength();


  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    ________ [ http://forcoder.org ] free video tutorials and ebooks about { Java, Delphi, Perl, Ruby, R, C#, C++, Swift, PHP, Scratch, Python, MATLAB, Visual Basic .NET, PL/SQL, Go, Objective-C, Visual Basic, JavaScript, Assembly, C Lua, D, Logo, Scala, Prolog, ABAP, VBScript, Hack, F#, SAS, ML, Fortran, LabVIEW, Lisp, Crystal, Dart, Bash, Rust, Julia, Ada, Awk, COBOL, Erlang, Kotlin, FoxPro, Transact-SQL, Alice, Clojure, Apex, Scheme } ________

Sign In or Register to comment.