sprintf Issue

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

sprintf Issue

 
0
  #1
Feb 7th, 2009
I have a code snippet that strangely works one way on a fedora 6 machine and another way on a fedora 8 machine.

  1. void Form1::SendToApp()
  2. {
  3. vector<int> isciistream;
  4. vector<int> fontstream;
  5. cout<<"Entering Form1::SendToApp()"<<endl;
  6. isciistream=wwnd->OnOK();
  7. isciistream.push_back(32);
  8. char str[255]="";
  9. cout<<"Size of IsciiStream:"<<isciistream.size()<<endl;
  10. for (int i=0;i<(int)isciistream.size();i++)
  11. {
  12. sprintf(str,"%s%c",str,isciistream[i]);
  13. cout << "isciistream[i] :" << isciistream[i]<< " str:"<<str[i]<<endl;
  14. }
  15.  
  16. QString temp=str;
  17. const char *foo = temp.ascii();
  18. cout<<"foo:"<<(int)foo[0]<<","<<(int)foo[1]<<","<<(int)foo[2]<<endl;
  19. }

for eg. if isciistream.size=3
isciistream[0]=204
isciistream[1]=32
isciistream[2]=32

on a fedora 6 machine the output i get is
foo: -52,32,32
which is correct.
in a fedora 8 machine i get
foo:32,0,43
I'm not sure why...

any help is appreciated
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: sprintf Issue

 
0
  #2
Feb 7th, 2009
> sprintf(str,"%s%c",str,isciistream[i]);
1. Almost all C library functions have undefined behaviour when the source and destination overlap. You're reading from the string you're printing into.

2. QString temp=str;
> const char *foo = temp.ascii();
If you print out the bytes of str before this, are they OK?
Perhaps QString defaults to a wide char on F8
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Re: sprintf Issue

 
0
  #3
Feb 7th, 2009
2. no..theyre messed up.
it says -52,0,0

1. BRILLIANT!

i used a temp variable for the sprint f and it worked!!!!!!!! thanks a ton!
Last edited by AutoC; Feb 7th, 2009 at 12:31 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC