繁体   English   中英

从.in文件中读取对象

[英]Reading to object from .in file

你好我有一个3个int的列表,间隔2个空格,我想读它们并创建一个对象。 我将从数据结构,类和前/后数据中粘贴我的代码。 我找不到错误的原因。 欢迎任何帮助:

Class.h:

class RAngle{
private:
    int x,y,l,b; 
    int solution,prec;
    RAngle(){
        x = y = solution = prec = b = l = 0;
    }

    RAngle(int i,int j,int k){
        x = i;
        y = j;
        l = k;
        solution = 0; prec=0; b=0;
    }

    friend ostream& operator << (ostream& out, const RAngle& ra){
        out << ra.x << " " << ra.y<<" " << ra.l <<endl;
        return out;
    }

    friend istream& operator >>( istream& is, RAngle& ra){
        is >> ra.x;
        is >> ra.y;
        is >> ra.l;

        return is ;
    }

};

dataStructure.h:

template <class T>
class List
{
private:
    struct Elem
    {
        T data;
        Elem* next;
    };

    Elem* first;

    void push_back(T data){
    Elem *n = new Elem;
    n->data = data;
    n->next = NULL;
    if (first == NULL)
    {
        first = n;
        return ;
    }
    Elem *current;
    for(current=first;current->next != NULL;current=current->next);
    current->next = n;
}

main.cpp中:

void readData(List <RAngle> &l){
    *RAngle r;
    int N;
    ifstream f_in;
    ofstream f_out;

    f_in.open("ex.in",ios::in);
    f_out.open("ex.out",ios::out);

    f_in >> N;
    for(int i=0;i<13;++i){
        f_in >> r;
        cout << r;
        l.push_back(r);
    }

    f_in.close();
    f_out.close();
}*

输入数据:

3 1 2
1 1 3
3 1 1

输出(在读取时打印):

1 2 1
1 3 3
1 1 3

正如你所看到的那样它开始以第二个位置开始阅读并以第一个结束。

读取f_in >> N的行首先将计数读入变量,但输入文件缺少该计数。 (或者更确切地说,它将第一个'3'视为计数,这意味着元组实际上以'1 2 1 ...'开头)。

正如你所看到的那样它开始以第二个位置开始阅读并以第一个结束。

那是因为你提取了第一个值。

readData函数中:

f_in >> N;    //  This line will extract the first value from the stream.

// the loop starts at the second value
for(int i=0;i<13;++i){
f_in >> r;
    cout << r;
    l.push_back(r);
}

暂无
暂无

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

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