Solved

Building and appending string, my code is LONG

Posted on 2012-03-21
1
271 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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now