Solved

Building and appending string, my code is LONG

Posted on 2012-03-21
1
277 Views
Last Modified: 2012-08-13
I am simply trying to build a string that displays the first 4 numbers of Hard Drive serial number, a hyphen,  and append the first four letters of today's day name.

Example:

2190-Wedn


The code below WORKS but I think I am taking the long way. Can anyone help me refactor this code a bit?

Thanks

 //-----------------------------------------------------------------
                       // Check Number Three: First four of HD Serial Number


                        LPCTSTR szHD = "C:\\"; // A pointer to a constant null-terminated string
                        UCHAR szFileSys[255],szVolNameBuff[255];
                        DWORD dwSerial,dwMFL,dwSysFlags;
                        BOOL bSuccess;

                        bSuccess = GetVolumeInformation(szHD,(LPTSTR)szVolNameBuff,
                                         255,&dwSerial, &dwMFL,&dwSysFlags,
                                         (LPTSTR)szFileSys,255);

                        // Convert to Character, Move to szHdSerial
                        wsprintf(szHdSerial,"%lu", dwSerial);


                        // Loop through, get first 4 chars
                        for(int ii=0 ; ii<4 ; ii++)
                        {
                            finalCombination[ii] = szHdSerial[ii];
                        }

                        std::string fC;
                        fC = finalCombination;
                        std::string hyphen = "-";
                        fC.append(hyphen);
                        strcpy (finalCombination, fC.c_str());

                        //MessageBox(NULL,finalCombination,"a",NULL);

                       //-----------------------------------------------------------------
                       // Check Number Four: Build String to use Cypher against
                        time_t rawtime;
                          struct tm * timeinfo;
                          char buffer [80];

                          time ( &rawtime );
                          timeinfo = localtime ( &rawtime );
                          strftime (buffer,80,"%A",timeinfo);
                          // buffer now contains "Wednesday"
                          for(int ii=0 ; ii<4 ; ii++)
                          {
                            finalCombination[ii+5] = buffer[ii];
                          }
                            fC = finalCombination;


                          strcpy (finalCombination, fC.c_str());

                            //MessageBox(NULL,finalCombination,"a",NULL);
                            // Now 2190-Wedn

Open in new window

0
Comment
Question by:edvinson
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 37748836
Well, there are a few points that could shorten this - firs, I'd completely switch to 'std::string' since you are using it anyway, e.g.

#include <string>
#include <sstream>
using namespace std;

 //-----------------------------------------------------------------
                       // Check Number Three: First four of HD Serial Number


                        LPCTSTR szHD = "C:\\"; // A pointer to a constant null-terminated string
                        UCHAR szFileSys[255],szVolNameBuff[255];
                        DWORD dwSerial,dwMFL,dwSysFlags;
                        BOOL bSuccess;

                        bSuccess = GetVolumeInformation(szHD,(LPTSTR)szVolNameBuff,
                                         255,&dwSerial, &dwMFL,&dwSysFlags,
                                         (LPTSTR)szFileSys,255);

                        // Convert to Character, only use the 1st four digits
                        stringstream ss;

                        ss << dwSerial;

                        string finalCombination = ss.str().substr(0,4);

                        finalCombination += "-";

                       // Check Number Four: Build String to use Cypher against
                        time_t rawtime;
                          struct tm * timeinfo;
                          char buffer [80];

                          time ( &rawtime );
                          timeinfo = localtime ( &rawtime );
                          strftime (buffer,80,"%A",timeinfo);

                          string wday = buffer;

                         finalCombination += wday.substr(0,5);

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question