出于练习目的,我尝试创建一个控制台应用程序,该应用程序将存储员工的基本信息并将其存储在名为employee-info.txt的文本文件中。我能够在文本文件中写入数据,但是每次程序写入新数据时,都会出现C6262警告。可以在Employee.cpp下的名为employeeDataChecker()和employeeWriteData()的函数中看到该警告,我将在下面指出这些代码。

对于employeeDataChecker(),警告指出该函数使用堆栈的'22800'字节:超出/分析:堆栈大小'16384'。考虑将一些数据移至堆。对于employeeWriteData,它具有相同的警告,但是堆栈'23264'的字节较大。我什至尝试将指针添加到集成在代码中的数组中,但是这会使我的代码更加混乱。

请提供有关如何进行管理或为实现目标提供更好解决方案的建议和指导。您还可以查明我的错误做法,以便我可以从中学习以改进我的代码。我在下面提供的代码很长,但是我仍然每天都在努力以减少代码。

当前,这是存储在employee-info.txt中的数据:

       ID           Firstname            Lastname                 Sales
        1              Dwyane             Anthony             250000.00
        2              Joseph            Cardinal             450000.00
        4               Bruno                Mars             250000.00


这是Employee.h的代码:

#pragma once
#include<string>
#include<iostream>

class Employee
{
public:
struct EmployeeRecord {
    static const int recordSize = 100;
    static const int fieldSize = 4;
    std::string record[recordSize][fieldSize];
};

public:
Employee();
~Employee();
void employeeDataChecker();
void employeeWriteData(int recordCount, std::string recordCopy[EmployeeRecord::recordSize][EmployeeRecord::fieldSize]);
void employeeDisplayData();
EmployeeRecord& employeeReturnRecordArray();

private:
EmployeeRecord emp_record;

};


这是Employee.cpp中的代码:

void Employee::employeeDataChecker() {
//Check if there are data in the employee-info.txt
EmployeeRecord emp;
std::ifstream inFile, inFile2;
int recordCount = 0;
inFile.open("C:\\Users\\RJ\\Desktop\\employee-info.txt"); // use to get the number of values stored in record array
inFile2.open("C:\\Users\\RJ\\Desktop\\employee-info.txt"); // use to get all contents in record array

for (int index = 0; index < emp.recordSize; index++) {
    for (int index2 = 0; index2 < emp.fieldSize; index2++) {
        while (inFile >> emp.record[index][index2]) {
            recordCount++;
        }
    }
}

for (int index = 0; index < emp.recordSize; index++) {
    for (int index2 = 0; index2 < emp.fieldSize; index2++) {
        inFile2 >> emp.record[index][index2];
    }
}
//used as a dummy array to hold the values in record array and pass as an argument
std::string recordCopy[emp.recordSize][emp.fieldSize];


    for (int index = 0; index < emp.recordSize; index++) {
        for (int index2 = 0; index2 < emp.fieldSize; index2++) {
            recordCopy[index][index2] = emp.record[index][index2];
        }
    }



inFile.close();
inFile2.close();

employeeWriteData(recordCount, recordCopy);
}//end of employeeDataChecker

