可视化的字符串断言错误
visual strcpy assertion error c++
我是一名刚刚学习c++的学生,所以我相信有更有效的方法来做到这一点;话虽如此,我还是希望你能帮我弄清楚为什么我的程序会崩溃。我已经把它缩小到一个stry函数,它会使所有东西崩溃并中断它,我已经注释掉并标记了它。显然,我在程序中多次使用具有类似参数的strcpy函数,所以我不明白为什么那个特定的函数会崩溃。我已经尝试了我能想到的一切,真的很感谢你的帮助。到目前为止,我有很多注释,所以它应该与正确的文本文件一起运行,名为"bookdb"我的文本文件中目前有这个
Active Learning Approach,Randal Albert,9780763757236,1,650,1,<br>
Technical Communications,John Lannon,9780321899972,2,724,0,
查看错误,您必须取消注释strcpy(bookArray[num_books]。author_name temp_authorName);
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
enum Genres {HORROR=1, SCIFI, COMEDY, DRAMA, ACTION};
struct Book
{
char title[100];
char author_name[50];
char isbn[14];
Genres genre;
int num_pages;
bool paperback;
};
//Function Declarations
unsigned short ReadBooks(Book * bookArray, unsigned short & num_books);
void DisplayBooks(Book * bookArray, unsigned short num_books);
void ResizeArrays(Book * bookArray, unsigned short num_books);
int main ()
{
unsigned short num_books = 0;
Book * bookArray = new Book();
num_books = ReadBooks(bookArray, num_books);
}//End Main
unsigned short ReadBooks(Book * bookArray, unsigned short & num_books)
{
ifstream readBooks("bookdb.txt");
char temp_title[100] = "0";
char temp_authorName[100] = "0";
char temp_isbn[14] = "0";
char temp_genre[50] = "0";
char temp_numPages[50] = "0";
char temp_paperback[50] = "0";
int genreNumber = 0,
numPages = 0,
paperback = 0;
if (readBooks.is_open() )
{
cout << "The file was successfully openedn" << endl;
readBooks.getline(temp_title, 100, ',');//Reads into temp cstring
strcpy(bookArray[num_books].title, temp_title); //copies to dynamic cstring
//cout << bookArray[num_books].title << endl; //displays part of structure to make sure it worked!!
readBooks.getline(temp_authorName, 100, ',');
strcpy(bookArray[num_books].author_name, temp_authorName);
//cout << bookArray[num_books].author_name << endl;
readBooks.getline(temp_isbn, 14, ',');
strcpy(bookArray[num_books].isbn, temp_isbn);
//cout << bookArray[num_books].isbn << endl;
readBooks.getline(temp_genre, 50, ',');//Get the genre as a char
genreNumber = atoi(temp_genre);//converts char to an int
bookArray[num_books].genre = static_cast <Genres> (genreNumber);//converts int to ENUM
//cout << bookArray[num_books].genre << endl;//Displays ENUM to make sure it worked!!
readBooks.getline(temp_numPages, 50, ',');
numPages = atoi(temp_numPages); //converts char to an int
bookArray[num_books].num_pages = numPages; //assigns int to structure
//cout << bookArray[num_books].num_pages << endl; //Displays part of structure to make sure to works!!
readBooks.getline(temp_paperback, 50, ',');
paperback = atoi(temp_paperback); //converts char to an int
bookArray[num_books].paperback = static_cast <bool> (paperback);
//cout << bookArray[num_books].paperback << endl;
num_books++;
//DisplayBooks(bookArray, num_books);
ResizeArrays(bookArray, num_books);
cout << "The number of books is: " << num_books << endl;
//while (!readBooks.eof() )
//{
readBooks.getline(temp_title, 100, ',');//Reads into temp cstring
strcpy(bookArray[num_books].title, temp_title); //copies to dynamic cstring
cout << bookArray[num_books].title << endl; //displays part of structure to make sure it worked!!
readBooks.getline(temp_authorName, 100, ',');
cout << temp_authorName << endl;
//strcpy(bookArray[num_books].author_name, "0");
///THIS BREAKS MY CODE////strcpy(bookArray[num_books].author_name, temp_authorName);
//cout << bookArray[num_books].author_name << endl;
readBooks.getline(temp_isbn, 14, ',');
//strcpy(bookArray[num_books].isbn, temp_isbn);
//cout << bookArray[num_books].isbn << endl;
readBooks.getline(temp_genre, 50, ',');//Get the genre as a char
//genreNumber = atoi(temp_genre);//converts char to an int
//bookArray[num_books].genre = static_cast <Genres> (genreNumber);//converts int to ENUM
//cout << bookArray[num_books].genre << endl;//Displays ENUM to make sure it worked!!
readBooks.getline(temp_numPages, 1000, ',');
//numPages = atoi(temp_numPages); //converts char to an int
//bookArray[num_books].num_pages = numPages; //assigns int to structure
//cout << bookArray[num_books].num_pages << endl; //Displays part of structure to make sure to works!!
readBooks.getline(temp_paperback, 50, ',');
//paperback = atoi(temp_paperback); //converts char to an int
//bookArray[num_books].paperback = static_cast <bool> (paperback);
//cout << bookArray[num_books].paperback << endl;*/
num_books++;
//ResizeArrays(bookArray, num_books);
//}//End while
readBooks.close();
}//End if
else
{
cout << "There was not an existing book file, so one will be created" << endl;
}//End else
return 0;
}//End ReadBooks
void DisplayBooks(Book * bookArray, unsigned short num_books)
{
for (unsigned short i = 0; i < num_books; i++)
{
cout << setw(30) << left << bookArray[i].title << left << setw(20) << bookArray[i].author_name << left << setw(15) << bookArray[i].isbn
<< left << setw(3) << bookArray[i].genre << left<< setw(6) << bookArray[i].num_pages << left << setw(4) << bookArray[i].paperback << endl;
}//End For
}//ENd Display Function
void ResizeArrays(Book * bookArray, unsigned short num_books)
{
Book * temp_bookArray = new Book[num_books + 1];
for (int i = 0; i < num_books; i++)
{
strcpy(temp_bookArray[i].title, bookArray[i].title);
//cout << temp_bookArray[i].title << endl; //For Debugging
strcpy(temp_bookArray[i].author_name, bookArray[i].author_name);
//cout << temp_bookArray[i].author_name << endl; //for Debugging
strcpy(temp_bookArray[i].isbn, bookArray[i].isbn);
//cout << temp_bookArray[i].isbn << endl;//for debugging
temp_bookArray[i].genre = bookArray[i].genre;
//cout << temp_bookArray[i].genre << endl;//for debugging
temp_bookArray[i].num_pages = bookArray[i].num_pages;
//cout << temp_bookArray[i].num_pages << endl;// for debugging
temp_bookArray[i].paperback = bookArray[i].paperback;
//cout << temp_bookArray[i].paperback << endl; //for debugging
}//End for
delete [] bookArray;
bookArray = temp_bookArray;
DisplayBooks(bookArray, num_books); //debugging to make sure bookArray is reassigned
}//End Resize Function
strlen获取所有没有' '的元素,所以如果你有STR =" 1234";STR长度为5,(1234 + ' )但返回4。字符串取所有5个元素+ ' '
ResizeArrays:
有问题- 为数组分配新的内存,但不将新数组返回给调用函数,以便它可以调整指针到新的数组位置。
- 调整大小可能需要大小,旧的大小,和新的大小,你只能复制这些元素的最小数量。
为了使它能正常工作,你可以这样做:
Book* ResizeArrays(Book * bookArray, unsigned short num_books)
{
....
return bookArray;
}
然后以这种方式使用函数
bookArray = ResizeArrays(bookArray, num_books);
然而,当您使用数组删除时,对于第一次分配,您也需要在main()中使用数组分配,
Book * bookArray = new Book[1];
即使它只分配一本书,它也应该作为一个数组来完成,因此它与ResizeArrays中使用的delete[]兼容。
我想这是一个学习练习。在c++的正常使用中,您不会使用赤裸裸的new和delete操作符。您可以简单地使用std::vector或类似的方法,或者创建一个管理内存的对象,分别在构造函数和析构函数中使用new和delete。
我看到的问题:
问题1
没有足够的空间容纳ISBN。输入文件中的ISBN长度为14个字符。要将它们保存在以空结尾的字符串中,您需要有一个可以容纳15个或更多字符的数组。
因此, 这一行 readBooks.getline(temp_isbn, 14, ',');
将在13个字符后停止读取,因为它必须使用第14个字符来存储' '
。
问题2
这行看起来不对。
readBooks.getline(temp_numPages, 1000, ',');
我怀疑这是一个打字错误,你的意思是
readBooks.getline(temp_numPages, 50, ',');
问题3
在ResizeArray
中存在内存处理问题。
void ResizeArrays(Book * bookArray, unsigned short num_books)
{
Book * temp_bookArray = new Book[num_books + 1];
// Here, you are deleting the memory that was allocated in `main`.
// bookArray in the calling function now points to memory that has been
// deleted here.
delete [] bookArray;
// Here, you are changing the value of bookArray but only
// locally in this function. This assignment doesn't
// change the value of bookArray in the calling function.
bookArray = temp_bookArray;
DisplayBooks(bookArray, num_books); //debugging to make sure bookArray is reassigned
}//End Resize Function
如果您将ResizeArray
更改为:
void ResizeArrays(Book*& bookArray, unsigned short num_books)
一切都会好起来的。
问题4
我看到您已经注释掉了ReadBooks
中的while
循环。如果你决定让这个循环恢复正常,你必须在每次循环中调整bookArray
的大小。
使用std::vector<Book*>
来保存图书比为Books
数组分配内存更容易。
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- 错误:断言失败 (src.type() == CV_8UC1) 在阈值中
- OpenCV 错误:断言失败 - 矩阵减去
- OpenCV 错误:断言在 cv::Mat 行 522 中失败
- C++ / CannyEdgeDetection.exe 已停止工作 代码块 /OpenCV 错误:断言失败
- ANN OPENCV错误断言失败了
- OpenCV 错误:断言失败(a_size.width == len)
- 在曲面中插入边-CGAL错误:断言冲突
- 错误 opencv 错误断言失败 (p [-1] <= 2) 是什么意思以及如何处理它?
- 在面中插入边缘 - CGAL 错误:断言冲突
- openCV 错误:断言失败 (scn == 3 || scn == 4) 在 cvtColor 函数中
- OpenCV 错误:断言失败.怎么办
- 错误:断言失败Allegro5 addons font text.c Line 77表达式字体
- OpenCV 错误:断言失败((img.depth() == CV_8U || img.depth() == CV_32
- OpenCV错误:断言在MixChannels(.)中失败
- OpenCV错误:断言失败,mat.cpp第537行
- roi的opencv错误断言
- OpenCV : 训练 SVM 错误 - 断言失败
- Opencv错误:断言失败
- 我正在尝试将灰度转换为更大的黑色图像.我得到这个错误OpenCV错误:断言失败