string gets truncated

Go To Last Post
39 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Dreth.

Now I'm faced with another problem.
I have a string named feedback[]="pvs \n Temp1: *C \n Temp2: *C \n Hum1: %RH \n Hum2: %RH.
If send this string as it is it's successfully received - this is what I need.

The moment I stringcopy strings for the temperature and humidity in their respective indices of the string / the left empty spaces ,
I receive only temperature values. The reset of the string gets truncated. In fact *C for temp2 is also chopped.
I don't understand why this is happening.
Any C language guru?

This topic has a solution.

Crazy generation

Last Edited: Sat. Jul 15, 2017 - 03:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The rest of the string gets truncated i.e values for humidity don't appear in the message.

Crazy generation

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is a new question - so start a new thread!

 

EDIT

 

Was originally tacked onto the end of this thread: http://www.avrfreaks.net/comment...

Last Edited: Sat. Jul 15, 2017 - 07:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Derick22 wrote:
The moment I stringcopy strings for the temperature and humidity in their respective indices of the string / the left empty spaces , I receive only temperature values. The reset of the string gets truncated. In fact *C for temp2 is also chopped.
Very likely because your copying inserts a string termination into 'feedback'.

 

Show the code for "the moment I stringcopy" to get a definite answer instead of a guess.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:
Show the code for "the moment I stringcopy"

 

But do it in a new thread !

 

ie, not this one!

 

EDIT

 

js did it for you!

Last Edited: Sat. Jul 15, 2017 - 07:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thread split.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Derick22 wrote:
The moment I stringcopy strings for the temperature and humidity in their respective indices of the string / the left empty spaces , I receive only temperature values. The reset of the string gets truncated.

Show code.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Derick22 wrote:
I have a string named feedback[]="pvs \n Temp1: *C \n Temp2: *C \n Hum1: %RH \n Hum2: %RH.

If you want to insert numbers in it, why don't you just use sprintf():

sprintf( feedback, "pvs \n Temp1: %d *C \n Temp2: %d *C \n Hum1: %d %RH \n Hum2: %d %RH", temp1, temp2, hum1, hum2 );

 

I stringcopy strings for the temperature and humidity in their respective indices of the string

You can't just insert strings into other strings - that will over-write!

The string won't automatically "expand" to accommodate the extra characters.

 

You would need to have the right amount of space - with "dummy" characters - in the template string, and replace the dummies.

 

 

Last Edited: Sat. Jul 15, 2017 - 08:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
You would need to have the right amount of space - with "dummy" characters - in the template string, and replace the dummies

And, as sternst suggested in #4, take care not to insert a premature terminator!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

How to post source code: http://www.avrfreaks.net/comment...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Extact for the; the moment I stringcopy...

Attachment(s): 

Crazy generation

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, come on!

Did you actually read any of the previous replies??

 

 

How to post source code: http://www.avrfreaks.net/comment/2153911#comment-2153911

Last Edited: Sat. Jul 15, 2017 - 12:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But I'm using an android phone.

Crazy generation

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Go on.

 

Android is perfectly capable of copying & pasting text.

 

But I think the other replies have already addressed the problem.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Derick22 wrote:
But I'm using an android phone.

Even if your phone is incapable of copy/pasting we don't care. It is up to you to produce something that we can work with. E.g. test run. Or even read without breaking our necks. ;-)

 

Curious: Are you really writing and building code for the AVR on an Android phone? If so, you're one of my new heroes..

 

I don't think so. You used the phone to photograph a screen of a PC where you write and build your AVR code. Is that PC not capable of browsing the web - e.g. the AVRfreaks site?

 

And, as have been said above: Why go through the very involved string copying, when sprintf() will do the string buildup for you in a safe and controlled manner. Are you very low on flash memory?

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Sat. Jul 15, 2017 - 02:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

JohanEkdahl wrote:
Curious: Are you really writing and building code for the AVR on an Android phone?

Exactly -- the "defense" seems to have holes.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hahaha! My Android is potable but not my desktop. Hope you now understand.

I sometime back used sprinf() function and managed to buid a code l was working on.

Unfortunately I had to do some additional work on the linker for it to work as intended.
Otherwise I was displaying only question marks i.e ???

Crazy generation

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

theusch wrote:

JohanEkdahl wrote:
Curious: Are you really writing and building code for the AVR on an Android phone?

Exactly -- the "defense" seems to have holes.

My guess: he is using the phone for posting (and making the photo), the code is on a PC without an internet connection.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fine, let's now focuss on the problems.
Has anyone else been face with the sprintf problem?
I used AVR studio 7 but initially studio 6.

Crazy generation

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Derick22 wrote:
Otherwise I was displaying only question marks i.e ???
Derick22 wrote:
Has anyone else been face with the sprintf problem?

I have seen the '?'s when trying to print float values without having enabled the float library, but not for ints.

See this link.

 

Edit: It always helps if you say exactly what you are doing (example code), the input you provide, what you expect (what you want the output to be) and what you are actually observing.

David (aka frog_jr)

Last Edited: Sat. Jul 15, 2017 - 03:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Derick22 wrote:
Otherwise I was displaying only question marks i.e ???

Derick22 wrote:
Has anyone else been face with the sprintf problem?

Yes. Thousands of avr-gcc beginners.. ;-)

 

