C++ C style strings

Questions about programming languages and debugging
Post Reply
User avatar
visser
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 472
Joined: 03 Apr 2007, 16:00
17
Location: online
Contact:

C++ C style strings

Post by visser »

Hey suck-o how have yall been?

I need a lil bit of help here....

im working with some c++ and getting really into it. Fell asleep today in work, my eyes were blood shot and i kept zoning out... my boss was quick to smell to make sure i smelled like the mortar i was working with lololololol

anyways tho heres the deal. I have a couple of variables set up. I HAVE to use a c style string for the ofstream arguments im running so thats set in stone. I also HAVE to be using this certain integer because it runs off of my while loop so without it we get an infinite for loop and thats a problem (i use that integer variable to incriment so it has to be a int type variable). What i really really want to do is to add the value of that value of that variable to the end of my c style ctring... but i cant figure out how... Ive tried using strcat () and ive tried using strcpy() but when i do that my compiler (G++) gets super pissed off that i used an int in place of a char type deal.

the integer could be very small or very large (1-9999) but regardless i just need it on the end of this darned c string!!!! Any help would be so nice lol

User avatar
f4Gg0t_43
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 245
Joined: 13 Sep 2008, 16:00
15
Contact:

Post by f4Gg0t_43 »

Code: Select all

#include<sstream>
int main(){
int a=99999;
std::ostringstream b;
b<<a;
/* b.str(); = string of a, b.str()[0] would be a char */
}
[/code]

User avatar
visser
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 472
Joined: 03 Apr 2007, 16:00
17
Location: online
Contact:

Post by visser »

Awesome thanks for the help!
dont know if that works or not because i found this just now actually:

http://www.cppreference.com/wiki/c/io/sprintf


that will actually return a c style string for me no problem. im working with it now and will be putting in another late night

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

Both methods should work. Note that the first method is more C++y. sprintf is from C.

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Post by IceDane »

As a general principle, you should avoid using C stuff when you're using C++. sprintf is C, and there isn't an overloaded version of it that takes a string. You would need an intermediary character array, which you would then convert to a string.

Use the string streams.

User avatar
visser
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 472
Joined: 03 Apr 2007, 16:00
17
Location: online
Contact:

Post by visser »

IceDane wrote:As a general principle, you should avoid using C stuff when you're using C++. sprintf is C, and there isn't an overloaded version of it that takes a string. You would need an intermediary character array, which you would then convert to a string.

Use the string streams.
i dont agree with this at all. I have seen reports multiple times where using c in certain cases your program compliles and runs faster. alsooooo I am using ifstream to open a txt file. here is the reference on it from cplusplus.com

http://www.cplusplus.com/reference/iost ... ream/open/

notice that it accepts filename and it can be a c string instead of something in quotes.


this brings up a new question then:
iostream is a standard library that everybody uses in there programs. the very first programs u learn in c++ use iostream. to output text you use cout. notice the c... and to input u use cin. cin has a function in it called getline and that inputs a cstyle string easily..... sooo stick to c++ yet c++ has many references RIGHT back to c....
whats up with that exactly?

User avatar
f4Gg0t_43
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 245
Joined: 13 Sep 2008, 16:00
15
Contact:

Post by f4Gg0t_43 »

visser wrote:notice that it accepts filename and it can be a c string instead of something in quotes.
What exactly do you think a c string is? An array of chars? char a[]="whatever";func(a); is the same as func("whatever");

Also strings are a lot more customizable, and if it was func(char* a); you could easily do func(string.c_str()); to convert a string to an array of chars.

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Post by IceDane »

visser wrote:
IceDane wrote:As a general principle, you should avoid using C stuff when you're using C++. sprintf is C, and there isn't an overloaded version of it that takes a string. You would need an intermediary character array, which you would then convert to a string.

Use the string streams.
i dont agree with this at all. I have seen reports multiple times where using c in certain cases your program compliles and runs faster. alsooooo I am using ifstream to open a txt file. here is the reference on it from cplusplus.com

http://www.cplusplus.com/reference/iost ... ream/open/

notice that it accepts filename and it can be a c string instead of something in quotes.


this brings up a new question then:
iostream is a standard library that everybody uses in there programs. the very first programs u learn in c++ use iostream. to output text you use cout. notice the c... and to input u use cin. cin has a function in it called getline and that inputs a cstyle string easily..... sooo stick to c++ yet c++ has many references RIGHT back to c....
whats up with that exactly?
As you seem to be pretty oblivious of what it is you are talking about, I'll take a moment to explain.

A C-style string is an array of characters:

Code: Select all

char *ptr = "String"; // Or
char array[] = "Lulz";
This is a C++-style string:

Code: Select all

string Foo = "Bar";
sprintf accepts a character array. That's a C-style string. C++ has a string class in its standard set of libraries - that's what you should be using if possible.

cout, cin and all the other cs in the C++ I/O objects don't stand for "C" - why would they? I suspect they stand for console, and a bit of google supports this claim.

If you would ever notice a huge performance difference between sprintf and string streams, string streams would be rewritten and so on. The performance difference between C and C++ is absolutely miniscule in most cases.

User avatar
f4Gg0t_43
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 245
Joined: 13 Sep 2008, 16:00
15
Contact:

Post by f4Gg0t_43 »

This is a list of all the members of the string class.

http://www.cplusplus.com/reference/string/string/

Post Reply