Skip to main content
gamedev.net
🔒 Locked

SPRINTF Problem?

Started by C0UNT Z3R0 7 replies 450+ views
Original Post
C0UNT Z3R0
C0UNT Z3R0
Hi people, Am having some problems with sprintf, or maybe there is something better I can used for the problem. I want to: - take a number - convert it into a string - use the string when creating a file e.g. "test" num ".wav" where num is the converted number Here is the code I have which doesnt work but I think im along the right lines if someone could help me out id be very greatful. ----------------------------------------------- int num = 1; char buf[25]; sprintf(buf, "int = %i\n", num); string dav = "test", buf, ".wav"; //I know this doesnt work (need to know how to do this) WaitForSingleObject(mutexHandle, INFINITE); FD_SET(clientSocket, &masterSet); ReleaseMutex(mutexHandle); printf("client on %d connected\n", clientSocket); ofstream myFile(dav, ios::app | ios::binary); num ++; ----------------------------------------------- Thanks
Jingo
Jingo

#include<sstream>

std::stringstream str;
str << "test" << num << ".wav";
std::string dav = str.str();
UltimaX
UltimaX
int num = 1;
char buf[256];
sprintf(buf, "test%d.wav", num);

You don''t need to use a string at all. Buf will now have the filename you were after.

-UltimaX-
Ariel Productions
|Designing A Screen Shot System|

"You wished for a white christmas... Now go shovel your wishes!"
C0UNT Z3R0
C0UNT Z3R0
Cheers just checkin it out now looks good
C0UNT Z3R0
C0UNT Z3R0
include

std::stringstream str;
int num = 1;
str << "test" << num << ".wav";
std::string dav = str.str();

WaitForSingleObject(mutexHandle, INFINITE);
FD_SET(clientSocket, &masterSet);
ReleaseMutex(mutexHandle);

printf("client on %d connected\n", clientSocket);
ofstream myFile(dav, ios::app | ios::binary); //(1).
num ++;

(1). Causes an error:-

No user-defined-conversion operator available that can perform this conversion
T1mb0
T1mb0
dav.c_str() at that point

Those file streams don''t want std::strings for some reason i don''t know.
SiCrane
SiCrane
Try ofstream myFile(dav.c_str(), ios::app | ios::binary); instead.
C0UNT Z3R0
C0UNT Z3R0
Thanks so much guys, got it sorted!

C0UNT Z3R0
C0UNT Z3R0
Oh another quick thing...

Edited Code:
------------------------------------
std::stringstream str;
str << "test" << conum << ".wav";
std::string dav = str.str();

WaitForSingleObject(mutexHandle, INFINITE);
FD_SET(clientSocket, &masterSet);
ReleaseMutex(mutexHandle);

printf("client on %d connected\n", clientSocket);
ofstream myFile(dav.c_str(), ios::app | ios::binary);
conum ++;
-------------------------------------

When I set up the file:

ofstream myFile(dav.c_str(), ios::app | ios::binary);

How would I add the (conum) number to the end of the varible name e.g. myFile + conum = myFile1 etc

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.