In Visual Basic, I could concatenate so easily with the '&' operator. Of course in C++ it's the 'address of' operator, which is a pain. I'm just trying to concatenate the return char* from a function with ".txt".
The reason being the function returns the char* of a name and I want the program to create a txt file called whatever the name is.
i.e. The function returns "Faz", I want that to concatenate with ".txt" so I can create a a file called "Faz.txt".
Do I need to use the macros to concatenate with ## ?
Comments
: The reason being the function returns the char* of a name and I want the program to create a txt file called whatever the name is.
: i.e. The function returns "Faz", I want that to concatenate with ".txt" so I can create a a file called "Faz.txt".
:
: Do I need to use the macros to concatenate with ## ?
:
In C:
#include
const int MYSIZE = somethingLargeEnough;
char myStr[MYSIZE];
char extension[] = ".txt";
char* ptr = func();
strcpy(myStr, ptr);
strcat(myStr, extension);
In C++:
#include
using namespace std;
string myStr;
string extension = ".txt";
myStr = func();
myStr = myStr + extension; // or myStr += extension;
The C way is the most efficient.
: : The reason being the function returns the char* of a name and I want the program to create a txt file called whatever the name is.
: : i.e. The function returns "Faz", I want that to concatenate with ".txt" so I can create a a file called "Faz.txt".
: :
: : Do I need to use the macros to concatenate with ## ?
: :
:
:
: In C:
:
: #include
:
: const int MYSIZE = somethingLargeEnough;
: char myStr[MYSIZE];
: char extension[] = ".txt";
: char* ptr = func();
: strcpy(myStr, ptr);
: strcat(myStr, extension);
:
:
: In C++:
:
: #include
: using namespace std;
:
: string myStr;
: string extension = ".txt";
: myStr = func();
: myStr = myStr + extension; // or myStr += extension;
:
:
: The C way is the most efficient.
:
:
Thing is, I've tried the 'string' header before, but when I declare a variable of the string class it doesn't highlight the type like it does with other types. An example being - in my IDE words like char and int automatically highlight as bold. When I write: 'string myStr;' it doesn't look like string is a proper type.
: : : The reason being the function returns the char* of a name and I want the program to create a txt file called whatever the name is.
: : : i.e. The function returns "Faz", I want that to concatenate with ".txt" so I can create a a file called "Faz.txt".
: : :
: : : Do I need to use the macros to concatenate with ## ?
: : :
: :
: :
: : In C:
: :
: : #include
: :
: : const int MYSIZE = somethingLargeEnough;
: : char myStr[MYSIZE];
: : char extension[] = ".txt";
: : char* ptr = func();
: : strcpy(myStr, ptr);
: : strcat(myStr, extension);
: :
: :
: : In C++:
: :
: : #include
: : using namespace std;
: :
: : string myStr;
: : string extension = ".txt";
: : myStr = func();
: : myStr = myStr + extension; // or myStr += extension;
: :
: :
: : The C way is the most efficient.
: :
: :
: Thing is, I've tried the 'string' header before, but when I declare a variable of the string class it doesn't highlight the type like it does with other types. An example being - in my IDE words like char and int automatically highlight as bold. When I write: 'string myStr;' it doesn't look like string is a proper type.
:
I think this behavior is normal.
I do not know any editor that highlights types which are defined in a header file. All the editors I know are only able to highlight predefined types that are intrinsic to the language (int, char). For example, the FILE type is not highlighted.
Steph
Why didn't I just try compiling it in the first place!?
Is there a simple function somewhere that can take a string as a parameter and return a char*?
Cheers.
: Is there a simple function somewhere that can take a string as a parameter and return a char*?
:
: Cheers.
:
If you check the string class in your C++ book (you have one, right?),
it should tell you to use string::c_str();
For example:
string str = "hello world";
printf("%s",str.c_str());
: :
: I think this behavior is normal.
: I do not know any editor that highlights types which are defined in a header file. All the editors I know are only able to highlight predefined types that are intrinsic to the language (int, char). For example, the FILE type is not highlighted.
:
: Steph
:
[blue]If you have MS Visual Studio C++ you can create a text file called [italic]UserType.Dat[/italic] using notepad or some other text editor and write in it the types you want the IDE to highlight when typing in your code. On my system this file needs to be created in the [italic]C:Program FilesMicrosoft Visual StudioCommonMSDev98Bin[/italic] folder. My file currently has a few extra type other than the intrinsic types and looks like this:
[b]UserType.Dat[/b]
[code]
deque
list
map
multimap
multiset
queue
set
stack
string
vector
[/code]
[/blue]
: : Is there a simple function somewhere that can take a string as a parameter and return a char*?
: :
: : Cheers.
: :
:
:
: If you check the string class in your C++ book (you have one, right?),
: it should tell you to use string::c_str();
:
: For example:
:
: string str = "hello world";
: printf("%s",str.c_str());
:
I'm not trying to print it to the screen. I need a char* variable. But the data that I have (which is now concatenated) is a string.
I want to assign a variable to the file name, but the file name MUST be a char* variable.
To summarize my problem, I have 2 strings, the first is simply a person's name (i.e. Gary) the second is ".txt". These 2 strings are concatenated to produce a FILE called "Gary.txt". Unfortunately at this point, "Gary.txt" is a string variable and it won't compile unless this is a char* variable.
: : : Is there a simple function somewhere that can take a string as a parameter and return a char*?
: : :
: : : Cheers.
: : :
: :
: :
: : If you check the string class in your C++ book (you have one, right?),
: : it should tell you to use string::c_str();
: :
: : For example:
: :
: : string str = "hello world";
: : printf("%s",str.c_str());
: :
: I'm not trying to print it to the screen. I need a char* variable. But
Yes... now look at the code I wrote and think about what it does.
str.c_str() returns a char*, I am simply passing it to printf() instead of letting a pointer point at it.
: : : : Is there a simple function somewhere that can take a string as a parameter and return a char*?
: : : :
: : : : Cheers.
: : : :
: : :
: : :
: : : If you check the string class in your C++ book (you have one, right?),
: : : it should tell you to use string::c_str();
: : :
: : : For example:
: : :
: : : string str = "hello world";
: : : printf("%s",str.c_str());
: : :
: : I'm not trying to print it to the screen. I need a char* variable. But
:
:
: Yes... now look at the code I wrote and think about what it does.
: str.c_str() returns a char*, I am simply passing it to printf() instead of letting a pointer point at it.
:
:
[blue]Technically it returns a [italic]const char *[/italic] and you also need to be carefull you don't modify the [italic]string[/italic] in between the point where you call the [italic]c_str[/italic] member function and where you actually use the data at the address returned from that call or the pointer may be invalidated.
[code]
string str = "Hello";
const char * ptr = str.c_str();
cout << ptr << endl; // OK, [b]ptr[/b] still points to valid address
str += " world!"; // [b]str[/b] has been modified, previous address
// returned by [b]c_str[/b] might not be any good
cout << ptr << endl; // Not OK, [b]ptr[/b]'s address may have been invalidated
[/code]
If you are trying to open a file using a [italic]string[/italic], you simply call the [italic]c_str[/italic] member function in the constructor for the file stream object or the [italic]open[/italic] call.
[code]string fname("myfile.txt");
ofstream output(fname.[b]c_str()[/b]);
[/code]
or...
[code]string fname("myfile.txt");
ofstream output;
...
output.open(fname.[b]c_str()[/b]);
[/code]
or if you wanted to use C FILE pointers for some reason...
[code]string fname("myfile.txt");
FILE * output = fopen(fname.[b]c_str()[/b],"w");
[/code][/blue]
A useful alternative, not only for strings, is sprintf().
char Filename[30];
char Name[20] = "Faz";
sprintf(Filename, "%s.txt", Name);
// Writes something type dependent into Filename