Best Way to Handle Unknown Data Sizes?

M

Mike Copeland

I have a number of functions similar to the code below that are
starting to cause problems when incoming data exceeds the limit (e.g.
1024) imposed by the code. Rather than select another arbitrary limit
and have the problem occur later when larger data appears, I'd like to
fix the issue the right way. I'm unsure how to do this...
Please advise. TIA

char* TextFunctions::removeCR(char *s) // Remove C/R from data string
{
static char w[1024];
int n;
char *p;

strcpy (w, s);
n = strlen(w);
p = strrchr(w, char(10));
if(p) w[p-w] = '\0';

return w;
} // remove CR
 
I

Ian Collins

I have a number of functions similar to the code below that are
starting to cause problems when incoming data exceeds the limit (e.g.
1024) imposed by the code. Rather than select another arbitrary limit
and have the problem occur later when larger data appears, I'd like to
fix the issue the right way. I'm unsure how to do this...
Please advise. TIA

char* TextFunctions::removeCR(char *s) // Remove C/R from data string
{
static char w[1024];
int n;
char *p;

strcpy (w, s);
n = strlen(w);
p = strrchr(w, char(10));
if(p) w[p-w] = '\0';

return w;
} // remove CR

Use C++ strings.
 
C

copx

I have a number of functions similar to the code below that are
starting to cause problems when incoming data exceeds the limit (e.g.
1024) imposed by the code. Rather than select another arbitrary limit
and have the problem occur later when larger data appears, I'd like to
fix the issue the right way. I'm unsure how to do this...
Please advise. TIA

char* TextFunctions::removeCR(char *s) // Remove C/R from data string
{
static char w[1024];
int n;
char *p;

strcpy (w, s);
n = strlen(w);
p = strrchr(w, char(10));
if(p) w[p-w] = '\0';

return w;
} // remove CR

That's C-style code, not really idiomatic C++. Plus, it sounds and looks
like homework.

But well, I will give you a few pointers (pun intended):

In C-style code you would dynamically allocate, reallocate, and
eventually free memory as needed. The related functions are: malloc(),
realloc(), and free() -- do a web search

In "modern C++" you are supposed to use the container types in the STL.
Web search for: std::string and std::vector
 
J

Jorgen Grahn

I have a number of functions similar to the code below that are
starting to cause problems when incoming data exceeds the limit (e.g.
1024) imposed by the code. Rather than select another arbitrary limit
and have the problem occur later when larger data appears, I'd like to
fix the issue the right way. I'm unsure how to do this...
Please advise. TIA

char* TextFunctions::removeCR(char *s) // Remove C/R from data string
{
static char w[1024];
int n;
char *p;

strcpy (w, s);
n = strlen(w);
p = strrchr(w, char(10));

Something (apart from the function's design) is wrong here -- ASCII CR
(not "C/R") is decimal 13. LF is 10.

And if you mean C++ '\n', say '\n' instead of hardcoding a numeric
value.

/Jorgen
 
R

red floyd

I have a number of functions similar to the code below that are
starting to cause problems when incoming data exceeds the limit (e.g.
1024) imposed by the code. Rather than select another arbitrary limit
and have the problem occur later when larger data appears, I'd like to
fix the issue the right way. I'm unsure how to do this...
Please advise. TIA

char* TextFunctions::removeCR(char *s) // Remove C/R from data string
{
static char w[1024];
int n;
char *p;

strcpy (w, s);
n = strlen(w);
p = strrchr(w, char(10));

Something (apart from the function's design) is wrong here -- ASCII CR
(not "C/R") is decimal 13. LF is 10.

And if you mean C++ '\n', say '\n' instead of hardcoding a numeric
value.
And if he really means CR, then he should use '\r'
 
I

Ian Collins

[ Reposting; this one uses free(), not realloc() ]

char* Upâ‹…Toâ‹…LF( char *Bâ° ) {
// From (Bâ°), return a string up to,
// but not including, any LineFeed ( 10 ).
//
// If possible, rewrite this using a temporary heap;
// so the heap can be destroyed,
// instead of calling free() and/or realloc().

static int Sz⋅Buff = 1024 ; static char *B¹ ; char *LF; int Len;

if ( LF = strchr( Bâ°, 10 ), !LF ) // Do nothing.
return Bâ° ;

if ( Len = LF - Bâ° + 1, Len > Szâ‹…Buff )
// Free the old buffer, alloc a larger one.
Sz⋅Buff = Len, free( B¹ ), B¹ = 0 ;

if ( !B¹ ) // Create the buffer.
B¹ = (char*)malloc( Sz⋅Buff );

sprintf( B¹, "%.*s", --Len, BⰠ); return B¹; }

What is this nonsense?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top