473,657 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String class, how to implement sprintf?

CJ

Ok, I've got a string class that stores the data in a dynamically allocated
"char* m_pData" variable. I'd like to write a member function similiar to
"sprintf" for this class.

When implementing this member function, I'd like to just call "sprintf"
under the covers. The problem is, that I dont' know how large the resulting
string will be and thus I don't know how much space I'll need to allocate
for my m_pData pointer.

Is there any easy what to do this?

Thanks!
-CJ
Jul 19 '05 #1
1 5118

"CJ" <cj@nospam.co m> wrote in message
news:zt******** ************@gi ganews.com...

Ok, I've got a string class that stores the data in a dynamically allocated "char* m_pData" variable. I'd like to write a member function similiar to
"sprintf" for this class.

When implementing this member function, I'd like to just call "sprintf"
under the covers. The problem is, that I dont' know how large the resulting string will be and thus I don't know how much space I'll need to allocate
for my m_pData pointer.

Is there any easy what to do this?

Thanks!
-CJ


Many environments include a (non-standard) function called "snprintf /
vsnprintf". It allows you to provide the maximum length of your buffer, and
returns a special value if the buffer is too small. You can call that
function repeteadly allocating larger and larger amounts of memory until the
whole message fits in the buffer. It'll be a little slower, but it'll work.

I guess you could also open a dummy file [like "/dev/null" (UNIX), or "NUL"
(DOS, Windows), or just a normal temporary file], fprintf into it, grab the
return value, allocate that amount of memory, and then call the normal
sprintf. This might be even slower.

Note: printf() & friends return the number of characters they emitted.

D.


Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4830
by: Oliver S. | last post by:
I've developed a string-class that holds the string in an array which is a member-variable of the class and that has maximum-size which is con- figurable through a template-parameter. If any operation would grow the string beyond its maximum size, an exeception would be thrown. This kind of string obviously has superior performance over a std::string because there's never any additional memory-allocation. But before I'm going to re-invent...
23
4742
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
4
9085
by: Carl Youngblood | last post by:
I imagine this subject has probably been brought up numerous times. Forgive me for bringing it up again. I was googling through old posts on this newsgroup about it and found a good suggestion on how to add functionality to the string class. Rather than trying to inherit from the string class, the author was suggesting wrapping the string class in another class, like so: >Right !!!, we should not derive from string, but we should wrap...
27
2425
by: djake | last post by:
In the stroustrup C++ programming language (third edition) i can find example like this: string s1= "Hello"; So I imagine string is a standard class. Furthermore the class in the example is initialized with a constructor like this
13
3150
by: Rob Meade | last post by:
Hi all, I have written a small ProperCase function which I would like to make available to our team at work through our common class library. A colleague mentioned that I could write a new class which derived from the String class, add the function there and then we would all use the new class which contained my function and all of the others etc....sounded relatively straight forward...
3
1398
by: ironpythonster | last post by:
Hi everyone,there is a simply question puzzle me: Under the CPP,where can use the char* to creat a string class for use,but under the .NET Framework? How to use C# to creat a string class as the BCL suppert?? It can do this work? PS: I read from the code of SSCLI,It is found that when BCL implent the string ,it calll for an model of the runtime lib.
7
2716
by: craigkenisston | last post by:
I need an special string that have a fixed capacity and that automatically(silently) trims extra space. MySpecialString str = new MySpecialString(10); str = "1234567890ABCDE" Console.WriteLine(MySpecialString.ToString()) // output "1234567890" i.e. trimes the "ABCDE"
7
1673
by: Peter Morris | last post by:
interface Interface1 { } interface Interface2 : Interface1 { } class A : Interface2 {
6
3283
emaghero
by: emaghero | last post by:
Morning all, I am currently processing some data that is contained in several files in several different locations. The filenames contain descriptions of the data that I am working with, for example rect_W_7.2_H_3.8.txt I am using the STL string class on MSVS to name the directories storing the files. string drive="c:\\" string dir1="Output_Files\\"; string dir2="Calculation_Data\\"; string wg1="Rectangular\\";
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.