Question

I am updating an older program, there is code that looks like this...

sprintf (s, "%-6s %-32s\n", header.getLabel (theRecs[i].getTag(j)).c_str (), theRecs[i].getData(j).c_str ());
//insertString = Utils::Format(header.getLabel(theRecs[i].getTag(j)), 7, "left");
//insertString += Utils::Format(theRecs[i].getData(j), 0, "left") + "\n";
pos = page + page * ROWS * (COLS + 1) + (insertPoint + j) * (COLS + 1);
insertString = static_cast <string> (s);
insertString = insertString.substr (0, 39);
returnValue.replace(pos, 39, insertString);

When I run this code, it works fine, output is correctly formatted...

Name   Hotsy Totsy                     Name   Spears, Britney                 
HAdd1  1 Hotsy Totsy Road              HAdd1  70 Doublewide Lane              
HAdd2  Mandeville LA 70471             HAdd2  Mandeville LA 70471             
HNum   985.444.5555                    HNum   985.432.1234                    

Name   Vitter, David                                                          
HAdd1  P.O. Box 1234                                                          
HAdd2  Mandeville LA 70470                                                    
HNum   504.626.1010                                                           
WNum   504.626.2020                                                           

However when I uncomment the commented lines, and comment the sprintf line and the static_cast line. I get output like this...

Name   Hotsy Totsy                     Name   Spears, Britney HAdd1  1 Hotsy Totsy Road                  
            HAdd1  70 Doublewide Lanee LA 70471    
                                       HAdd2  Mandeville LA 70471                                                     HNum   985.432.1234                                              
                                                     Name   Vitter, David                                                           HAdd1  P.O. Box 1234                                              
            HAdd2  Mandeville LA 70470                           
                         HNum   504.626.1010              
                                             WNum   504.626.2020                                                                         

I have tested to make sure the strings are identical when it gets to the substr, however they are getting different outputs. It is weird, the code works fine when it starts with a cstring and is casted to a string, but it doesn't work when it is a string all along. What is going on here?

Here is the Utils::Format...

/****************************************************************
 * String-ify a 'string', with width formatting and with
 * specified justification.
 *
 * Parameters:
 *   value - the 'string' variable to be converted and formatted.
 *   width - the width of the output field.
 *   justify - desired justification, either 'right' or 'left'
 * Return: the string-ified version of 'value'
**/
string Utils::Format(const string value, const int width,
                     const string justify)
{
  Utils::oss.str("");
  if("left" == justify)
  {
    Utils::oss.setf(ios::left, ios::adjustfield);
    Utils::oss << setw(width) << value;
  }
  else if("right" == justify)
  {
    Utils::oss.setf(ios::right, ios::adjustfield);
    Utils::oss << setw(width) << value;
  }
  else
  {
    Utils::oss << setw(width) << value;
  }
  return oss.str();
} // string Utils::Format(string value, LONG width, string justify)
Was it helpful?

Solution

Your sprintf has a newline on the end (\n), but your other formatting code doesn't, so you're not starting a fresh line each time.

Update: You're also providing a width of 32 chars for the second field in the sprintf, but 0 in the Utils::Format call.

Update2: If the carriage return makes any difference, then it's definitely a length problem with the string coming off your formatting routines, because in the sprintf version, the \n would have actually been removed by the substr anyway. Set the length in the second Utils::Format call properly...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top