C++ C++;缓冲区太小错误
Visual Studio在运行我的代码时会间歇性地引发异常。我断断续续地说,因为我已经能够成功地运行代码而没有错误。该错误是在我创建函数“print_Days”后引发的 引发的异常是: 调试断言失败 文件:minkernel\crts\ucrt\corecrt\u internal\u string\u templates.h 电话号码:81 表达式:(L“缓冲区太小”&&0) 该函数从一个.txt文件中读取,该文件列出了一周中的7天(周一到周日),然后按字母顺序在2D c字符串数组中对这些天进行排序(遗憾的是,教授让我们使用c字符串而不是字符串) 以下是我的全部代码:C++ C++;缓冲区太小错误,c++,c-strings,strcpy,C++,C Strings,Strcpy,Visual Studio在运行我的代码时会间歇性地引发异常。我断断续续地说,因为我已经能够成功地运行代码而没有错误。该错误是在我创建函数“print_Days”后引发的 引发的异常是: 调试断言失败 文件:minkernel\crts\ucrt\corecrt\u internal\u string\u templates.h 电话号码:81 表达式:(L“缓冲区太小”&&0) 该函数从一个.txt文件中读取,该文件列出了一周中的7天(周一到周日),然后按字母顺序在2D c字符串数组中对这些天
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
//Constants for 2D array
const int NUM_OF_ROWS = 7; //Seven days listed in the file
const int NUM_OF_COLS = 10; //Longest word is 9 chars long, plus \0
void get_Days(ifstream& file, char days[][NUM_OF_COLS], int rows);
void sort_Days(char days[][NUM_OF_COLS], int rows);
void print_Days(const char days[][NUM_OF_COLS], const int rows);
void get_Days(ifstream& file, char days[][NUM_OF_COLS], int rows) {
//Read from text file and return day
for (int i = 0; i < rows; ++i)
{
file >> days[i];
}
}
void sort_Days(char days[][NUM_OF_COLS], int rows) {
//Sort the array alphabetically
char temp[NUM_OF_COLS];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if (strcmp(days[j - 1], days[j]) > 0)
{
strcpy_s(temp, days[j - 1]);
strcpy_s(days[j - 1], days[j]);
strcpy_s(days[j], temp);
}
}
}
}
void print_Days(const char days[][NUM_OF_COLS], const int rows) {
//Print the sorted array to the console
for (int i = 0; i < NUM_OF_ROWS; ++i)
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < NUM_OF_COLS; j++)
{
cout << days[i][j] << endl;
}
}
}
int main() {
//This program reads from a file (days.txt), sorts the days
// alphabetically, and then prints the result to the console.
ifstream infile("days.txt");
char days[NUM_OF_ROWS][NUM_OF_COLS];
if (!infile)
{
cout << "File (days.txt) does not exist." << endl;
return 1;
}
get_Days(infile, days, NUM_OF_ROWS);
infile.close();
sort_Days(days, NUM_OF_ROWS);
print_Days(days, NUM_OF_ROWS);
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//二维数组的常数
const int NUM_OF_行=7//文件中列出的七天
const int NUM OF_COLS=10//最长单词的长度为9个字符,加上\0
void get_Days(ifstream&file,char Days[][NUM_OF_COLS],int行);
无效排序天数(字符天数[][NUM\u OF\u COLS],int行);
无效打印天数(常量字符天数[][NUM\u OF\u COLS],常量整数行);
void get_Days(ifstream&file,char Days[][NUM_OF_COLS],int行){
//从文本文件读取并返回日期
对于(int i=0;i>天[i];
}
}
无效排序天数(字符天数[][NUM\u OF\u COLS],整数行){
//按字母顺序对数组排序
字符温度[字符数];
对于(int i=0;i0)
{
strcpy_s(温度,天数[j-1]);
strcpy_s(天[j-1],天[j]);
strcpy_s(天[j],温度);
}
}
}
}
无效打印天数(常量字符天数[][NUM\u OF\u COLS],常量整数行){
//将排序后的数组打印到控制台
对于(int i=0;i cout代码有几处错误:
排序天数
sort\u Days
算法正在抛出错误,因为当嵌套for循环以j=0开始时,您试图索引Days[j-1]
。因此,您的初始索引超出了范围
此外,您似乎试图对c样式字符串执行冒泡排序,但冒泡排序实现不正确。请参考如何实现简单的冒泡排序。提示:循环条件的、strcmp
和strcpy\u
索引需要一些调整
打印天数
您的print_Days
函数不正确。以下是打印每个c样式字符串的版本,而不是打印字符串中的每个char
:
void print_Days(const char days[][NUM_OF_COLS], const int rows)
{
for (int j = 0; j < rows; j++)
{
cout << days[j] << endl;
}
}
void print_Days(const char Days[][NUM_OF_COLS],const int rows)
{
对于(int j=0;j 是否有两个for
循环使用i
作为循环变量。在调试器中查找调用堆栈,看看代码中的哪一行是问题所在。@1201程序调用堆栈说问题出在第35行,即“strcpy_s(temp,days[j-1]);”Visual Studio有一个名为“调用堆栈”的窗口,您可以追溯您的错误调用堆栈将“string.h”中的第123行列为触发断点。这一行的代码是:_DEFINE_CPP_重载_SECURE_FUNC_0_1(errno_t,strcpy_s,_Post_z char,Destination,_In_z char const*,_Source)然后调用堆栈列出程序中的第39行,这是sort函数中的第一个strcpy_实例。