Solved

Building and appending string, my code is LONG

Posted on 2012-03-21
1
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

724 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