By default printf() does not handle floats, but instead displays question marks. This is to save flash memory - everything associated with floats is relatively expensive on AVRs (since they have no hardware for floating point data but needs to do it all in firmware).

 

To enable floats in printf() do as follows: In Studio,

  1. Open the project Options
  2. Under Toolchain, AVR/GNU Linker, Miscellaneous enter the following into the text box Other Linker Flags-u,vfprintf -lprintf_flt
  3. Under Toolchain, AVR/GNU Linker, Libraries, make sure the Libraries list contains this: libm

 

That should do it. Please understand that you will be hit by a "fixed cost" increase in flash consumption of about 1.5 KB.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I expect this output;

Pvs
Temp1: value *C
Temp2: value *C
Hum 1: value %RH
Hum2: value %RH

I have tried the sprintf function ie

sprinf("pvs \n Temp1: %2.1f *C \n Temp2 : %2.1f *C \n Hum1: %2.1f %RH \n Hum2: %2.1f %RH",float1, float2, float3, float4);

When I try this l only get the first four lines correct.
The last line is trancated.
Also the %RH symbol for line four is trancated.

Crazy generation

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually it is;
sprinf(feedback,"pvs \n Temp1: %2.1f *C \n Temp2 : %2.1f *C \n Hum1: %2.1f %RH \n Hum2: %2.1f %RH",float1, float2, float3, float4);

Crazy generation

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Change %RH into %%RH.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Show an example (or some examples) of the output you get.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Derick22 wrote:
I have tried the sprintf function ie sprinf("pvs \n Temp1: %2.1f *C \n Temp2 : %2.1f *C \n Hum1: %2.1f %RH \n Hum2: %2.1f %RH",float1, float2, float3, float4);

No, you haven't.

 

That is not the function call you have in your code.

 

The first parameter to sprintf() is the buffer into which sprintf() will place the result.

 

1. DO NOT type code into the forum. Use mark-copy from your actual code and then paste that into a post here. It is completely meaningless, and a waste of time, to try to debug code that isn't what you actually have. (Sorry if I sound harsh, but we've had an influx of such meaningless "aproximations of what is in the editor" the last few days..)

 

2. Once you post the actual call to sprintf() that you have, then also tell us the size of the string buffer you pass as the first parameter. And taken point 1 above to your heart you will of-course actually show us the definition of that buffer rather than just giving us a number. ;-)

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Derick22 wrote:
Any C language guru?

Actually, this is not guru stuff - this is basic textbook stuff.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, so now we've got the actual sprintf() call in a follow up post.

 

And that is marked as the solution. Care to tell future readers what the solution was? (Good forum manners..)

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
And that is marked as the solution.

But it is not the solution.

 

Stefan showed the correction required in #24.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

JohanEkdahl wrote:
And that is marked as the solution.

But it is not the solution.

 

Stefan showed the correction required in #24.

I know that. I'm asking the OP to make that clear. I don't care if he moves the "solution marker" or if he complements his post #23 with "the solution was what sternst said in #24". Just that he makes it clear to future readers what the solution was.

 

I find it highly ungrateful, and a "spoiled" attitude, when people get help to solve their problems and then just walks away without even a thought of giving something back to the community.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sure that was the solution...
Thank you for helping.

Crazy generation

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have found this community to be so helpful to me, in fact handy when I run into problems.

What sort of reward is expected so as to give back to the community.

Crazy generation

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Send £5 notes ;-) 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Derick22 wrote:
What sort of reward is expected so as to give back to the community.

IMO nothing more than feeding back when a problem is resolved.

 

Two days or three years from now someone else has a very similar problem and types "sprintf string truncated" into a search box. How frustrated will they be if they find a thread that matches their problem but there is no clearly marked solution? How happy will they be if they find such a thread and there is a clearly marked solution?

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Sat. Jul 15, 2017 - 05:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Having marked a post as "the solution", can the OP change it?

 

Can several posts be marked as "the solution"?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Andy, have you never started a thread? Surely you must have one where you could try the experiment?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

My Android is potable

Wow you can DRINK your Android?? I'll try drinking mine now......

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

string gets truncated

Caught in the act -- string being truncated:

Image result for cutting string

Image result for cutting string

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Andy, have you never started a thread? Surely you must have one where you could try the experiment?

So I tried that:

 

Having marked a post as "the solution", can the OP change it?

Yes - the button changes to 'Unmark Solution'

smiley

 

Can several posts be marked as "the solution"?

No - the other 'Mark as Solution' buttons disappear when one post has been marked.

sad

 

But there is, of course, a bug in this: although the button you click does immediately change to 'Unmark Solution', the other buttons don't disappear until you manually update/refresh the page - so you can try pressing another button, but it gives you HTTP 403.

 

Also, the green highlighting on the marked post & the OP don't appear until you manually refresh.

 

I guess this is related to the way the stars don't update properly ... 

 

<rolls eyes>

 

EDIT: Also, although the 'Unmark Solution' button you click does immediately change back to 'Mark Solution', the other buttons don't re-appear and the green highlighting isn't removed until after a manual refresh.

 

EDIT 2:

 

Rather than further derail this thread, I have raised this as a separate thread in Housekeeping:

 

http://www.avrfreaks.net/forum/f...

 

Last Edited: Mon. Jul 17, 2017 - 07:28 AM