【问题标题】:Segmentation fault reading file分段错误读取文件
【发布时间】:2016-11-13 15:08:48
【问题描述】:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

struct Point
{
    double x;
    double y;
};

istream& operator>>(istream& is, Point& p)
{
    char ch;
    if(is >> ch && ch != '(')
    {
        is.unget();
        is.clear(ios_base::failbit);
        return is;
    }

    char ch2;
    double x;
    double y;

    is >> x >> ch >> y >> ch2;
    if(!is || ch != ';' || ch2 != ')')
    {
        cerr << "Error: Bad record!\n";
        exit(1);
    }
    p.x = x;
    p.y = y; 
}

ostream& operator<<(ostream& os, const Point& p)
{
    return os << '(' << p.x 
    << ';' << p.y << ')' << endl;
}

int main()
{
    Point p;
    vector<Point> original_points;
    cout << "Please enter 3 points:\n";
    for(int i = 0; i < 3; ++i)
    {
        cin >> p;
        original_points.push_back(p);
    }

    cout << "\nYou've entered:\n";

    for(Point x : original_points)
        cout << x;

    ofstream ost{"mydata"};
    for(Point x : original_points)
        ost << x;
    ost.close();

    vector<Point> processed_points;
    ifstream ist{"mydata"};
    while(ist >> p)
        processed_points.push_back(p);
    ist.close();

    cout << "original_points: ";
    for(Point x : original_points)
        cout << x;

    cout << "processed_points: ";
    for(Point x : original_points)
        cout << x;

    if(original_points.size() != processed_points.size())
        cout << "Oops! Seems like something went wrong!\n";
    return 0;
}

经过调试发现错误是由这行代码引起的:

while(ist >> p)

这部分代码几乎 100% 抄自书中:

istream& operator>>(istream& is, Point& p)
{
    char ch;
    if(is >> ch && ch != '(')
    {
        is.unget();
        is.clear(ios_base::failbit);
        return is;
    }

    char ch2;
    double x;
    double y;

    is >> x >> ch >> y >> ch2;
    if(!is || ch != ';' || ch2 != ')')
    {
        cerr << "Error: Bad record!\n";
        exit(1);
    }
    p.x = x;
    p.y = y; 
}

Google 和 stackoverflow 表示此错误是由于以错误的方式访问内存造成的。我检查了这个代码一个小时,我不知道是什么导致了问题。我今天开始学习流,这是“编程-使用C++的原理和实践(第二版)”第10章的一个练习。

附:对不起我的英语语法,这不是我的母语)

【问题讨论】:

  • 您需要进入您的&gt;&gt; 过载,并确定段错误发生的位置。
  • 一小时是很短的时间。继续努力。过几天再来。
  • 好吧,我在写这段代码的时候只有纯控制台。我会遵循上面的建议,希望能发布解决方案。

标签: c++ iostream


【解决方案1】:

并非函数operator&gt;&gt;(istream&amp; is, Point&amp; p) 中代码的所有分支都会返回最终值,所以我想你忘记了在函数末尾推送返回值return is;

istream& operator>>(istream& is, Point& p)
{
    char ch;
    if(is >> ch && ch != '(')
    {
        is.unget();
        is.clear(ios_base::failbit);
        return is;
    }

    char ch2;
    double x;
    double y;

    is >> x >> ch >> y >> ch2;
    if(!is || ch != ';' || ch2 != ')')
    {
        cerr << "Error: Bad record!\n";
        exit(1);
    }
    p.x = x;
    p.y = y; 
    return is;
}

【讨论】:

  • 是的,当我使用 C::B 时,我很快就发现了这一点,编译器立即警告我。但是我遇到了另一个问题。我的错误处理程序无法识别文件结尾或类似的东西,而我在调试时发现在最后一个值是红色程序之后再次进入>>函数并导致错误“错误:错误记录!\ n”,但是所有值都是正确的红色并存储在向量中。首先,我认为当我们达到 EOF 时,while(ist >> p) 变为错误。其次, if(is >> ch && ch != '(') 不应该让程序达到第二个 if。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多