Thread: extracting info from strdate

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    extracting info from strdate

    // The program below gives the output "10/010/01"
    //But I only want the dd of mm/dd/yy.
    //How do I extract just the day? Thanks.



    #include <time.h>
    #include <iostream.h>
    #include <string.h>

    int main()
    {

    char datebuf[9];
    char day[2];
    char *digit1=&datebuf[3];
    char *digit2=&datebuf[4];

    _strdate(datebuf);
    strcpy(day,digit1);
    strcat(day,digit2);
    cout<<day<<endl;
    return 0;



    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char day[2];
    You need at least 3 chars to store "dd\0" - gotta remember that \0 when dealing with 'C' style strings

    > char *digit1=&datebuf[3];
    > char *digit2=&datebuf[4];
    Ok, but unnecessary...

    > strcpy(day,digit1);
    > strcat(day,digit2);
    This is where the problem is - using strcpy and strcat to copy single chars
    Try
    Code:
    day[0] = datebuff[3];
    day[1] = datebuff[4];
    day[2] = '\0';
    Or if you insist on using a str... function
    Code:
    strncpy( day, &datebuff[3], 2 );
    day[2] = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    Smile

    Thanks so much, Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help displaying info from structure
    By kisiellll in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 12:51 PM
  2. Extracting info from a packet trace file
    By Ho ming in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 11:54 AM
  3. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  4. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 03:12 PM
  5. Extracting info from string
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-25-2003, 09:04 AM