?
Solved

Building and appending string, my code is LONG

Posted on 2012-03-21
1
Medium Priority
?
282 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

840 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