char pointer manipulation
Jun 22, 2022 at 9:12pm UTC
I am using an old directX game engine that uses char* to load file paths. How do I iterate through files like a counter to load multiple files EX.
1 2 3 4 5 6 7 8 9 10 11 12 13
char * sFileName = "Level" ;
char * sFormat = ".x" ;
int iLevelNumber = 1;
for ( i = 1; i < 26; i++)
{
loadLevel(sFileName + iLevelNumber + sFormat);
//something that reads like this
Level1.x
Level2.x ...etc
}
Last edited on Jun 22, 2022 at 9:13pm UTC
Jun 22, 2022 at 11:24pm UTC
Use a
std::string
. When you call the function, do so with the
.c_str()
method:
1 2 3 4 5 6
std::string fileName = "Level" ;
std::string extension = ".x" ;
for (n = 1; n < 26; n++)
{
loadLevel( (fileName + std::to_string(n) + extension).c_str() );
}
If you are using C++20 you can also use the various stuff found in
<format>
.
Jun 22, 2022 at 11:38pm UTC
When i use the string lib from c++ i get this
1 2 3 4 5 6 7 8 9
1>Linking...
1>libcpmtd.lib(xdebug.obj) : warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)
1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)
1>libcpmtd.lib(stdthrow.obj) : error LNK2019: unresolved external symbol __CrtDbgReportW referenced in function "void __cdecl std::_Debug_message(wchar_t const *,wchar_t const *,unsigned int)" (?_Debug_message@std@@YAXPB_W0I@Z)
1>Debug\chunkload.exe : fatal error LNK1120: 3 unresolved externals
1>Build log was saved at "file://c:\Users\Administrator\Documents\Visual Studio 2008\Projects\chunkload\chunkload\Debug\BuildLog.htm"
1>chunkload - 4 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on Jun 22, 2022 at 11:38pm UTC
Jun 23, 2022 at 12:10am UTC
after some tinkering in the visual studio settings I found if I go to the project properties and go to C++ -> code generation and Changed it to MT/ (Multi threaded) it did the trick
Jun 25, 2022 at 11:22am UTC
...
Last edited on Jun 25, 2022 at 12:24pm UTC
Jun 25, 2022 at 12:03pm UTC
Because using std::string is better for string concatenation than char*.
Topic archived. No new replies allowed.