Skip to main content
gamedev.net
🔒 Locked

Another simple problem!?!

Started by C0UNT Z3R0 Apr 18, 2004 at 12:35 PM 7 replies 650+ views
Original Post
C0UNT Z3R0
C0UNT Z3R0
Thanks to the people who helped me out before I really appreciate it, just have another small thing to ask... ------------------------------------ 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 Thanks again
SiCrane
SiCrane
C++ lacks reflection, so what you want to do isn''t possible in the general case. You might want to try an array of ofstream objects.
C0UNT Z3R0
C0UNT Z3R0
OH GOD NO!
How would I go about doing that?

Thanks
C0UNT Z3R0
C0UNT Z3R0
So its not possible to use a string for a variable name at all?
nonpop
nonpop
You could do something like this:

#include <map>
#include <string>
#include <fstream>

map<string, ofstream> myFiles;

myFiles[dav].open (dav.c_str(), ios::app | ios::binary);

C0UNT Z3R0
C0UNT Z3R0
That seems to work I think, will not really know until ive done some more coding which actually uses it. It does give me 264 warnings though for some reason, most of which are to do with the map library, any idea why?
C0UNT Z3R0
C0UNT Z3R0
Hi again,

What happens if I now want to write data to the files outside the for loop. I thought I could just used:

myFile[dav].write(buffer, messageSize);

but it does not recognise [dav]. I thought this may be because it was only mapped with ofstream so I tried just fstream but that didnt work either. Is there anyway to make this [dav] string available outside the else and for statements?

--------------------------------------------------------
for (; {

unsigned int clientSocket = accept(mySocket, 0, 0);

if (clientSocket == SOCKET_ERROR) {

printf("Accept Failed!\n");
gQuitFlag = true;
return;

} else {
map myFile;
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);
myFile[dav].open(dav.c_str(), ios::app | ios::binary);

cout<conum ++;
cout<<"++\n";
}//end else
}//end for

--------------------------------------------------------

Thanks
nonpop
nonpop
Tons-of-warnings -problem:
Put
#pragma warning (disable: 1234)  

before the std #includes (the code is not 1234; you'll see the correct one in the compiler warnings list)

The other problem:
Is myFile and dav declared outside the loop? In your example, it's not (and thus it cannot be used outside it either)


[edited by - nonpop on April 19, 2004 12:05:49 PM]
C0UNT Z3R0
C0UNT Z3R0
Got it sorted now, stupid mistake!
Cheers nonpop

Topic Locked

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

Sign in to reply to this topic.