CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 1999
    Posts
    14

    question about CStrings GetBuffer

    Hi!

    I used these lines of code to read a text file into a CString-object:

    CString cstrFileBuffer;
    LPSTR lpstrFileBuffer;
    DWORD dwFileLength;
    lpstrFileBuffer = cstrFileBuffer.GetBuffer ();

    CFile file (cstrFileName, CFile::modeRead);
    dwFileLength = file.GetLength ();
    file.Read (lpstrFileBuffer, dwFileLength);
    cstrFileBuffer.ReleaseBuffer ();

    Well, this works in my case, but i was wondering if the GetBuffer()-function
    does allocate the memory when calling it (without initializing the CString).
    If not i might get some problems later on...

    Can anybody tell me a more elegant and/or safer way to read in a text-file?!


  2. #2
    Join Date
    Apr 1999
    Location
    France
    Posts
    35

    Re: question about CStrings GetBuffer

    CString is an object which manages its memory by its own.
    When you instantiate a new object and call GetBuffer I guess the pointer you'll have will point to nothing.
    I don't think you can manage the memory allocation zone of CString object directly.
    Why don't you use a CArchive object associated with you CFile object.
    CArchive can easly read CString objects and has the ReadString() method.

    Just ask MSDN about CArchive it will explain you how to use archive object and serialization.
    Hope it helps.

    Bruno


    C++ developer
    Faculté de Médecine
    27 Bd Jean Moulin
    13385 Marseille cedex 05

  3. #3
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: question about CStrings GetBuffer

    I believe the memory is allocated right from the start, and as you access the memory buffer it expands as needed. Be warned, though, that normally CStrings can hold a maximum of 32767 characters; I am not sure what would happen if you tried to copy more than that to the buffer.

    I would recommend a global memory object, allocated with ::GlobalAlloc(GHND, size), ::GlobalLock(handle) to lock it in place, ::GlobalUnlock() and ::GlobalFree() to unlock and release the allocated memory back to the ppol when finished. Saves the worry of buffer size on a CString. In effect, the CString is doing the same thing - but you do not have the overhead of the CString changing the memory size when needed if you allocate the whole lump in one go.

    --
    Jason Teagle
    [email protected]

  4. #4
    Join Date
    May 1999
    Posts
    30

    Straight C calls to read file fast/safe

    FILE* file = fopen(i_filename, "r");
    if(NULL == file)
    {
    bProcess = FALSE;
    }
    else
    {
    //find the size of the file
    fseek(file,0,SEEK_END);
    long len=ftell(file);
    fseek(file,0,SEEK_SET);

    char* data = (char*) malloc(len+1);
    if(NULL == data)
    {
    bProcess = FALSE;
    TempfileName = i_filename;
    }
    else
    {
    long readLen =
    //read in the whole contnet
    fread( data, sizeof( char ), len+1, file );



  5. #5
    Join Date
    May 1999
    Location
    PA
    Posts
    38

    Re: question about CStrings GetBuffer

    CString constructor allocates the initial memory depending on the parameter string passed as a parameter to it. If no string is passed as a parameter to this constructor then CString allocates some minimal memory for itself to begin with.
    In the GetBuffer(Length) function if you specify the length smaller or equal to the length allocated by CString then it will not do any thing with the memory and will simply return you the pointer (LPSTR). If instead the the specified length is greater than its actual length then GetBuffer function takes care of reallocating the memory of the length equal to the specified length and then returns the pointer.

    I hope this will give you an answer to your question
    Thanks


  6. #6
    Guest

    Re: Read file to CString

    Hi!
    I wanted to read an text-file into a CString, or actually to CStringEx so I could do some manipulation of the file. Here is the code I used, I don't know if it's any good in terms of "safe" but it works fine for me.

    //the function returns the string
    CStringEx CMyProgramDlg::ReadFile()
    {
    CStringEx content;
    char next;
    int i=0;

    ifstream fin;
    fin.open("text.txt"); //open the file
    if (fin.fail())
    {
    exit(1);
    }

    fin.get(next);
    while(! fin.eof()) //get char from file and put into string
    {
    content.Insert(i,next);
    i++;
    fin.get(next);
    }

    fin.close(); //close file
    return content; //return the string with text
    }




    Magnus Pettersson , Email: [email protected]



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured