Casting in C

Thread Starter

chrisw1990

Joined Oct 22, 2011
551
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:
Rich (BB code):
unsigned char fileNAME[30];
Code:
Rich (BB code):
	FSchdir("\\Logs");
	rtccDate=(RtccGetDate());
	strncpy(fileNAME, rtccDate);
	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:

cheezewizz

Joined Apr 16, 2009
82
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.
 

Thread Starter

chrisw1990

Joined Oct 22, 2011
551
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.
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?
 

Thread Starter

chrisw1990

Joined Oct 22, 2011
551
can you help me manually do this? seems the easiest option..
iv got a struct sorted..
Rich (BB code):
	struct FileNameDate
	{
		char wday;	//Day of the week 00-06
		char mday;	//Day of the month 01-31
		char mon;	//Month of the year 01-12
		char long year;	//Year 00-99 (2000-2099)
	}	FNDate;				//Future Definitions specified by struct FileNameDate (Name_of_Variable);
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..
 

cheezewizz

Joined Apr 16, 2009
82
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
Rich (BB code):
FSchdir("\\Logs");    
 rtccDate=(RtccGetDate());     
strncpy(fileNAME, rtccDate.char);
might work. But like I say you'd need to go through the char array and add 0x30 to each. So maybe
Rich (BB code):
    FSchdir("\\Logs");     
 rtccDate=(RtccGetDate());
 for(int i = 0; i < 4; ++i)
        rtccDate.char += 0x30;
 strncpy(fileNAME, rtccDate.char); 
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.
 

cheezewizz

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

Thread Starter

chrisw1990

Joined Oct 22, 2011
551
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
Rich (BB code):
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......
 

cheezewizz

Joined Apr 16, 2009
82
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...
 

Thread Starter

chrisw1990

Joined Oct 22, 2011
551
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..
Rich (BB code):
		typedef union 
		{
			unsigned long full;
			char bytes[4];
		}SPLIT;

SPLIT split;

split.full=(RtccGetDate());
strcat(fileNAME, split.bytes[0]);
but that last strcat line is throwing up.. not an error, 'cos it still builds, but it says:
main.c:150:2: warning: passing argument 2 of 'strcat' makes pointer from integer without a cast
any ideas?
 

cheezewizz

Joined Apr 16, 2009
82
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...
 
Top