void Employee::employeeWriteData(int recordCount, std::string recordCopy[EmployeeRecord::recordSize][EmployeeRecord::fieldSize]) {
Employee emp;
EmployeeRecord empRec;
int numEmployees;
std::string firstName, lastName;
std::string fullName = "";
double sales;

std::ofstream outFile;
outFile.open("C:\\Users\\RJ\\Desktop\\employee-info.txt", std::ofstream::app);

//pass all values from recordCopy to record array
for (int index = 0; index < empRec.recordSize; index++) {
    for (int index2 = 0; index2 < empRec.fieldSize; index2++) {
        empRec.record[index][index2] = recordCopy[index][index2];
    }
}

std::cout << "------------------------------------------------" << std::endl;
std::cout << "Enter The Number of Employees to Add: ";
std::cin >> numEmployees;
std::cin.get();

if (recordCount == 0) {

    //If employee-info.txt is empty.
    outFile << std::fixed << std::showpoint << std::setprecision(2);
    outFile << std::setw(5) << "ID";
    outFile << std::setw(20) << "Firstname";
    outFile << std::setw(20) << "Lastname";
    outFile << std::setw(22) << "Sales" << std::endl;

    for (int index = 0; index < numEmployees; index++) {
        int empID = index;
        empID++;
        std::cout << "*****Employee ID No." << empID << "*****" << std::endl;
        std::cout << "Enter First Name: ";
        std::getline(std::cin, firstName);
        std::cout << "Enter Last Name: ";
        std::getline(std::cin, lastName);
        fullName = firstName + " " + lastName;

        std::cout << "Enter Total Sales: ";
        std::cin >> sales;

        std::string empIDConverted = std::to_string(empID);
        std::string salesConverted = std::to_string(sales);

        for (int index2 = 0; index2 < empRec.fieldSize; index2++) {
            if (index2 == 0) {
                empRec.record[index][index2] = empIDConverted;
            }
            else if (index2 == 1) {
                empRec.record[index][index2] = firstName;
            }
            else if (index2 == 2) {
                empRec.record[index][index2] = lastName;
            }
            else if (index2 == 3) {
                empRec.record[index][index2] = salesConverted;
            }
        }

        outFile << std::fixed << std::showpoint << std::setprecision(2);
        int numSetW;
        for (int index2 = 0; index2 < empRec.fieldSize; index2++) {

            if (index2 == 0) {
                numSetW = 5;
            }
            else if(index2 == 3){
                numSetW = 22;
            }
            else {
                numSetW = 20;
            }

            if (index2 == (empRec.fieldSize - 1)) {

                std::string getSales = empRec.record[index][index2];
                double salesPreviousType;
                std::istringstream iss(getSales);
                iss >> salesPreviousType;

                outFile << std::setw(numSetW) << salesPreviousType << std::endl;
            }
            else {
                outFile << std::setw(numSetW) << empRec.record[index][index2];
            }
        }
        std::cin.get();
    }

}
else if (recordCount >= empRec.fieldSize) {
    //If employee-info.txt already has an existing data. It will write new data at the end of record array.

    int rows = recordCount / empRec.fieldSize;
    int increasedFieldSize = empRec.fieldSize * 2;
    int preIndex = rows;

    for (int index = rows; index < (numEmployees + preIndex); index++) {
        int empID = index;
        empID++;
        std::cout << "*****Employee ID No." << empID << "*****" << std::endl;
        std::cout << "Enter First Name: ";
        std::getline(std::cin, firstName);
        std::cout << "Enter Last Name: ";
        std::getline(std::cin, lastName);
        fullName = firstName + " " + lastName;

        std::cout << "Enter Total Sales: ";
        std::cin >> sales;

        std::string empIDConverted = std::to_string(empID);
        std::string salesConverted = std::to_string(sales);

        for (int index2 = 0; index2 < empRec.fieldSize; index2++) {
            if (index2 == 0) {
                empRec.record[index][index2] = empIDConverted;
            }
            else if (index2 == 1) {
                empRec.record[index][index2] = firstName;
            }
            else if (index2 == 2) {
                empRec.record[index][index2] = lastName;
            }
            else if (index2 == 3) {
                empRec.record[index][index2] = salesConverted;
            }
        }

        outFile << std::fixed << std::showpoint << std::setprecision(2);
        int numSetW;
        for (int index2 = 0; index2 < empRec.fieldSize; index2++) {

            if (index2 == 0) {
                numSetW = 5;
            }
            else if (index2 == 3) {
                numSetW = 22;
            }
            else {
                numSetW = 20;
            }

            if (index2 == (empRec.fieldSize - 1)) {

                std::string getSales = empRec.record[index][index2];
                double salesPreviousType;
                std::istringstream iss(getSales);
                iss >> salesPreviousType;

                outFile << std::setw(numSetW) << salesPreviousType << std::endl;
            }
            else {
                outFile << std::setw(numSetW) << empRec.record[index][index2];
            }

        }
        std::cin.get();
    }

}
else {
    std::cout << "Number problem!";
}


outFile.close();
  }//end of employeeWriteData

最佳答案

由于静态100x4 EmployeeRecord数组,您的std::string结构对于在堆栈上分配来说是很大的。动态分配数组或EmployeeRecord实例以消除该警告。

std::string recordCopy[emp.recordSize][emp.fieldSize];也是如此。

关于c++ - 在文件中使用ofstream时C6262堆栈超出了c++中的警告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55993546/

10-09 16:15
查看更多