0

我正在使用该程序从导出的注册表文件中提取某些信息。我是使用 Visual Studio 的新手,它在 2012 年编译并运行良好,但在我意识到我需要 2010 年才能在 Windows XP 上运行应用程序之前我做到了。当我在 2010 年运行代码时,它给了我一个运行时错误,说“字符串下标超出范围”。任何有关此错误的方向将不胜感激。

    // SoftwareList.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <stdio.h>
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;

    class Node
    {
    public:
        string name;
        string publisher;
        string version;
        string installDate;
        int position;
    };

    void quickSort(Node arr[], int left, int right)
    {
        int i = left, j = right;
        Node temp;
        Node pivot = arr[(left + right) / 2];

        /* partition */
        while (i <= j) {
            while (arr[i].name < pivot.name)
                i++;
            while (arr[j].name > pivot.name)
                j--;
            if (i <= j) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                i++;
                j--;
            }
        }
        /* recursion */
        if (left < j)
            quickSort(arr, left, j);
        if (i < right)
            quickSort(arr, i, right);
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
        string line;
        string field;
        string filter;
        int index = 0;
        int arraySize = 10000;
        Node *programs;
            programs = new Node[arraySize];

        //Export the needed registry keys
        printf("Exporting Registry List\n");
        system("reg export        HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall         reg_out_unicode.txt");
system("cmd /a /c type reg_out_unicode.txt>reg_out_ansi.txt");
system("del reg_out_unicode.txt");

//Clean up the registry keys to only show the info needed
ifstream infile;
ofstream outfile;
infile.open("reg_out_ansi.txt");
outfile.open("SoftwareList.txt");
Node currentProgram;

//Initialize the new Node
currentProgram.name = "";
currentProgram.publisher = "";
currentProgram.version = "";

while (getline(infile,line))
{

    if (line[0] == '[' )
    {
        //Add the last filled node
        if (!currentProgram.name.empty())
        {
            currentProgram.position = index;
            programs[index] = currentProgram;
            index++;
        }

        //Initialize the empty Node
        currentProgram.name = "";
        currentProgram.publisher = "";
        currentProgram.version = "";
    }
    //Find the first " and pull the string from after that
    //const char *temp = line.c_str();
    char *temp = new char[line.size() + 1];
    strcpy(temp,line.c_str());

    if (*temp == '"')
    {
        *temp++;
        while (*temp != '"') //Stops at the next "
        {
            field.push_back(*temp++); //Add what is in between the "" into the field string for checking
        }
    }

    if (strcmp(field.c_str(),"DisplayName") == 0) // Grab the DisplayName of a program
    {
        field.push_back(*temp++);
        field.push_back(*temp++);
        field.clear();
        while (1)
        {
            if (*temp == '"')
            {
                *temp++;
                while (*temp != '"') //Stops at the next "
                {
                    field.push_back(*temp); //Add what is in between the "" into the field string for checking
                    *temp++;                        
                }
                break;
            } else {
                *temp++;
            }
        }
        currentProgram.name = field;
    }

    if (strcmp(field.c_str(),"DisplayVersion") == 0) // Grab the DisplayVersion of a program
    {
        field.push_back(*temp++);
        field.push_back(*temp++);
        field.clear();
        while (1)
        {
            if (*temp == '"')
            {
                *temp++;
                while (*temp != '"') //Stops at the next "
                {
                    field.push_back(*temp); //Add what is in between the "" into the field string for checking
                    *temp++;

                }
                break;
            } else {
                *temp++;
            }
        }
        currentProgram.version = field;
    }

    if (strcmp(field.c_str(),"Publisher") == 0) // Grab the Publisher of a program
    {
        field.push_back(*temp++);
        field.push_back(*temp++);
        field.clear();
        while (1)
        {
            if (*temp == '"')
            {
                *temp++;
                while (*temp != '"') //Stops at the next "
                {
                    field.push_back(*temp); //Add what is in between the "" into the field string for checking
                    *temp++;                        
                }
                break;
            } else {
                *temp++;
            }
        }
        currentProgram.publisher = field;
    }

    if (strcmp(field.c_str(),"InstallDate") == 0) // Grab the Publisher of a program
    {
        field.push_back(*temp++);
        field.push_back(*temp++);
        field.clear();
        while (1)
        {
            if (*temp == '"')
            {
                *temp++;
                while (*temp != '"') //Stops at the next "
                {
                    field.push_back(*temp); //Add what is in between the "" into the field string for checking
                    *temp++;                        
                }
                break;
            } else {
                *temp++;
            }
        }
        currentProgram.installDate = field;
    }

    field.clear();
}

