(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.

#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;
}

Other alternatives include using strcpy (or strncpy) with an offset:

#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;
}

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. ;)

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.

std::string also works well

ie

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;

>std::string also works well
Not if the compiler is too old to support it.

I thought <string.h> was part of the old c++ standard?

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.