-
January 1st, 2007, 10:59 PM
#1
Problem with strcat() function
I want to create an html file using C and as the first step , I wrote a simple program as follows:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *name="My Namel",*str="";
clrscr();
strcat(str,"<html><head>");
strcat(str,name);
strcat(str,"</html></head>");
puts(str);
getch();
}
But the puts(str) statement is giving unexpected result.What is getting printed is "<html><head>My Name Name ad>". Why doesnt it print "<html><head>My Name </html></head>" as is expected? .Ultimately I want to write the string str to the html file using file operation. I tried adding a null character at the end of str, but that throws some illegal operations exceptions and Turbo C is terminated ubruptly.Thanks in advance
-
January 1st, 2007, 11:45 PM
#2
Re: Problem with strcat() function
Read the documentation on strcat(). The strcat() requires that there is room for all of those characters that you are adding.
This does not define a character buffer big enough to hold all of those characters:
That is what it should be.
Also, main() returns int, not void.
Regards,
Paul McKenzie
-
January 2nd, 2007, 04:10 AM
#3
Re: Problem with strcat() function
Paul is right. Get away from char pointers NOW and use std::string
1) Many Companies are outlaying the use of such structures.
2) Many companies are not hiring if you are not fluent in STL
If you are learning from a book that is over three years old, then get a ney one.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 2nd, 2007, 04:32 AM
#4
Re: Problem with strcat() function
Originally Posted by TheCPUWizard
Paul is right. Get away from char pointers NOW and use std::string
1) Many Companies are outlaying the use of such structures.
2) Many companies are not hiring if you are not fluent in STL
If you are learning from a book that is over three years old, then get a ney one.
TheCPUWizard .i think you Didn't get OP question .he is writing a code in C .then why C++ Suggestion Comes in Picture.and if still OP want to use Pointers in Code he can use malloc and Realloc Function for this.with the help of those above Function he can write his code in a efficient manner.
Thanx
-
January 2nd, 2007, 10:11 AM
#5
Re: Problem with strcat() function
To build the string in C it is probably preferable to use sprintf rather than strcat.
thus:
Code:
sprintf( str, "<html><head>%s</head></html>", name );
You have to ensure str points to an adequately allocated amount of memory. Remember to include one for the null terminator. In this example I make that 27 + strlen( name ). Preferable to avoid actually putting magic numbers like 27 into the code.
-
January 2nd, 2007, 11:42 AM
#6
Re: Problem with strcat() function
Originally Posted by TheCPUWizard
1) Many Companies are outlaying the use of such structures.
2) Many companies are not hiring if you are not fluent in STL
I am not saying against std::string but I haven't heard of this. There is no such outlaying or not hiring rules that I know of. There are huge systems already in C and older C++ days where std::string did not exist. Any documentation of such companies that you know of to support your words?
Originally Posted by humptydumpty
TheCPUWizard .i think you Didn't get OP question .he is writing a code in C .then why C++ Suggestion Comes in Picture.and if still OP want to use Pointers in Code he can use malloc and Realloc Function for this.with the help of those above Function he can write his code in a efficient manner.
How can repeated calls to malloc and/or realloc be an efficient manner? Better is to allocate once (after estimating the max storage) and work with sprintf as suggested by NMTop40.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 2nd, 2007, 03:37 PM
#7
Re: Problem with strcat() function
Originally Posted by exterminator
How can repeated calls to malloc and/or realloc be an efficient manner? Better is to allocate once (after estimating the max storage) and work with sprintf as suggested by NMTop40.
And, if possible, check that the string is not too long for the buffer and fail with a pretty error message in that case, instead of getting a buffer overrun and undefined behavior.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
January 3rd, 2007, 12:24 AM
#8
Re: Problem with strcat() function
Originally Posted by exterminator
I am not saying against std::string but I haven't heard of this. There is no such outlaying or not hiring rules that I know of. There are huge systems already in C and older C++ days where std::string did not exist. Any documentation of such companies that you know of to support your words? How can repeated calls to malloc and/or realloc be an efficient manner? Better is to allocate once (after estimating the max storage) and work with sprintf as suggested by NMTop40.
i think you get your question ans.
Thanx
-
January 3rd, 2007, 12:38 AM
#9
Re: Problem with strcat() function
Originally Posted by humptydumpty
i think you get your question ans.
No. I didn't.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 3rd, 2007, 01:06 AM
#10
Re: Problem with strcat() function
Originally Posted by exterminator
How can repeated calls to malloc and/or realloc be an efficient manner? Better is to allocate once (after estimating the max storage) and work with sprintf as suggested by NMTop40.
What if max storage can not be estimated... I.E. a word processor...
Jim
ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII
"The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.
"Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.
-
January 3rd, 2007, 01:34 AM
#11
Re: Problem with strcat() function
Originally Posted by Vanaj
What if max storage can not be estimated... I.E. a word processor...
That is a different problem altogether, isn't it? Looking at the OP, I think he can do with what I suggested. Otherwise, there needs to be an encapsulation provided. As SuperKoko suggests, check for overflow before appending - and then double the size of the buffer and copy over the previous written bytes. The logic would be similar to how a vector and probably a string object grows. Still that would not need repeated calls to malloc/realloc every time anything needs to be appended. At least, that is the idea that I had in my mind regarding the problem.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 3rd, 2007, 01:44 AM
#12
Re: Problem with strcat() function
Originally Posted by exterminator
That is a different problem altogether, isn't it? Looking at the OP, I think he can do with what I suggested. Otherwise, there needs to be an encapsulation provided. As SuperKoko suggests, check for overflow before appending - and then double the size of the buffer and copy over the previous written bytes. The logic would be similar to how a vector and probably a string object grows. Still that would not need repeated calls to malloc/realloc every time anything needs to be appended. At least, that is the idea that I had in my mind regarding the problem.
Take what NMTop40 and SuperKoko posted and combine the suggestions and all will work just fine...
Jim
ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII
"The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.
"Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.
-
January 3rd, 2007, 01:48 AM
#13
Re: Problem with strcat() function
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|