char pointer manipulation

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
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>.
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
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
...
Last edited on
Because using std::string is better for string concatenation than char*.
Registered users can post here. Sign in or register to post.