473,544 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Translating a DEFINE to char*

Dear people,

The code below is compiling:

#define BUF_SZ 16383
.....
strcat(ConnectS tring, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.

Jan 5 '06 #1
13 2029
ma*********@hot mail.com <ma*********@ho tmail.com> wrote:
#define BUF_SZ 16383
....
strcat(ConnectS tring, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


Try snprintf(), simpe and flexible.
--
:wq
^X^Cy^K^X^C^C^C ^C
Jan 5 '06 #2
ma*********@hot mail.com wrote:
Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectS tring, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


Macros are not substituted inside strings or inside comments. You can
achieve what you want using sprintf like this:

char my_string[BIG_ENOUGH];

sprintf(my_stri ng, "Content-Length: %d\n", BUF_SZ);

HTH

Jan 5 '06 #3
ÓÚ Thu, 05 Jan 2006 00:38:32 -0800£¬ma******* **@hotmail.comÐ ´µ½£º
Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectS tring, "Content-Length: BUF_SZ\n"); sprintf("Connec tString, "Content-Length: %d\n",BUF_SZ);
/*
%d -- int
%u -- unsigned int
%s -- char *
%c -- char
....
*/

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


Jan 5 '06 #4
iamgodk wrote:
于 Thu, 05 Jan 2006 00:38:32 -0800,ma****** ***@hotmail.com 写到:

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectS tring, "Content-Length: BUF_SZ\n");

sprintf("Connec tString, "Content-Length: %d\n",BUF_SZ);


you've gained an extra quote

sprintf(Connect String, "Content-Length: %d\n",BUF_SZ);

<snip>

--
Nick Keighley

De maan likt niet hoog
Maar het is niet zo
De maan is wel hoog
Of niet sams?

Jan 5 '06 #5
ma*********@hot mail.com wrote:

The code below is compiling:

#define BUF_SZ 16383
.....
strcat(ConnectS tring, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize
used in the string, but also somehow keep it simple, and keep it
flexible.


Try the following:

#define BUF_SZ "16383"
...
strcat(ConnectS tring, "Content-Length: " BUF_SZ "\n");

assuming you can modify the #define as shown.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Jan 5 '06 #6
Chuck
modify the #define


Then I could rather make two defines:

#define BUF_SZ 16383
#define CONTENTLENGTH "Contect-Length 16383 \n"
.....
strcat(ConnectS tring, CONTENTLENGTH);

but I think the "sprintf(Connec tString, "Content-Length:
%d\n",BUF_SZ);" is the neatest.

Jan 5 '06 #7
<ma*********@ho tmail.com>
??????:11****** *************** *@z14g2000cwz.g ooglegroups.com ...
Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectS tring, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


no need to call snprintf at run time, the compiler could do that. try this

#define BUF_SZ 16383
#define TO_STR(a) _TO_STR(a)
#define _TO_STR(a) #a
strcat(ConnectS tring, "Content-Length: " TO_STR(BUF_SZ) "\n");

kaikai
Jan 5 '06 #8

ma*********@hot mail.com wrote:
Dear people,
Hello,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectS tring, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.


Here is another solution to stringize (I think it's in the FAQ) your
macro:

#include <stdio.h>
#include <string.h>

#define BUF_SZ 16383

#define STR(n) #n
#define MKSTR(n) STR(n)

int main(void)
{
char ConnectString[50+1];
*ConnectString = '\0';
strncat(Connect String,"Content-Length: " MKSTR (BUF_SZ) "\n", 50);
puts(ConnectStr ing);
return 0;
}

Regis

Jan 5 '06 #9
kaikai wrote:
<ma*********@ho tmail.com>
??????:11****** *************** *@z14g2000cwz.g ooglegroups.com ...
Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(Connec tString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.

no need to call snprintf at run time, the compiler could do that. try this

#define BUF_SZ 16383
#define TO_STR(a) _TO_STR(a)
#define _TO_STR(a) #a
strcat(ConnectS tring, "Content-Length: " TO_STR(BUF_SZ) "\n");


This could be made conforming by changing the name of
_TO_STR to avoid using the reserved identifier. However, it's
probably better to construct the string at run-time with
sprintf() or some such than to depend on the lexical form of
the BUF_SZ definition. Consider what happens when somebody
rewrites BUF_SZ in a more mnemonic way, e.g.

#define BUF_SZ ((1 << 14) - 1)
#define BUF_SZ (HEADER_LEN + PREFIX_SZ + BODY_MAX)
#define BUF_SZ 0x3fff
...

--
Eric Sosman
es*****@acm-dot-org.invalid

Jan 5 '06 #10

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

Similar topics

12
1634
by: Charles Law | last post by:
Hi guys A bit of curve ball here ... I have a document (Word) that contains a series of instructions in sections and subsections (and sub-subsections). There are 350 pages of them. I need to translate these instructions into something that can be processed automatically, so I have used the Command pattern to set up a set of commands that...
8
2544
by: John Ratliff | last post by:
Let's say I had a program which uses some constants. Would it be "better" to declare them like this: #ifndef __MY_HDR_FILE #define __MY_HDR_FILE #define STRING_CONST "some string..." #define INT_CONSTANT 4852 #endif
0
915
by: winthux | last post by:
I'm trying to use the library in c++ by using but it requires some specific structures and macros: #define gg_common_head(x) \ int fd; /* podgl?dany deskryptor */ \ int check; /* sprawdzamy zapis czy odczyt */ \ int state; /* aktualny stan maszynki */ \
8
6705
by: mailursubbu | last post by:
Hi, Will it be possible to #define a char pointer... It means if write some thing like #define CHAR_PTR (char *) // I know this wont work I should be able to use CHAR_PTR to define a variable of type char *.
6
2018
by: John Dann | last post by:
I'm trying (in VB2005) to interact with a third-party DLL written (I think) in C++. One of the functions requires me to pass a structure consisting solely of char types according to the DLL documentation, but AIUI in C++ this will be single byte values not unicode as in .Net. Each variable element of the srtucture will assume a simple...
23
3888
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
9
1935
by: lombardm | last post by:
I am trying to decipher/translate some code that I have no experience with. I am trying to find out how the checksum is computed for a Megellan Explorist GPS Waypoint (POI) file. Below is the first 3 lines of a typical file. I understand everything except how the "a*23" at the end of the line is arrived at.
5
2570
by: Canned | last post by:
Hi, I'm trying to write a class that can convert ascii to binary and vice versa. I write my class based on this function I've found on internet That works perfectly, but when I try to implement it in my own class it gives me alot of headache, also because I'm totally new to the language. It work only with one character at a time, and if I...
0
7361
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...
0
7763
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...
0
7701
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5901
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...
1
5289
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...
0
3404
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
983
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
654
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...

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.