CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2003
    Location
    India,Kerala State
    Posts
    98

    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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    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.
    Code:
    str="";
    This does not define a character buffer big enough to hold all of those characters:
    Code:
    char str[200];
    That is what it should be.

    Also, main() returns int, not void.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Problem with strcat() function

    Quote 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

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Problem with strcat() function

    Quote 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?
    Quote 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.

  7. #7
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Problem with strcat() function

    Quote 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()!

  8. #8
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Problem with strcat() function

    Quote 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

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  10. #10
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Problem with strcat() function

    Quote 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.

  11. #11
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Problem with strcat() function

    Quote 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.

  12. #12
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Problem with strcat() function

    Quote 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.

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Problem with strcat() function

    Quote Originally Posted by Vanaj
    Take what NMTop40 and SuperKoko posted and combine the suggestions and all will work just fine...
    Ok...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured