在文件中使用 ofstream 时,C6262 堆栈在 C++ 中超出警告
C6262 stack exceeds warning in c++ when using ofstream in file
出于练习目的,我尝试创建一个控制台应用程序来存储员工的基本信息并将其存储在名为 employee-info.txt 的文本文件中。我能够在文本文件中写入数据,但每次程序写入新数据时,都会出现 C6262 警告。警告可以在Employee.cpp下调用employeeDataChecker()和employeeWriteData()的函数中看到,我将在下面的代码中指出。
对于 employeeDataChecker(),警告指出函数使用了“22800”字节的堆栈:exceeds/analyze:stacksize“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 std::string
数组,您的 EmployeeRecord
结构对于堆栈分配而言相当大。动态分配数组或 EmployeeRecord
实例以消除该警告。
std::string recordCopy[emp.recordSize][emp.fieldSize];
也是如此。
出于练习目的,我尝试创建一个控制台应用程序来存储员工的基本信息并将其存储在名为 employee-info.txt 的文本文件中。我能够在文本文件中写入数据,但每次程序写入新数据时,都会出现 C6262 警告。警告可以在Employee.cpp下调用employeeDataChecker()和employeeWriteData()的函数中看到,我将在下面的代码中指出。
对于 employeeDataChecker(),警告指出函数使用了“22800”字节的堆栈:exceeds/analyze:stacksize“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 std::string
数组,您的 EmployeeRecord
结构对于堆栈分配而言相当大。动态分配数组或 EmployeeRecord
实例以消除该警告。
std::string recordCopy[emp.recordSize][emp.fieldSize];
也是如此。