| | |
adding to a string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
(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?
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.
Other alternatives include using strcpy (or strncpy) with an offset:
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.
You can concatenate strings with the C library function strcat, or strncat.
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> using namespace std; int main() { char string1[] = "This is "; char string2[] = "a string"; char string3[17]; strcpy(string3, string1); strcat(string3, string2); cout<<'|'<< string3 <<'|'<<endl; }
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> using namespace std; int main() { char string1[] = "This is "; char string2[] = "a string"; char string3[17]; strcpy(string3, string1); strcpy(string3 + strlen(string1), string2); cout<<'|'<< string3 <<'|'<<endl; }
I'm here to prove you wrong.
•
•
•
•
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
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
std::string also works well
ie
std::string also works with the cout /cin stream objects ie cout << str1; works as does cin >> str1;
ie
C++ Syntax (Toggle Plain Text)
std::string str1 = "string 1 goes here"; std::string str2 = "string 2 goes here"; 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:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Graphics in Pixel,Mode13h:Part 1
- Next Thread: Word Counting
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






