简体   繁体   English

ifstream 不从文件中读取值

[英]ifstream doesn't read values from the file

I'm making a program that works with points and files.我正在制作一个处理点和文件的程序。 I have no warnings or errors, but it still doesn't work as it should.我没有警告或错误,但它仍然无法正常工作。 I think the problem is with the ifstream, cause the ofstream works well and puts entered the values into the file.我认为问题出在 ifstream 上,导致 ofstream 运行良好并将输入的值放入文件中。

The output that i get looks like this我得到的输出看起来像这样

Please enter seven (x,y) pairs:
//here the seven pairs are entered

These are your points: 
//(...,...)x7 with the values

These are the points read from the file: 
//and the program ends and returns 0

I hope someone can help me.我希望有一个人可以帮助我。 Here's my code.这是我的代码。

#include <iostream>
#include "std_lib_facilities.h"

using namespace std;

struct Point{
    float x;
    float y;
};

istream& operator>>(istream& is, Point& p)
{
    return is >> p.x >> p.y;
}

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

void f() {
    vector<Point> original_points;
    cout << "Please enter seven (x,y) pairs: " << endl;
    for (Point p; original_points.size() < 7;) {
        cin >> p;
        original_points.push_back(p);
    }
    cout << endl;
    cout << "These are your points: " << endl;
    for (int i=0; i < 7; i++) {
        cout << original_points[i] << endl;
    }
    string name = "mydata.txt";
    ofstream ost {name};
    if (!ost) error("can't open output file", name);
    for (Point p : original_points) {
        ost << '(' << p.x << ',' << p.y << ')' << endl;
    }
    ost.close();
    ifstream ist{name};
    if (!ist) error("can't open input file", name);
    vector<Point> processed_points;
    for (Point p; ist >> p;) {
        processed_points.push_back(p);
    }
    cout << endl;
    cout << "These are the points read from the file: " << endl;
    for (int i=1; i <= processed_points.size(); i++) {
        cout << processed_points[i] << endl;
    }
}

int main()
{
    f();
    return 0;
}

You output brackets and a comma but you don't consume them so your first read operation will fail.您输出括号和逗号,但不使用它们,因此您的第一次读取操作将失败。 Try:尝试:

istream& operator>>(istream& is, Point& p)
{
    char open;
    char close;
    char comma;
    is >> open >> p.x >> comma >> p.y >> close;
    if (open != '(' || close != ')' || comma != ',')
    {
      is.setstate(std::ios_base::failbit);
    }
    return is;
}

Your program will also crash at the end due to going out of the bounds of the vector, you are using indexes from 1 to length , vectors are 0-indexed so should be accessed from 0 to length-1 :由于超出向量的边界,您的程序最终也会崩溃,您使用的索引从1length ,向量是 0 索引的,因此应该从0访问到length-1

for (int i = 0; i < processed_points.size(); i++) {
    cout << processed_points[i] << endl;
}

Or just use a range based loop:或者只使用基于范围的循环:

for (auto& point : processed_points) {
    cout << point  << endl;
}

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

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