cout << "Registry Successfully Parsed" << endl;

//Sort the output
quickSort(programs,0,arraySize - 1);
cout << "Sorting Successful" << endl;

cout << '\n' << "Choose Output Filtering Method" << endl;
cout << "-------------------------" << endl;
cout << "  " << "[1] All Software" << endl;
cout << "  " << "[2] All But Microsoft" << endl;
cout << "  " << "[3] Filter By Software Title" << endl;
cout << "  " << "[4] Filter By Software Publisher" << endl;
cout << "  " << "[5] Filter By Install Date" << endl;
cout << "Enter Choice: ";

//Write File Header
outfile << "Title" << '\t' << "Version" << '\t' << "Publisher" << '\t' << "Install Date" << endl;

while (1)
{
    cin >> filter;
    if (!strcmp(filter.c_str(),"1"))
    {
        //Print all elements to outfile
        cout << "Writing Output File" << endl;
        for (int i = 0; i < arraySize; i++)
        {
            if (strcmp(programs[i].name.c_str(),"") != 0)
            {
                outfile << programs[i].name.c_str() << '\t' << programs[i].version.c_str() << '\t' << programs[i].publisher.c_str() << '\t' << programs[i].installDate.c_str() << endl;
            }
        }
        break;
    } else  if (!strcmp(filter.c_str(),"2")) {
        //Print selected elements to outfile
        cout << "Writing Output File" << endl;
        for (int i = 0; i < arraySize; i++)
        {
            if (strcmp(programs[i].name.c_str(),"") != 0)
            {
                if (programs[i].publisher.find("Microsoft") == string::npos)
                {
                    if (programs[i].name.find("Microsoft") == string::npos)
                    {
                        outfile << programs[i].name.c_str() << '\t' << programs[i].version.c_str() << '\t' << programs[i].publisher.c_str() << '\t' << programs[i].installDate.c_str() << endl;
                    }
                }
            }
        }
        break;
    }else if (!strcmp(filter.c_str(),"3")) {
        string title;
        cout << '\n' << "Enter Title (Case Sensitive): ";
        cin >> title;
        cout << endl;

        //Print selected elements to outfile
        cout << "Writing Output File" << endl;
        for (int i = 0; i < arraySize; i++)
        {
            if (strcmp(programs[i].name.c_str(),"") != 0)
            {
                if (programs[i].name.find(title) != string::npos)
                {
                    outfile << programs[i].name.c_str() << '\t' << programs[i].version.c_str() << '\t' << programs[i].publisher.c_str() << '\t' << programs[i].installDate.c_str() << endl;
                }
            }
        }
        break;
    } else  if (!strcmp(filter.c_str(),"4")) {

        string publisher;
        cout << '\n' << "Enter Publisher (Case Sensitive): ";
        cin >> publisher;
        cout << endl;

        //Print selected elements to outfile
        cout << "Writing Output File" << endl;
        for (int i = 0; i < arraySize; i++)
        {
            if (strcmp(programs[i].name.c_str(),"") != 0)
            {
                if (programs[i].publisher.find(publisher) != string::npos)
                {
                    outfile << programs[i].name.c_str() << '\t' << programs[i].version.c_str() << '\t' << programs[i].publisher.c_str() << '\t' << programs[i].installDate.c_str() << endl;
                }
            }
        }
        break;
    } else if (!strcmp(filter.c_str(),"5")) {

        string date;
        cout << '\n' << "Enter Date (YYYYMMDD): ";
        cin >> date;
        cout << endl;

        //Print selected elements to outfile
        cout << "Writing Output File" << endl;
        for (int i = 0; i < arraySize; i++)
        {
            if (strcmp(programs[i].name.c_str(),"") != 0)
            {
                if (programs[i].installDate.find(date) != string::npos)
                {
                    outfile << programs[i].name.c_str() << '\t' << programs[i].version.c_str() << '\t' << programs[i].publisher.c_str() << '\t' << programs[i].installDate.c_str() << endl;
                }
            }
        }
        break;
    } else {
        cout << "Invalid Input... Try Again" << endl;
    }
}

//Close and clean up files
infile.close();
outfile.close();
system("del reg_out_ansi.txt");
return 0;
    }
4

0 回答 0