简体   繁体   English

分段故障读取文件

[英]Segmentation fault reading file

#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;
}

After debugging I've figured out that the error is caused by this line of code: 调试之后,我发现错误是由以下代码行引起的:

while(ist >> p)

This part of code is almost 100% copied from book: 这部分代码几乎是从书本中复制的:

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 and stackoverflow says that this error is caused by accessing memory in wrong way. Google和stackoverflow表示此错误是由于错误地访问内存引起的。 I was checking this code for an hour and I can't figure out what causes problem. 我花了一个小时检查此代码,但无法弄清楚是什么原因引起的。 I've started studying streams today and this is an exercise in 10th chapter of "Programming - Principles and Practice Using C++ (Second Edition)". 我今天开始研究流,这是“编程-使用C ++的原理和实践(第二版)”第10章的练习。

PS Sorry for my english grammar, it's not my native language ) PS对不起,我的英语语法,这不是我的母语)

Not all branches of your code in function operator>>(istream& is, Point& p) lead to returning final value so I suppose that you're forgot push returning value return is; 并不是函数operator>>(istream& is, Point& p)中的代码的所有分支都导致返回最终值,所以我想您忘记了推返回值return is; in the end of function 在功能结束时

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM