文件输出为十六进制?

时间:2015-10-23 03:01:09

标签: c++ file-io vector

我正在制作一个程序,用我的名字,日期,文件名以及它的作用描述来发表评论。它还会查找任何intvoid函数,这些函数会添加每个函数的功能描述。 我已经完成了这些步骤!

我创建了一个vector<string>,它将整个文件读入自身。然后,它会搜索向量中的任何voidint函数。如果它找到int,则调用string findParameters来查找int的参数并在描述中列出它们。

实施例

/*THE DESCRIPTION THAT THE PROGRAM MADE IS BELLOW*/

/*Function: int nDigits
Description: 
     Description of the function
Parameters:
     long long number: Description
 Returns:
     returnVal: Description
*/

/*THE FUNCTION THE PROGRAM WAS LOOKING FOR IS BELLOW*/
int nDigits(long long number){


    int counter;
    for (counter = 0; number > 0; counter++){
        number = number / 10;

    }

    return counter;

}

问题

当程序没有超过160行时,程序运行正常,但当我输出时,任何东西都会将所有内容转换为十六进制,当我尝试在记事本中打开它时,除了最后几个函数之外,我的大部分代码都被删除了。

文件输出示例

0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0020 2020 2020 200d 0a20 2020 2020
2020 2063 6f75 6e74 6572 2b2b 3b0d 0a20
2020 2020 2020 2066 616b 655f 6e75 6d62
6572 202f 3d20 3130 3b0d 0a20 2020 207d
0d0a 2020 2020 0d0a 2020 2020 0d0a 2020

当然,大约有400行。

我不知道发生了什么,所以任何帮助都会非常感激!我相信它可能会限制我在载体中放入多少行?

我的代码

查找参数功能

string findParameters(string line, string outputFileName, vector<string> &list_parameters){


    ofstream outFile;
    outFile.open(outputFileName, ios::out);
    int comma;
    string something;
    int length;
    int parstart;
    int parend;
    int last;
    if (outFile.is_open()) {

        if (line.find("(") != string::npos) {
            last = parstart = line.find("(",0) + 1;

            if (line.find(")", parstart + 1) != string::npos) {
                 parend = line.find(")", parstart + 1) + 1;



                    do {
                        comma = line.find(",", last+1);

                        if (comma == string::npos) {
                            length = parend - last;
                            something = line.substr(last, length-1);

                            list_parameters.push_back("\n     " + something);
                        }
                        else {
                            length = comma - last;
                            something = line.substr(last, length);

                            list_parameters.push_back("\n     " + something);
                            last = comma+1;
                        }
                    } while (comma != string::npos);




            }
        }
        outFile.close();
    }
    else {
        cout << "\nfile find parameters did not open";
    }

    return "";

}

输出标题功能

    //Starting Header
        //File Name
    outFile << "/* \nFilename: " << outputFileName;

        //Date
    char date[9];
    _strdate_s(date);
    outFile << "\n\nDate: " << date << "\n";
        //Description
    outFile << "\nProgrammer: Dyrenex\n\nDescription:\n     //Description of what the code in the file is meant to do and how it does what it is meant to do" << "\n";
    outFile << "\n\n */ \n\n";



    size_t found;
    //Writing the Rest of Input File
    while (isDone != existingFile.size()){

        //Finding void functions
        line = existingFile[isDone];
        if (line.find("void") != string::npos){
            //cout << "\nFound a void!" << "\n";
            outFile << "\n/*Function: \nDescription:\n     Description of the function\nParameters:" <<
                "\n     Parameters here\n Returns:\n     returnVal:*/";

        }
        else if (line.find("int",0) != string::npos) {
            int intstart = line.find("int");

            if (line.find("(", intstart + 1) != string::npos) {
                int parstart = line.find("(", intstart + 1);

                if (line.find(")", parstart + 1) != string::npos) {
                    int parend = line.find(")", parstart + 1);
                    vector<string> list_parameters;

                    outFile << "\n/*Function: " << line.substr(intstart, parstart)
                        << "\nDescription: \n     Description of the function\nParameters:";

                    findParameters(line, outputFileName,list_parameters);

                    int counter = 0;

                    while (counter != list_parameters.size()) {
                        outFile << list_parameters[counter] << ": Description";
                        counter++;
                    }


                        outFile << "\n Returns:\n     returnVal: Description\n*/";

                }
            }
        }

        //outputting file line
        outFile << "\n" << existingFile[isDone];

        isDone++;

    }


    outFile.close();

阅读现有文件

    ifstream inFile;
    inFile.open(inputFileName, ios::in);
    if (inFile.is_open()){

        while (!inFile.eof()){

            getline(inFile, line);
            existingFile.push_back(line);
        }

        inFile.close();
     }

*这不是我的功课!我这样做是为了自动将我的名字放在我的作业上,所以我不必为每项作业做到这一点! *

提前非常感谢你!

我尝试将标题放在我的问题中,但是如果你不喜欢我的问题的格式 这里是我所有代码的链接 http://pastebin.com/4P5hFzQm

Bellow是我尝试操作的.cpp(输入文件)

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;


