It is currently Thu Feb 22, 2018 9:42 am

All times are UTC - 5 hours

 Page 1 of 1 [ 4 posts ] 
Author Message
 Post subject: Need Help With String Manipulation
PostPosted: Fri Oct 07, 2011 1:54 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 452
My objective is to pass a pointer of a char[] into a function. This char[] will hold the file's name and extension. I already know how to pull out the extenstion. But what I am trying to do, is the take everything in the string up to and including the "." then I wan't to contatinate this name with a different exetension. Any help would help very much.

int foo( FILE *fp, char *szFilename ) {

   // Test If Filename Is Valid
   int iLength = (int)strlen( szFilename ); // szFilename is correct in debugger
   if ( iLength < 4 ) {
      return -1;
   // Make A Local Copy Of Filename
   char *szFile;
   unsigned int uiSize = strlen( szFilename ) + 1;
   szFile = new char[uiSize];
   strcpy_s( szFile, uiSize, szFilename );
   _strlwr_s( szFile, uiSize );             // Local is correct in debugger

   char szType[5];
   // Get Last 3 Character Of The Filename
   strcpy_s( szType, 5, &szFile[iLength-3] );
   // Convert szType To Lower Case
   _strlwr_s( szType, 5 );                  // szType is .ext in debugger
   // Below Is where Im stuck, Not sure How to create this new char and
   // Copy in the " somefile. " where somefile is unsigned

   // Get The Name Of The File
   char *szName = new char[uiSize-5];
   memset( szName, 0, uiSize-5 );
   for ( unsigned int uiIndex = 0; uiIndex < (uiSize-5); uiIndex++ ) {
      // Assertion Error Thrown Here
      strcpy_s( &szName[uiIndex], (uiSize-4), &szFile[uiIndex] ); 
   // Once Top Is Right And I Have The Random File Name In szName. Then I need To
   // Use strncat_s() on it to append .txt and add the null terminator, if needed
   // After I Have The original name with the new extension, I then want to write it to a file.

   // More Code Below
   // ...
   // ...

   return 0;
} // foo

int main() {

int main() {
char szFilename[256];
memset( szFilename, 0, 256 );
_snprintf_s( szFilename, 255, 255, "somefile.ext" );  // .ext is short for extension
szFilename[255] = 0;

   FILE *fp;
   fopen_s( &fp, szFilename, "rb" );
   if ( !fp ) {
      printf( "Can not open file.\n" );
   return -1;

   printf( "file open.\n" );

   foo( fp, szFilename );

   fclose( fp );

return 0;

This would help me out alot, and Thank you in advance. If there is an easier way to do the above that would be helpful too. Thank You!

 Post subject: Re: Need Help With String Manipulation
PostPosted: Fri Oct 07, 2011 11:25 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 452
I was able to get the above code to work, you can refer to my post in the OpenGL series in The Caligari Parser section.

 Post subject: Re: Need Help With String Manipulation
PostPosted: Tue Jan 17, 2012 8:05 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 452
I have this code block I am working on but I keep getting an unhandled exception

// -----------------------------------------------------------------------
// SearchFileNames()
// Search Through Our Vector For Filenames To See If It Already Exists
unsigned int ImageFile::SearchFileNames( const char *szFilename ) {

   // You Do Not Have To Check Our std::vector Of std::strings To
   // See If .end() == .begin() Since We Will Always Have At Least
   // One String Saved. This Happens The Moment Our Constructor Is
   // Called. If A Filename Doesn't Exist It Is Automatically Pushed
   // Back. If We Are Inside This Function. This Also Means We Have
   // The Right File Type.
   unsigned int uiIndex;

   // Iterate Through Our Names And Check If File Name Already Exists
   for ( int i = 0; i < (int)_vFileNames.size(); ++i ) {
      if ( strcmp( _vFileNames[i].c_str(), szFilename ) == 0 ) {
         // Match Found
         printf( "Filename already exists. Do you want to rename this file?\n" );
         char* c = "";
         gets_s( c, 1 );
         _strlwr_s( c, 1 );
         if ( strcmp( c, "y" ) == 0 ) {
            // User Wants To Overwrite
            _vFileNames[i] = Format( szFilename );

            // More Here Loading Opening Modifiying Etc...
            // Howerver, This Can Be Done Outside Of This
            // Function Call Using Our Index Value To Find
            // Its File Pointer And Filename.

            return uiIndex = (unsigned int)_vFileNames.size();
         } else if ( strcmp( c, "n" ) == 0 ) {
            // User Does Not Want To Overwrite
            return uiIndex = (unsigned int)_vFileNames.size();
      } else {
         // File Not Found Add To Our Vector
         _vFileNames.push_back( Format( szFilename ) );
         return uiIndex = (unsigned int)_vFileNames.size();
   return uiIndex = (unsigned int)_vFileNames.size();   

} // SearchFileNames

even if i replace gets_s( c, 1 ); with std::cin >> c; or change char c* = ""; to a char c; and replace gets_s( c, 1 ) with getchar. and replace any place where c is used with &c, I still get errors. LOL
All three ways compiles and builds but I keep getting runtime errors.

I just want the user to press a y/n to make a choice... and to press enter afterwards...

 Post subject: Re: Need Help With String Manipulation
PostPosted: Wed Jan 18, 2012 6:00 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1106
Location: Ontario Canada
The problem here is that you are not declaring any memory to store the characters that the user enters. You declared a pointer char* c however gets_s expects to have room for some memory.

replace char* c with something like char c[max_size]; Now you have some room to store the results.

Also make sure you check the return value of gets_s to make sure that your buffer was actually large enough to store the string that you tried storeing.

Look here for sample usage: ... c7(v=vs.80).aspx

Display posts from previous:  Sort by  
 Page 1 of 1 [ 4 posts ] 

All times are UTC - 5 hours

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to: