943,752 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 408
  • C++ RSS
Feb 7th, 2009
0

sprintf Issue

Expand Post »
I have a code snippet that strangely works one way on a fedora 6 machine and another way on a fedora 8 machine.

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008
Feb 7th, 2009
0

Re: sprintf Issue

> 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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Feb 7th, 2009
0

Re: sprintf Issue

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: New to C++
Next Thread in C++ Forum Timeline: template specialization for template member function?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC