可视化的字符串断言错误

visual strcpy assertion error c++

本文关键字:错误 断言 字符串 可视化      更新时间:2023-10-16

我是一名刚刚学习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数组分配内存更容易。