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

Length of a string?

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



Joined: 23 Aug 2005
Posts: 93

View user's profile Send private message Visit poster's website

Length of a string?
PostPosted: Fri Jan 20, 2006 5:16 am     Reply with quote

Do someone know why I get a "String Too Long" from this sprintf() as soon as I put another sign in it?
I know its rather long but, is there a max length beside the one I put there (128)? beacuse the string aint >128 long! And it doesnt matter if I make room for whatever in the buffer.
When I made a strlen() at it I got the length of only 84.
Beside, thanks for a god forum.
Code:

int8 my_string[128];
RESET_BCC;  //bcc=0     
     sprintf(my_string,
     "F.F(00)\r\nC.1.0(%s)\r\n1.8.1(%f)\r\n1.8.2(%f)\r\n"
     "32.7(%lu)\r\nC.5.0(00)\r\n%C%C%C%C",
     METER_NO,energy_A,energy_A2,voltage,0x21,0x0d,0x0a,0x03);
     
     putc(0x02);                 //stx, startbit text
     printf("Length is %u\r\n", strlen(my_string));
     printf(putcsum, my_string); //print string
     putc(bcc);                  //as last, print checksum
Tagge



Joined: 23 Aug 2005
Posts: 93

View user's profile Send private message Visit poster's website

PostPosted: Fri Jan 20, 2006 6:03 am     Reply with quote

There was some stuff in the forum about this, but not any straight answers about the length. I use a PIC18F2525.
Why I want to make a long string and not make it in parts is that I must calculate the bcc of it.
/Tagge
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Jan 20, 2006 9:18 am     Reply with quote

The maximum size of string is dependable of the maximum RAM block size of the device
being used, so in you case it should be 256.

But in your code:
Code:

int8 my_string[128];
RESET_BCC;  //bcc=0     
     sprintf(my_string,
     "F.F(00)\r\nC.1.0(%s)\r\n1.8.1(%f)\r\n1.8.2(%f)\r\n"
     "32.7(%lu)\r\nC.5.0(00)\r\n%C%C%C%C",
     METER_NO,energy_A,energy_A2,voltage,0x21,0x0d,0x0a,0x03);
     


You are using the same name to define a data space block and a STREAM name.

int8 my_string[128];

sprintf(my_string, ....


The compiler should complain about this.


Humberto
Ttelmah
Guest







PostPosted: Fri Jan 20, 2006 11:10 am     Reply with quote

He is using sprintf, and for this the first variable, is a string name.
However the sprintf is invalid, because of the split constant format string. This has to be a _single_ string.

Best Wishes
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Jan 20, 2006 12:10 pm     Reply with quote

Quote:

He is using sprintf,


Yep, RJ you are right. Had a confusion with fprintf.

Humberto
Guest








PostPosted: Sat Jan 21, 2006 12:49 am     Reply with quote

Hi, what do you mean by the "split constant format string"? Confused how should the string be written? I also had the string in one line but the same error occurs. The string is working fine like it is but I cant put any more in to it and I would like to have a few more bytes in it. But no more than about max 100 bytes.
Ttelmah
Guest







PostPosted: Sat Jan 21, 2006 3:39 am     Reply with quote

The format string, must be a single constant string. If you need to go onto multiple lines, use the \ line extender character.
Code:

   int8 my_string[128];
   int16 voltage;
   float energy_A2;
   float energy_A;
   char METER_NO[8];
   sprintf(my_string, "F.F(00)\r\nC.1.0(%s)\r\n1.8.1(%f)\r\n1.8.2\
   (%f)\r\n32.7(%lu)\r\nC.5.0(00)\r\n%C%C%C%C",
   METER_NO,energy_A,energy_A2,voltage,0x21,0x0d,0x0a,0x03);

Compiles without complaint for me.
Howver I have to query 'why' you are coding as you are?. The 'point' of the code I posted before, is that it calculates the checksum as the data is sent, using the normal printf, and gets rid of the need to have a temporary string to hold the output data, and the large amounts of time involved in accessing such an array. You appear to be using this code, since you are including the 'RESET_BCC' define, yet are still wasting space sticking all the data into an output string.

Best Wishes
Tagge



Joined: 23 Aug 2005
Posts: 93

View user's profile Send private message Visit poster's website

PostPosted: Mon Jan 23, 2006 2:33 am     Reply with quote

I cant compile your string eigther? I get the "string to long" error?
This following type works but I cant put anything else in the string?

Code:

printf(putcsum, "F.F(00)\r\nC.1.0(%s)\r\n1.8.1(%f)\r\n1.8.2(%f)\r\n"
   "32.7(%lu)\r\nC.5.0(00)\r\n%C%C%C%C",
   METER_NO,energy_A,energy_A2,voltage,0x21,0x0d,0x0a,0x03);

//but not this, makes also "string too long" and its copied from yours? :
printf(putcsum, "F.F(00)\r\nC.1.0(%s)\r\n1.8.1(%f)\r\n1.8.2\
   (%f)\r\n32.7(%lu)\r\nC.5.0(00)\r\n%C%C%C%C",
   METER_NO,energy_A,energy_A2,voltage,0x21,0x0d,0x0a,0x03);


And yes, I used your code, its very good!
I just tried different ways to get around this problem with the string length.
Has it anything to do with my compiler version?
I got the 3.210
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Jan 23, 2006 8:21 am     Reply with quote

Quote:

Has it anything to do with my compiler version?
I got the 3.210


Quote:


3.210 A bug in the printf %f is fixed
3.212 A %u and %d bug is fixed
3.215 A problem with %u during printf redirection is fixed
3.216 A problem with 32 bit constants in a printf with %lx is fixed
3.223 Formatting inconsistancies with %U and %D are fixed
3.224 A problem with the %02d format in printf is fixed
3.225 Some formatting issues with %LD are fixed
3.231 New printf specifications %g and %w added and %f improved
3.232 printf %f now puts a leading 0 for numbers less than 1
3.233 The %f, %w and %g formats have been adjusted to be more consistant
3.233 %w now accepts signed numbers
3.234 Still more %f adjustments. It should now solve everyones problems.
3.237 Left justification has been added to %f, use %x.yf where x<=y to get left justification



Confused ...... Question

I would test with another version.


Humberto
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