C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Concatenation Posted by Faz on 7 Jun 2006 at 5:21 AM
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 ## ?
Report
Re: Concatenation Posted by Lundin on 7 Jun 2006 at 6:22 AM
: 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 ## ?
:


In C:

#include <string.h>

const int MYSIZE = somethingLargeEnough;
char myStr[MYSIZE];
char extension[] = ".txt";
char* ptr = func();
strcpy(myStr, ptr);
strcat(myStr, extension);


In C++:

#include <string>
using namespace std;

string myStr;
string extension = ".txt";
myStr = func();
myStr = myStr + extension; // or myStr += extension;


The C way is the most efficient.

Report
Re: Concatenation Posted by Faz on 7 Jun 2006 at 6:37 AM
: : 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 ## ?
: :
:
:
: In C:
:
: #include <string.h>
:
: const int MYSIZE = somethingLargeEnough;
: char myStr[MYSIZE];
: char extension[] = ".txt";
: char* ptr = func();
: strcpy(myStr, ptr);
: strcat(myStr, extension);
:
:
: In C++:
:
: #include <string>
: 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.
Report
Re: Concatenation Posted by stephl on 7 Jun 2006 at 6:44 AM
: : : 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 ## ?
: : :
: :
: :
: : In C:
: :
: : #include <string.h>
: :
: : const int MYSIZE = somethingLargeEnough;
: : char myStr[MYSIZE];
: : char extension[] = ".txt";
: : char* ptr = func();
: : strcpy(myStr, ptr);
: : strcat(myStr, extension);
: :
: :
: : In C++:
: :
: : #include <string>
: : 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
Report
Re: Concatenation Posted by Faz on 7 Jun 2006 at 6:59 AM
Excellent, thanks!
Why didn't I just try compiling it in the first place!?
Report
How can I convert strings to char variables? Posted by Faz on 8 Jun 2006 at 4:28 AM
It turns out that a string cannot be used, it needs to be a char* type.
Is there a simple function somewhere that can take a string as a parameter and return a char*?

Cheers.
Report
Re: How can I convert strings to char variables? Posted by Lundin on 8 Jun 2006 at 4:41 AM
: It turns out that a string cannot be used, it needs to be a char* type.
: 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());
Report
Re: How can I convert strings to char variables? Posted by Faz on 8 Jun 2006 at 5:23 AM
: : It turns out that a string cannot be used, it needs to be a char* type.
: : 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.
Report
Re: How can I convert strings to char variables? Posted by Lundin on 8 Jun 2006 at 5:32 AM
: : : It turns out that a string cannot be used, it needs to be a char* type.
: : : 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.

Report
Re: How can I convert strings to char variables? Posted by HK_MP5KPDW on 8 Jun 2006 at 6:34 AM
: : : : It turns out that a string cannot be used, it needs to be a char* type.
: : : : 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.
:
:

Technically it returns a const char * and you also need to be carefull you don't modify the string in between the point where you call the c_str member function and where you actually use the data at the address returned from that call or the pointer may be invalidated.

string str = "Hello";
const char * ptr = str.c_str();
cout << ptr << endl;  // OK, ptr still points to valid address
str += " world!";     // str has been modified, previous address 
                      // returned by c_str might not be any good
cout << ptr << endl;  // Not OK, ptr's address may have been invalidated 


If you are trying to open a file using a string, you simply call the c_str member function in the constructor for the file stream object or the open call.

string fname("myfile.txt");
ofstream output(fname.c_str());

or...
string fname("myfile.txt");
ofstream output;
...
output.open(fname.c_str());

or if you wanted to use C FILE pointers for some reason...
string fname("myfile.txt");
FILE * output = fopen(fname.c_str(),"w");

Report
Re: How can I convert strings to char variables? Posted by FDrache on 8 Jun 2006 at 9:02 AM
Outgoing from char arrays, when you want to concatenate them, there is the strcat() function.
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


Report
Re: Concatenation Posted by HK_MP5KPDW on 8 Jun 2006 at 5:01 AM
: : 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
:

If you have MS Visual Studio C++ you can create a text file called UserType.Dat 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 C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin folder. My file currently has a few extra type other than the intrinsic types and looks like this:

UserType.Dat
deque
list
map
multimap
multiset
queue
set
stack
string
vector





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.