adding to a string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

adding to a string

 
0
  #1
Mar 4th, 2005
(for reference i am in c++, windows, borland 3 compiler)hey guys here is what i want to do, i am trying to save a file using <fstream> but want the player to pick a name to save the file as, so to do that i have to modify the string they input to say the path and the file extension in it (thus able to just ofstream.open(savename) type of a thing). as i understood it before the charecter strings are stored as arrays so i would use two strings and basically combine them using one spot at a time. but when i tried this it didn't work, any ideas?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: adding to a string

 
0
  #2
Mar 4th, 2005
When working with C-style strings, the first thing you need to remember is to have enough memory. If you're going to tack a string onto another string, make sure that the destination string has enough memory to hold both strings plus one null character.

You can concatenate strings with the C library function strcat, or strncat.
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char string1[] = "This is ";
  9. char string2[] = "a string";
  10. char string3[17];
  11.  
  12. strcpy(string3, string1);
  13. strcat(string3, string2);
  14.  
  15. cout<<'|'<< string3 <<'|'<<endl;
  16. }
Other alternatives include using strcpy (or strncpy) with an offset:
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char string1[] = "This is ";
  9. char string2[] = "a string";
  10. char string3[17];
  11.  
  12. strcpy(string3, string1);
  13. strcpy(string3 + strlen(string1), string2);
  14.  
  15. cout<<'|'<< string3 <<'|'<<endl;
  16. }
However, those options are prone to error. If you can get hold of a string class that handles all of this low level stuff behind the scenes, you'll be better off. It's considered a rite of passage to implement your own string class, if you feel so inclined.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 7
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: adding to a string

 
0
  #3
Mar 5th, 2005
If you can get hold of a string class that handles all of this low level stuff behind the scenes, you'll be better off
Back in my 'ole highschool days in AP Computer Science class, we used a library called apstring, it's usage is similar to how java handles strings. Google it, may save you the effort.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: adding to a string

 
0
  #4
Mar 5th, 2005
std::string also works well

ie

  1. std::string str1 = "string 1 goes here";
  2. std::string str2 = "string 2 goes here";
  3.  
  4. str1 += str2; // add str2 to str1

std::string also works with the cout /cin stream objects ie cout << str1; works as does cin >> str1;
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: adding to a string

 
0
  #5
Mar 5th, 2005
>std::string also works well
Not if the compiler is too old to support it.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: adding to a string

 
0
  #6
Mar 5th, 2005
I thought <string.h> was part of the old c++ standard?
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: adding to a string

 
0
  #7
Mar 5th, 2005
>I thought <string.h> was part of the old c++ standard?
There is no "old" C++ standard, and if string.h declares a string class then it's a non-standard extension. string.h is a standard C header that declares functions and typedef's for working with C-style strings.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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