sprintf equivalent for the string class <string>

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emaghero
    New Member
    • Oct 2006
    • 85

    sprintf equivalent for the string class <string>

    Morning all,

    I am currently processing some data that is contained in several files in several different locations. The filenames contain descriptions of the data that I am working with, for example rect_W_7.2_H_3. 8.txt

    I am using the STL string class on MSVS to name the directories storing the files.
    Code:
    string drive="c:\\"
    string dir1="Output_Files\\";
    string dir2="Calculation_Data\\";
    string wg1="Rectangular\\";
    string loc=drive.append(dir1).append(dir2);
    Thus far I have been using sprintf_s to open the file that I want to work on, for example
    Code:
    char file[100];
    
    sprintf_s(file,100,rect_W_%0.1f_H_%0.1f.txt,width,height);
    
    string current=loc;
    
    current.append(file);
    
    ofstream data.open(current);
    This works fine and I havn't had any problems with it.

    I was wondering if there was a sprintf equivalent for strings. Since I will be working with files of variable name length I don't want to have to count into the filename and find the position of %0.1f and use insert every time.

    Thanks
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The equivilent is to use a stringstream which work much like cout/cin or any other file stream something like

    Code:
    #include <sstream>
    #include <iomanip>
    
    ...
    
    ostringstream ss;
    
    ss << loc << "rect_W_" << fixed << setprecision(1) << width << "_H_"  << fixed << setprecision(1) << height << ".txt";
    
    current = ss.str();

    Comment

    • emaghero
      New Member
      • Oct 2006
      • 85

      #3
      Originally posted by Banfa
      The equivilent is to use a stringstream which work much like cout/cin or any other file stream something like

      Code:
      #include <sstream>
      #include <iomanip>
      
      ...
      
      ostringstream ss;
      
      ss << loc << "rect_W_" << fixed << setprecision(1) << width << "_H_"  << fixed << setprecision(1) << height << ".txt";
      
      current = ss.str();
      Cheers, thanks very much.

      Comment

      • emaghero
        New Member
        • Oct 2006
        • 85

        #4
        Originally posted by Banfa
        The equivilent is to use a stringstream which work much like cout/cin or any other file stream something like

        Code:
        #include <sstream>
        #include <iomanip>
        
        ...
        
        ostringstream ss;
        
        ss << loc << "rect_W_" << fixed << setprecision(1) << width << "_H_"  << fixed << setprecision(1) << height << ".txt";
        
        current = ss.str();
        On a related note,

        Suppose I want to open a binary file for import / export using

        Code:
        int file;
        errno_t err=_sopen_s(&file,overlapfile,_O_BINARY,_SH_DENYNO,_S_IREAD);
        
        if(file==-1){
            perror("Failed to open file");
        }
        else{
            //Import data
        }
        The parameter overlapfile is of type
        Code:
        const char *
        Is there a way to convert a string to this type after overlapfile has been declared as

        Code:
        char overlapfile[500];
        or
        Code:
        char *overlapfile;
        I have tried various approaches, the latest being

        Code:
        string f1;
        //Assign something to f1
        
        char *overlapfile=new(char[static_cast<int>(f1.size())]);
        
        for(int i=0;i<static_cast<int>(f1.size());i++) overlapfile[i]=f1[i];
        this works but you get some junk at the end of overlapfile.

        Comment

        • emaghero
          New Member
          • Oct 2006
          • 85

          #5
          Originally posted by emaghero
          On a related note,

          Suppose I want to open a binary file for import / export using

          Code:
          int file;
          errno_t err=_sopen_s(&file,overlapfile,_O_BINARY,_SH_DENYNO,_S_IREAD);
          
          if(file==-1){
              perror("Failed to open file");
          }
          else{
              //Import data
          }
          The parameter overlapfile is of type
          Code:
          const char *
          Is there a way to convert a string to this type after overlapfile has been declared as

          Code:
          char overlapfile[500];
          or
          Code:
          char *overlapfile;
          I have tried various approaches, the latest being

          Code:
          string f1;
          //Assign something to f1
          
          char *overlapfile=new(char[static_cast<int>(f1.size())]);
          
          for(int i=0;i<static_cast<int>(f1.size());i++) overlapfile[i]=f1[i];
          this works but you get some junk at the end of overlapfile.
          Just found a really easy way to get this done
          Code:
          char *overlapfile;
          
          overlapfile=(char *)(f1.c_str());
          Thanks for all the help.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Good :D

            The thing to remember is that all this basic C stuff is required by C++ too so there is nearly always a way to do it through the C++ standard library and STL

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              This code is dangerous:

              Code:
              char *overlapfile; 
                
              overlapfile=(char *)(f1.c_str());
              The basic_string<ch ar>::c_str() function returns a const char* so you can't use that pointer to hack into the string.

              The user of overlapfile sees it as a char* and if that user tries to change the string pointed at by overlapfile a run-time crash will occur.

              overlapfile should be defined as a const char* and there should be no cast.

              Comment

              Working...