Casting in C

Discussion in 'Programmer's Corner' started by chrisw1990, Apr 11, 2012.

  1. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    ok, so..
    im using the RTCC date for filename of a file on an SD card.
    problem im having, im using plib function RtccGetDate() this returns an unsigned long with all the date info..
    however, the MDD filesystem uses a char array/pointer to a char array, for the filename.. therefore its not liking one version, im unsure how to change the type, tried using strncpy..
    heres the code snippet:
    Variables:
    Code ( (Unknown Language)):
    1. unsigned char fileNAME[30];
    Code:
    Code ( (Unknown Language)):
    1.     FSchdir("\\Logs");
    2.     rtccDate=(RtccGetDate());
    3.     strncpy(fileNAME, rtccDate);
    4.     FilePoint = FSfopen(fileNAME, "a+");    //Opens fileNAME and appends to the end
    Other Question is this:
    when use strncpy() does it append? lets say i do what iv done above, then want to put something else on the end.. could i use strncpy(fileName, SomethingElse);? and SomethingElse be at the end? as long as it is within the array length..
    Hope this makes sense.. any questions, please ask, all feedback welcomed.
     
    Last edited: Apr 11, 2012
  2. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    I don't know much about RtccGetDate() or the format it returns things in but in general, no you can't just cast a long into char*. Some kind of conversion process would be required.

    And for the other bit, have a look at strncat(), which is what you're after. It concatenates one string onto another.
     
    chrisw1990 likes this.
  3. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    cheers for the help.. that kinda isnt the answer i was looking for :p ill have to find some way round it, manually get the data say instead of using the function, dunno..
    if i used strncat() wouldnt that need two arrays for the strings? or could i use say strcat(fileNAME, "SomethingElse")? and that would then put that at the end?
     
  4. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    can you help me manually do this? seems the easiest option..
    iv got a struct sorted..
    Code ( (Unknown Language)):
    1.     struct FileNameDate
    2.     {
    3.         char wday;  //Day of the week 00-06
    4.         char mday;  //Day of the month 01-31
    5.         char mon;   //Month of the year 01-12
    6.         char long year; //Year 00-99 (2000-2099)
    7.     }   FNDate;             //Future Definitions specified by struct FileNameDate (Name_of_Variable);
    8.  
    so how do i get each of the bits into the relevant slots?
    the RTCC pic info is here and the register in use is RTCDATE..
    so somehow i need to go:
    FNDate.wday=RTCDATE(bits0-7) and so on..
    and then concatenate that/strcpy it into fileNAME..
     
  5. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    hah nothings ever simple is it. Looking at http://wiki.pinguino.cc/index.php/RTCC.getDate (presumably that's same thing you're on about) you'd still need to convert the BCD results to something that's usable.

    It does seem to indicate that you can access them from the 'char' member.
    So something like
    Code ( (Unknown Language)):
    1.    
    2. FSchdir("\\Logs");    
    3.  rtccDate=(RtccGetDate());    
    4. strncpy(fileNAME, rtccDate.char);
    5.  
    might work. But like I say you'd need to go through the char array and add 0x30 to each. So maybe
    Code ( (Unknown Language)):
    1.     FSchdir("\\Logs");    
    2.  rtccDate=(RtccGetDate());
    3.  for(int i = 0; i < 4; ++i)
    4.         rtccDate.char[i] += 0x30;
    5.  strncpy(fileNAME, rtccDate.char); [/i]
    but like I say I've not used this library before so I might be talking b4lls...

    As for strncat() yep you should be able to use a string literal as the second argument. Check out http://www.cplusplus.com/reference/clibrary/cstring/strncat/

    urgh excuse the whitespace in the code. dunno what happened there.
     
  6. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    ahh sorry it looks like I was talking about something completely different. was busy typing this when you replied. what compiler are you using?
     
  7. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    no in fact, that little link to pinguino site has given me a thought.. there is an example in plib.h explained here that, on pg 270 says
    Code ( (Unknown Language)):
    1. rtccDate dt; dt.l=RtccGetDate();
    which i ignored because in microchip style, it explains half of it..
    coupled with the structure on that site i could convert it......
     
  8. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    Hmmm did you see page 291 of that peripheral lib guide you linked to? It's got a code sample there... So the only difficulty you're faced with if you use your struct is converting the double digits values to BCD, i.e. if you've got ox10 for the 16th day or whatever it needs to be 0x16...
     
  9. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    ok, i hadnt noticed that, but it slightly confused me, i think cos of the spaces between it.. however; trying a different method which seems to be working..
    Code ( (Unknown Language)):
    1.         typedef union
    2.         {
    3.             unsigned long full;
    4.             char bytes[4];
    5.         }SPLIT;
    6.  
    7. SPLIT split;
    8.  
    9. split.full=(RtccGetDate());
    10. strcat(fileNAME, split.bytes[0]);
    but that last strcat line is throwing up.. not an error, 'cos it still builds, but it says:
    any ideas?
     
  10. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    it's just saying it expects a pointer to a char rather than a char/int. If you pass in 'bytes' rather than 'bytes[n]' it should do it. What will you be doing with this fileName though? As it stands it won't be in any kind of human readable form...
     
  11. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    making a filename for file on uSD card so it wont be a readable format?
     
  12. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    because its in bcd right?
    surely bcd wont matter?
    http://www.tpub.com/neets/book13/53s.htm
    that makes me think its just a representation to humans, rather than an actual change of the number like hex to octal or hex to decimal..?
     
    Last edited: Apr 11, 2012
Loading...