Click to See Complete Forum and Search --> : question about CStrings GetBuffer


Alan Benett
May 1st, 1999, 01:48 PM
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?!

B. Colombet
May 7th, 1999, 06:31 AM
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

Jason Teagle
May 7th, 1999, 08:14 AM
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.

indika
May 7th, 1999, 09:57 AM
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 );

Chetan
May 7th, 1999, 03:56 PM
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

November 6th, 1999, 06:47 AM
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: magpe96@nts.mh.se