Problem with strcat() function

This is a discussion on Problem with strcat() function within the C Programming forums, part of the General Programming Boards category; I want to create an html file using C and as the first step , I wrote a simple program ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    6

    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:

    Code:
    #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
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    first of all it is: int main

    You need to allocate memory for the string. Look up malloc and free.

    /edit.
    Or for something simple like this you could just make your string as big as it needs to be. The trouble is determining how big you need it to be without wasting all kinds of space.

    Code:
    char str[80];  /* this let you store up to 80 chars including the null. */
    /edited for code tags and...
    You should look up "strncat" especially if you go the quick and dirty method of using a fixed length array for your strings.

    /edit 3
    conio.h is not a standard header.
    Last edited by Bajanine; 01-01-2007 at 09:20 PM.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()    // main should return a value
    {
        char *name="My Namel";
        /* If this is the string that u are concatenating. This should had enough
        space to hold the string which u are about to concatenate
        
        Ex: str1 = "hello"
            str2 = "world"
            
            char str3[15];
            
            strcat(str1,str2); --> "helloworld\0"; // this is how it will represent
        */
        char str[80]; 
        
        strcpy(str,"<html><head>");
        strcat(str,name);
        strcat(str,"</head></html>");
        
        fputs(str,stdout);
        
        getchar();
        return 0;
    }
    
    /* my output
    <html><head>My Namel</head></html>
    */
    you need proper code indentation.

    NOTE: To be more dynamic u can use malloc to allocate space for str at runtime. Which u might have to look in the future

    ssharish2005
    Last edited by ssharish2005; 01-03-2007 at 04:32 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Why are you flushing sdout before you print anything? Why are you flushing it at all? . . . getchar() will flush it for you.

    You output is wrong. The first call should be strcpy(), not strcat(), or else you should set set[0] to '\0'.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    DWKS, just concentrating on his code. I Don't know i was checking it, for some reason i places flushed it out but forgot to remove it out. Any way thanks for that

    ssharish2005

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well if you were checking it why did you miss the first strcat()?

    BTW, fputs() doesn't output a newline like puts() does.

    You should look up "strncat" especially if you go the quick and dirty method of using a fixed length array for your strings.
    http://www.cplusplus.com/reference/c...g/strncat.html

    > Turbo C
    *gasp*
    .
    .
    .
    You should strongly consider getting a newer compiler, such as Dev-C++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Yet another way.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char str[80], name[] = "My Namel";
       sprintf(str, "<html><head>%.*s</head></html>", (int)(sizeof str - 27), name);
       fputs(str, stdout);
       getchar();
       return 0;
    }
    
    /* my output
    <html><head>My Namel</head></html>
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 01:36 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21