fstream-c++初学者求助 Access violation reading location

问题描述

c++初学者求助 Access violation reading location

函数如下

 #include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
 int main()
{
    int Month, Day;
    string Name, Email, Address;
    while (cin >> Month >> Day >> Name >> Email >> Address)
    {
        Person p(Month ,Day,Name,Email,Address), m;
        int size = sizeof(Person);
        string name = p.getName();
        int month = p.getMonth(), day = p.getDay();
        fstream out1("SortByName.dat", ios::out | ios::in | ios::binary);//按姓名排序
        out1.seekp(0, ios::end);
        while (out1.tellp())
        {
            out1.seekp(-size, ios::cur);
            out1.read((char*)&m, size);
            if (m.getName() <= name)
                break;
            out1.seekp(out1.tellp(), ios::beg);
            out1.write((const char*)&m, size);
            out1.seekp(-2 * size, ios::cur);

        }
        out1.write((const char*)&p, size);
        out1.close();
        fstream out2("SortByBirthDay.dat", ios::out | ios::in | ios::binary);//按生日排序
        out2.seekp(0, ios::end);
        while (out2.tellp())
        {
            out2.seekp(-size, ios::cur);
            out2.read((char*)&m, size);
            if ((m.getMonth() < month) || (m.getMonth() == month&&m.getDay() <= day))
                break;
            out2.seekp(out2.tellp(), ios::beg);
            out2.write((const char*)&m, size);
            out2.seekp(-2 * size, ios::cur);
        }
        out2.write((const char*)&p, size);
        out2.close();
    }
}

头文件Person.h

//Person.h
 #ifndef PERSON_H
#define PERSON_H

#include<iostream>
#include<string>
using namespace std;

class Person
{
public:
    Person(int, int, string, string, string);
    Person(){};
    int getMonth() const;
    int getDay() const;
    string getName() const;
    void setEmail(string);
    void setTel(string);
    string getDayOfWeek() const;
    virtual void print() const;
protected:
    int month;
    int day;
    string m;
    string dayOfWeek;
    string name;
    string email;
    string tel;
};

#endif

Person.cpp在这

//Person.cpp
#include"Person.h"

Person::Person(int a, int b, string c, string d, string e)
{
    month = a;
    day = b;
    name = c;
    email = d;
    tel = e;
    dayOfWeek = getDayOfWeek();
    string mm[12] = { "January", "February", " March", " April", " May", "June",
        "July", "August", "September", " October", " November", " December" };
    m = mm[month-1];
}
int Person::getMonth() const
{
    return month;
}

int Person::getDay() const
{
    return day;
}
string Person::getName() const
{
    return name;
}
void Person::setEmail(string a)
{
    email = a;
}
void Person::setTel(string a)
{
    email = a;
}
string Person::getDayOfWeek() const
{
    string week[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    int m=month,d=day,y=15;
    if (m < 3)
    {
        m += 12; y--;
    }
    int ans =  y + y / 4 + 26 * (m + 1) / 10 + d - 36;
    ans = (ans % 7 + 7) % 7;
    return week[ans];
}
void Person::print() const
{
    cout << m << '/' << day << '/' << dayOfWeek << "   " << name
        << "   Tel:" << tel << "   Email:" << email << endl;
}

调试时发现函数运行到最后一步时报错

Unhandled exception at 0x5527DF58 (msvcp120d.dll) in AddressBook.exe: 0xC0000005: Access violation reading location 0x0043E4A4.

求大神指导,在下感激不尽

解决方案

指针飞了,看看中途是否改变过指针指向

解决方案二:

请问:这段程序,是输入任意一组数据都会报错?还是要特定的输入数据才会报错?

解决方案三:

Debug下打开call stack,看看函数调用堆栈,先找到出错的函数调用

解决方案四:

非法访问,,引起的错误

解决方案五:

非法访问数据,这个问题要注意主机类型的和访问的地址

时间: 2024-11-30 21:17:45

fstream-c++初学者求助 Access violation reading location的相关文章

异常-c++初学者求助 Access violation reading location

问题描述 c++初学者求助 Access violation reading location 函数如下 #include<iostream>#include<fstream>#include<iomanip>using namespace std; int main(){ int Month Day; string Name Email Address; while (cin >> Month >> Day >> Name >

Unhandled exception at 0x........ in XXXX.exe: 0xC0000005:Access violation reading location 0x......

Unhandled exception at 0x........ in XXXX.exe: 0xC0000005:Access violation reading location 0x...... 对于C++初学者或粗心者,很容易犯如下图所示错误: 那么该错误是由什么造成的呢? 答案无疑只有一个,即:读取了本该没有的值或者地址. 那么如何解决呢? 第一件事,检查下你传入的参数是否合法: 第二件事,若malloc了一块内存,必须记得free: 第三件事,你是否赋值了已经溢出的值或地址. 举例说

dump文件分析-access violation at location 0x000000000000

问题描述 access violation at location 0x000000000000 关机时,程序异常结束,内存不可读写. dump文件分析得到的调用堆栈如下: 根据上面的调用堆栈,好像是回调函数出现问题.请问如何准确定位到代码错误位置? 解决方案 你标框的地方不是已经告诉你了出错的代码文件,行数了,你打开对应的代码分析看看 解决方案二: Unhandled exception at 0x00000000 in CallDll.exe: 0xC0000005: Access viol

c++ 二叉树 access violation?链表不也是这样的吗?为什么以下代码会出错 求助

问题描述 c++ 二叉树 access violation?链表不也是这样的吗?为什么以下代码会出错 求助 #include using namespace std; class binary_tree_node { public: binary_tree_node(const int& init_data = int(),binary_tree_node*init_left=NULL,binary_tree_node* init_right=NULL) { data_field = init_

Application Access Violation or Hang After Applying SP4(从微软站点找来的,E文,看看对你的情况是...

access|application|微软|站点 Application Access Violation or Hang After Applying SP4 This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice. No formal produc

Application Access Violation or Hang After Applyin

Application Access Violation or Hang After Applying SP4 This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.No formal product support is available fro

mfc-VC++报错,Access Violation.

问题描述 VC++报错,Access Violation. void CDahaojiDlg::OnTimer(UINT nlDEvent) { if(nlDEvent==2) { CTimeSpan time=time2-time1; strTime1.Format(""%2d小时 %2d分 %2d秒""time.GetHours()time.GetMinutes()time.GetSeconds()); GetDlgItem(IDC_STATIC29)->

提示access violation at address解决办法

 解决方法: 右击"我的电脑".单击"属性". 在"系统属性"中单击"高级". 在"性能"中单击"设置". 在"性能选项"中单击"数据执行保护". 单击"添加".选择要运行的程序. OK.就这么简单. Access Violation(非法访问)错误的解决方法 Access Violation(非法访问),General Pro

Win8系统提示错误Access Violation怎么办

  1.你如果用翻译软件翻译的话"Access Violation At Address"的意思是"在地址访问冲突",虽然具体意思不是这样,但这个问题是Access Violation计算机用户在运行的应用程序试图存取时未被指定使用的存储区出现的问题. 解决方法:将此应用程序设置为启用DEP. 具体解决步骤 1.首先,需要找到不需要启用DEP的应用程序的安装位置,在后面添加的时候需要按路径找到该应用程序.在桌面快捷方式上右键单击,选择"打开文件位置&quo