Click here to Skip to main content
15,888,521 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: char buf Pin
sunit511-Jan-06 3:09
sunit511-Jan-06 3:09 
AnswerRe: char buf Pin
benjymous11-Jan-06 3:22
benjymous11-Jan-06 3:22 
GeneralRe: char buf Pin
Smith#11-Jan-06 3:30
Smith#11-Jan-06 3:30 
GeneralRe: char buf Pin
benjymous11-Jan-06 7:30
benjymous11-Jan-06 7:30 
GeneralRe: char buf Pin
Eytukan11-Jan-06 3:43
Eytukan11-Jan-06 3:43 
AnswerRe: char buf Pin
Nish Nishant11-Jan-06 3:59
sitebuilderNish Nishant11-Jan-06 3:59 
AnswerRe: char buf Pin
vipinasda11-Jan-06 4:43
vipinasda11-Jan-06 4:43 
AnswerRe: char buf Pin
lemur211-Jan-06 4:50
lemur211-Jan-06 4:50 
You're asking one of two things:

Q1) how do I create an adjustable-length string to read data into?

A - use something like std::vector instead

Q2) how do I read in an arbitrary length of user data and minimise the amount of temporary storage required?

A - basically, if you don't know in advance how much data there will be, you can only make an educated guess, then shorten the string to the correct size at the end.

To keep things to an *absolute minimum*, you read a character at a time and resize and copy the string each new character. However, this is theoretical - it would be dog slow and would probably fragment memory hideously for long message.

A common solution is to start off with a buffer (use a std::vector again) and read as many bytes in a chunk from your socket as it would take to fill this. If you've finished reading, stop and shorten your vector.

If there's more text, increase the length of the string and read more. You could increase by the same amount (so your string lengths are N, 2N, 3N etc) or double it each time (giving lengths of N, 2N, 4N etc), or any other method.

What you want to do is minimise the amount of memory copying and number of socket reads.

Hope that helps (homework? Big Grin | :-D )

Kev
GeneralRe: char buf Pin
Sebastian Schneider11-Jan-06 5:12
Sebastian Schneider11-Jan-06 5:12 
AnswerRe: char buf Pin
Sebastian Schneider11-Jan-06 5:10
Sebastian Schneider11-Jan-06 5:10 
AnswerAnswer!! Pin
Eytukan11-Jan-06 5:25
Eytukan11-Jan-06 5:25 
AnswerRe: char buf Pin
Bob Stanneveld11-Jan-06 10:31
Bob Stanneveld11-Jan-06 10:31 
GeneralRe: char buf Pin
Tim Smith11-Jan-06 11:52
Tim Smith11-Jan-06 11:52 
GeneralRe: char buf Pin
Bob Stanneveld12-Jan-06 22:19
Bob Stanneveld12-Jan-06 22:19 
AnswerRe: char buf Pin
Stephen Hewitt11-Jan-06 15:34
Stephen Hewitt11-Jan-06 15:34 
QuestionPermissions and fopen Pin
Chris Meech11-Jan-06 3:02
Chris Meech11-Jan-06 3:02 
AnswerRe: Permissions and fopen Pin
vipinasda11-Jan-06 4:53
vipinasda11-Jan-06 4:53 
GeneralRe: Permissions and fopen Pin
Chris Meech11-Jan-06 6:17
Chris Meech11-Jan-06 6:17 
GeneralRe: Permissions and fopen Pin
__yb11-Jan-06 11:36
__yb11-Jan-06 11:36 
GeneralRe: Permissions and fopen Pin
vipinasda11-Jan-06 16:23
vipinasda11-Jan-06 16:23 
GeneralRe: Permissions and fopen Pin
__yb11-Jan-06 23:52
__yb11-Jan-06 23:52 
QuestionON_NOTIFY_RANGE and Textbox's KillFocus Pin
Sarvan AL11-Jan-06 2:31
Sarvan AL11-Jan-06 2:31 
AnswerRe: ON_NOTIFY_RANGE and Textbox's KillFocus Pin
Cedric Moonen11-Jan-06 2:33
Cedric Moonen11-Jan-06 2:33 
AnswerRe: ON_NOTIFY_RANGE and Textbox's KillFocus Pin
FarPointer11-Jan-06 3:15
FarPointer11-Jan-06 3:15 
GeneralThanks: ON_NOTIFY_RANGE and Textbox's KillFocus Pin
Sarvan AL11-Jan-06 21:43
Sarvan AL11-Jan-06 21:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.