CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

printf sometime doesn't output correct data??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Honken



Joined: 03 Mar 2004
Posts: 15

View user's profile Send private message

printf sometime doesn't output correct data??
PostPosted: Thu Jan 17, 2008 6:32 am     Reply with quote

I am running v4.066 with a dsPIC30F6010A and noticed that sometimes after I added or removed some code the printf function outputs more data after the NULL termination.

I looks like it does something wrong when it using its pointer so it wont see the null termination.

It's like if I have odd numbers of char in the string then it's OK but with even I get the string and some junk data on my RS232 line.

Is it possible to add some sort off pre-processor statement so the code alines to even or odd memory addresses??
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Jan 17, 2008 8:03 am     Reply with quote

Please post sample code.
Honken



Joined: 03 Mar 2004
Posts: 15

View user's profile Send private message

PostPosted: Thu Jan 17, 2008 10:40 am     Reply with quote

boolean loadCANfile(char filenname[], int8 handle, struct file_handle * hFile)
{

// This fprintf will output more than "test.txt" it will add 2 more byte from memory
fprintf(RS232, "\r\nO %cR>%s\r\n", handle, filenname);
}



main
{

loadCANfile("test.txt", 1, &hFiles_0);
}
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Jan 18, 2008 2:48 am     Reply with quote

Your problem may be related to an error I can see.

you pass the value 1 as handle.
You print this using %c.
%c displays the character of the value so an '1' is 49 or 0x31. the character represented by a 1 is SOH (Start Of Heading) this will most likely be displayed as garbage or you may not see anything at all.
You need to use %d to display the actual value of handle.

See if that helps.
Honken



Joined: 03 Mar 2004
Posts: 15

View user's profile Send private message

PostPosted: Fri Jan 18, 2008 4:29 am     Reply with quote

I tested that but then I get "0 49R>test.txt1 2"

with %c I get "0 1R>test.txt1 2"

And I want "0 1R>test.txt"

Really strange
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Jan 18, 2008 7:14 am     Reply with quote

Do you have more code before the fprintf in that function ?
try using char *filename in the header instead of char filename[].

The %c %d issue is very strange. This implies that handle does actually contain the ascii value of '1'.

So unless you are modifying filename and handle before calling fprintf then something very strange is happening.

It also looks like there are 3 extra chars 1[sp]2

Going back to your first post. Doe this work sometimes depending on adding more code ? or lines (blank) ?
kamputty
Guest







PostPosted: Fri Jan 18, 2008 1:25 pm     Reply with quote

Hi all!

What happens if you use this line

fprintf(RS232, "\r\nO %cR>%s\r\n", '0'+handle, filenname);

You will notice I added ---> '0'+handle

i'm assuming that 'handle' has NOT been adjusted to use ascii, but is a number 0~9. By using the '0'+handle, we change the value to ascii 0~9...but you should really change it to %d...

~Kam (^8*
Honken



Joined: 03 Mar 2004
Posts: 15

View user's profile Send private message

PostPosted: Sat Jan 19, 2008 3:57 am     Reply with quote

I forgot to add the code above the fprintf function.

Yes I convert the handle to ASCII before I use the fprintf,

// Make it a char
handle = handle + 0x30;

wonder if it has to be something with the address of the handle?

I have noticed if handle is located on even address the problem ocurs?!?!
SET



Joined: 15 Nov 2005
Posts: 161
Location: Glasgow, UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat Jan 19, 2008 10:36 am     Reply with quote

Do you declare 'handle' as an 'int'? If so then I assume that its a 16 bit value. And your function expects an 8 bit value. The whole problem looks like a misalignment with the parameters passed to the function call - and in some cases the string terminator is missed with the result that you get the extra characters printed.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group