无法打开文件并读取文件

时间:2019-02-18 08:44:18

标签: c++ file ifstream istream

我使用“ temperatures.txt”来调用代码,该文件的格式如下:

-9.3 -15.1

-9.0 -14.1

..

..

temps也是一个结构,它具有:int max,min; //温度 最终的矢量应用于填充两条open_polylines并构造具有最高和最低温度的图形,但它会产生一个空窗口->因此,必须确保要返回的矢量(vec)为空

istream& operator>>(istream& is, Temps& t){
    //assume format (max----min)
    string c1="    ";
    Temps tt;

    is >> t.max >> t.min;
    //return is;}
    t=tt;
    return is;
}

vector<Temps> readTemp(string fileName){
    ifstream temp_file{fileName};
    Temps t;
    vector<Temps> vec;
    if(!temp_file){
        error("Can't open ", fileName);
    }

    while(temp_file>>t){
        cout<<t.max << t.min;
        vec.push_back(t);
    }
    return vec;
}

1 个答案:

答案 0 :(得分:1)

此代码错误

class ViewItems extends React.PureComponent {
constructor(props) {
    super(props);
    this.default_root_item = {
        name: 'Items',
        index: 0,
        children: [],
    };
    this.state = {
        root_items: this.default_root_item,
        items: [],
    };
 }

componentDidMount() {
    this.load_items();
}

componentDidUpdate(PrevProps) {
    if (PrevProps.item_id !== this.props.item_id) {
        this.load_items();
    }
}

componentWillUnmount() {
    this.unlisten_path_change();
}

load_items = () => {
    const file_name = 'file_name.json';
    client.get_file(this.props.item_id, file_name, 'json')
        .then((request) => {
            const items = request.response;
            this.setState({items: [this.default_root_item]});}
    this.handle_path_change(this.props.location.pathname);})};

    return (
        <ChildComponent            
             on_close={this.handle_item_close}
             root_item={this.state.root_item}/>)}

export default class ChildComponent extends React.PureComponent {
    <Items
        items={root_item}/>
    function Items(props) {
    return (
        <ul className="Items_list">
            <div className="items">
               {props.items.map((item, index) => {
                return (
                   <Item
                       key={index}
                       item={item}
                   />
                );
               })}
            </div>
        </ul>
       );
    }
}

export function get_file(qrcode, file_name, response_type,on_progress, cancel) {
    const local_override_defined = item_files[qrcode] && item_files[qrcode][file_name];
    if (local_override_defined) {
        const file = item_files[qrcode][file_name];
        const reader = new FileReader();

        return new Promise(resolve => {
            if (response_type === 'blob') {
                resolve({response: file});
            } else {
                  reader.onload = () => {
                  if (response_type === 'json') {
                      resolve({response: JSON.parse(reader.result)});
                  } else {
                      resolve({response: reader.result});
                  }
            };
            reader.readAsText(file);
        }
    });
}

 return new Promise((resolve, reject) => {
    item_file_get_url(qrcode, file_name).then(({response}) => {
        const request = new XMLHttpRequest();
        request.addEventListener('progress', on_progress);
        request.open('GET', response.url);
        request.responseType = response_type;
        send_request(request, undefined, cancel, response.url).then(resolve).catch(reject);

    }).catch(reject);
});

您将温度读入istream& operator>>(istream& is, Temps& t){ //assume format (max----min) string c1=" "; Temps tt; is >> t.max >> t.min; //return is;} t=tt; return is; } ,但随后用t覆盖了它们。就是这样,不需要tt(或tt)。

c1