int nDigits(long long number){


    int counter;
    for (counter = 0; number > 0; counter++){
        number = number / 10;

    }

    return counter;

}

int reverse(long long number){

    int reverse = 0;
    while (number > 0){

        reverse = reverse * 10;
        reverse = reverse + (number % 10);
        number = number / 10;

    }

    return reverse;
}

bool isPalindrome(long long number){

    if (number == reverse(number)){
        return true;
    }
    else{
        return false;
    }
}

bool repeatsDigit (long long number){


    int temp[1024];
    long long fake_number = number;
    int counter = 0;
    int limit = nDigits(number);


    while (fake_number > 0){
        temp[counter] = fake_number % 10;

        counter++;
        fake_number /= 10;
    }


    for (counter = 0; counter < limit; counter++){

        for (int counter2 = 0; counter2 < limit; counter2++){

            if (temp[counter] == temp[counter2] && counter != counter2){
                return true;
            }

        }

    }

    return false;
}

void digitCount( long long n, int& eCount, int& oCount){

    int temp[1024];
    long long fake_number = n;
    int counter = 0;
    int limit = nDigits(n);


    while (fake_number > 0){
        temp[counter] = fake_number % 10;

        counter++;
        fake_number /= 10;
    }


    for (counter = 0; counter < limit; counter++){

        if (temp[counter] % 2 == 0){
            eCount++;
        }
        else{
            oCount++;
        }

    }


}

int largest(long long n){

    int temp[1024];
    long long fake_number = n;
    int counter = 0;
    int limit = nDigits(n);
    int largestnum = 0;

    while (fake_number > 0){
        temp[counter] = fake_number % 10;

        counter++;
        fake_number /= 10;
    }


    for (counter = 0; counter < limit; counter++){

        if (largestnum < temp[counter]){
            largestnum = temp[counter];
        }

    }
    return largestnum;
}
int smallest(long long n){

    int temp[1024];
    long long fake_number = n;
    int counter = 0;
    int limit = nDigits(n);
    int smallestnum = 10;

    while (fake_number > 0){
        temp[counter] = fake_number % 10;

        counter++;
        fake_number /= 10;
    }


    for (counter = 0; counter < limit; counter++){

        if (smallestnum > temp[counter]){
            smallestnum = temp[counter];
        }

    }

    return smallestnum;
}
double average(long long n){

    int temp[1024];
    long long fake_number = n;
    int counter = 0;
        int limit = nDigits(n);
        double averagenum = 0;

    while (fake_number > 0){
        temp[counter] = fake_number % 10;

        counter++;
        fake_number /= 10;
    }


    for (counter = 0; counter < limit; counter++){

        averagenum += temp[counter];

    }

    return averagenum/nDigits(n);
}


int main(){

    long long something = 12345;
    int evenCount = 0;
    int oddCount = 0;



    /*real output*/
    ifstream file;
    file.open("integers.dat",ios::in);
    if (file.is_open()){
        file >> something;
        cout << "The number of digits in " << something << " is " << nDigits(something) << "\n";

        file >> something;
        cout << "The reverse of : " << something << " is " << reverse(something) << "\n";

        file >> something;
        if (isPalindrome(something) == 1){
            cout << "There is a Palindrome in number: " << something << "\n";
        }
        else{
            cout << something << " is NOT a Palindrome" << "\n";

        }

        file >> something;
        if (repeatsDigit(something) == 1){
            cout << "There are repeat digits in " << something << "\n";
        }
        else{
            cout << "There are NOT repeat digits: in " << something << "\n";

        }
        file >> something;
        digitCount(something, evenCount, oddCount);
        cout << "There are " << evenCount <<" Even and, " << oddCount <<" Odd in the number " << something << "\n";

        file >> something;
        cout << "The largest Number in " << something << " is " << largest(something) << "\n";

        file >> something;
        cout << "The smallest Number in " << something << " is " << smallest(something) << "\n";

        file >> something;
        cout << "The average Number in " << something << " is " << fixed << setprecision(1) << average(something) << "\n";

    }
    else {
        cout << "File did not open";
    }

    return 0;
}

此外,输出文件是程序命名的新文件,因此无论您想要什么名称都可以命名...它并不重要

再次感谢您,如果您有任何疑问,请询问!我尽可能多地学习,这个网站也在帮助我做到这一点!

1 个答案:

答案 0 :(得分:0)

我无法完成您的代码,但它看起来不像它对矢量做了很多,它只是存储它。如果我没有弄错,你可以像下面的代码一样缩短你的程序。

但是,要正确识别函数,您需要将行存储在向量中以进行检查。

int main() 
{
    const char* note = "Etcetera Etcetera";

    ifstream in("in.cpp");
    ofstream out("out.cpp");

    string line;
    while (getline(in, line))
    {
        if (
                (
                    line.find('(') != string::npos && 
                    line.find(')') != string::npos
                ) 
                &&
                (
                    line.find("double") == 0 ||
                    line.find("int") == 0 ||
                    line.find("bool") == 0 ||
                    line.find("void") == 0
                )
            )
        {
            out << note << endl;
        }

        out << line << endl;
    }

    system("pause");
    return 0;
}