Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > intToStr

Reply
Thread Tools

intToStr

 
 
Heinrich Wolf
Guest
Posts: n/a
 
      06-29-2012
Stefan pointed you the C way with vsnprintf. Since you seem to use C++ you
should maybe ask also in a C++ newsgroup. There is a stringstream class. But
I am not expirienced with that.

 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      06-29-2012
On 29/06/2012 15:05, Andrej Viktorovich wrote:
> Thank you, I got many answers to a lot of hypothetical questions.


No - you got the answer to the question you asked. You were told why
your example code failed and you were shown a number of ways in which,
using C, your code could be made to work.

> But how do you deal in case for example when you need just log some
> variables like debug info.


That is not the question you asked. The answer to it is probably
language-specific. If you are actually planning to work in C, you'ld use
a member of the printf family - your example below suggests that
fprintf() may be appropriate.

In C++ it is likely that there are other mechanisms which would be
appropriate.

If you are planning to use C++, you should post your questions to the
appropriate newsgroup - comp.lang.c++

>
> void SaveToDebugInfoFile(char[] info)
> {
> ...
> }
>
> int main
> {
> int a =1;
> int b =2;
> int c =3;
> ...
>
> SaveToDebugInfoFile(*/ need to pass a,b,c variables with some text /*);
> }
>
> I hope to have in output something like this:
> "variable a=1 , variable b=2, variable c=3"
>
>
> int main
> {
> int a =1;
> int b =2;
> int c =3;
> ...
>
> SaveToDebugInfoFile(*/ need to pass a,b,c variables with some text /*);
> }
>
> I hope to have in output something like this:
> "variable a=1 , variable b=2, variable c=3"
>



 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      06-29-2012
בתאריך יום שישי, 29 ביו*י 2012 15:05:39 UTC+1, מאת Andrej Viktorovich:
> Thank you, I got many answers to a lot of hypothetical questions.
>
> But how do you deal in case for example when you need just log some variables like debug info. How you construct string that will be saved to file? Do you really make such long and boring conversion to string routine (creating char[] variable, calling itoa ) for each variable of a,b,c and then concatenate them into one string before passing to function SaveToDebugInfoFile.
>
>

No, you use vsprintf to construct

SaveToDebugInfoFilef(char *format, ...)

Then you just pass strings to this function, exactly as you would to printf().

If you want to dump an array there's no way of doing it other than in a loop. You could write your own vsprintf() equivalent that takes arrays, but that would probably be more trouble than its worth.

--
Visit my website
http://www.malcolmmclean.site11.com/www

New halftoning routines
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-29-2012
Andrej Viktorovich <(E-Mail Removed)> writes:
>> Assuming that C++ differs little from C in this respect, you're
>> returning the address of a buffer which is local to the invocation of
>> intToStr. It's not a good idea.

>
> I have feeling that in C i must create memory area and pass it like
> parameter to function that converts int to string.
>
> But I would like to have simple function with one param of integer and
> I would like to ask intToString to do all that job of creating memory
> area and I need just char[] on utput. Is it possible to think that way
> in C?


Yes, but you have to think *really hard*. }

You can't just return a string or array from a C function. It has to be
allocated somewhere, and you pretty much have to manage that allocation
yourself.

There are (at least) three approaches:

1. Declare the local buffer as "static", so its lifetime extends over
the entire execution of the program. This has some drawbacks. There's
only one buffer in memory, so successive calls will clobber the value
from previous calls. And you have to decide how big the buffer needs to
be.

2. Have the caller pass in a pointer to (the first element of) its own
buffer.

3. Have the function allocate a new buffer using malloc(). This makes
the caller responsible for free()ing the buffer when it's done with it.

(Since you're using "cout << ...", you might consider using C++
mechanisms for this, like std::string. But if you have any questions
about C++, you'll need to ask in comp.lang.c++.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Fred K
Guest
Posts: n/a
 
      06-29-2012
On Friday, June 29, 2012 7:05:39 AM UTC-7, Andrej Viktorovich wrote:
> Thank you, I got many answers to a lot of hypothetical questions.
>
> But how do you deal in case for example when you need just log some variables like debug info. How you construct string that will be saved to file? Do you really make such long and boring conversion to string routine (creating char[] variable, calling itoa ) for each variable of a,b,c and then concatenate them into one string before passing to function SaveToDebugInfoFile.
>
> void SaveToDebugInfoFile(char[] info)
> {
> ...
> }
>
> int main
> {
> int a =1;
> int b =2;
> int c =3;
> ...
>
> SaveToDebugInfoFile(*/ need to pass a,b,c variables with some text /*);
> }
>
> I hope to have in output something like this:
> "variable a=1 , variable b=2, variable c=3"
>
>
> int main
> {
> int a =1;
> int b =2;
> int c =3;
> ...
>
> SaveToDebugInfoFile(*/ need to pass a,b,c variables with some text /*);
> }
>
> I hope to have in output something like this:
> "variable a=1 , variable b=2, variable c=3"


If all you want is to write it to a log file, what's wrong
with just using fprintf() ? Then you don't ever need to
create buffer space at all.

 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      07-01-2012
On 2012-06-29, James Kuyper <(E-Mail Removed)> wrote:
> cout << 55 << "\n";
>
> It would also be more idiomatic to use endl rather than "\n".


It's not the same thing. "\n" outputs a newline,
std::endl output a newline and performs a flush.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      07-01-2012
On Sun, 1 Jul 2012 21:26:20 +0000 (UTC), Ike Naar
<(E-Mail Removed)> wrote:

>On 2012-06-29, James Kuyper <(E-Mail Removed)> wrote:
>> cout << 55 << "\n";
>>
>> It would also be more idiomatic to use endl rather than "\n".

>
>It's not the same thing. "\n" outputs a newline,
>std::endl output a newline and performs a flush.


This actually works in a C program???

--
Remove del for email
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      07-01-2012
On 2012-07-01, Barry Schwarz <(E-Mail Removed)> wrote:
> On Sun, 1 Jul 2012 21:26:20 +0000 (UTC), Ike Naar
><(E-Mail Removed)> wrote:
>
>>On 2012-06-29, James Kuyper <(E-Mail Removed)> wrote:
>>> cout << 55 << "\n";
>>>
>>> It would also be more idiomatic to use endl rather than "\n".

>>
>>It's not the same thing. "\n" outputs a newline,
>>std::endl output a newline and performs a flush.

>
> This actually works in a C program???


Well, it works in the dialect called C/C++
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      07-01-2012
On 07/01/2012 05:31 PM, Barry Schwarz wrote:
> On Sun, 1 Jul 2012 21:26:20 +0000 (UTC), Ike Naar
> <(E-Mail Removed)> wrote:
>
>> On 2012-06-29, James Kuyper <(E-Mail Removed)> wrote:


[Restored context:]
>>> I thought you said you were using C? This looks like C++, a different,
>>> though closely related language. You'll get better answers about such
>>> things by going to comp.lang.c++. ... Also, do you realize
>>> that you'd get a similar result by writing


....
>>> cout << 55 << "\n";
>>>
>>> It would also be more idiomatic to use endl rather than "\n".

>>
>> It's not the same thing. "\n" outputs a newline,
>> std::endl output a newline and performs a flush.

>
> This actually works in a C program???


No, it was a comment referring to C++ code, as you can tell from the
restored context quoted above.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-01-2012
On 07/ 2/12 10:22 AM, James Kuyper wrote:
> On 07/01/2012 05:31 PM, Barry Schwarz wrote:
>> On Sun, 1 Jul 2012 21:26:20 +0000 (UTC), Ike Naar
>> <(E-Mail Removed)> wrote:
>>
>>> On 2012-06-29, James Kuyper<(E-Mail Removed)> wrote:

>
> [Restored context:]
>>>> I thought you said you were using C? This looks like C++, a different,
>>>> though closely related language. You'll get better answers about such
>>>> things by going to comp.lang.c++. ... Also, do you realize
>>>> that you'd get a similar result by writing

>
> ....
>>>> cout<< 55<< "\n";
>>>>
>>>> It would also be more idiomatic to use endl rather than "\n".
>>>
>>> It's not the same thing. "\n" outputs a newline,
>>> std::endl output a newline and performs a flush.

>>
>> This actually works in a C program???

>
> No, it was a comment referring to C++ code, as you can tell from the
> restored context quoted above.


Sense of humour MIA?

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments