can';我在调用istream作为参数的函数中使用ifstream吗? 这是C++编程原理和实践中第10章的练习。 该程序与以下几点配合使用: 它提示用户输入(x,y)对。然后,它们存储在一个称为原始点的点向量中 原始点将打印到文件中 然后,程序读取文件以检索相同的点,这些点存储在一个称为已处理\u点的点向量中 比较两个向量,如果它们不相同,程序将抛出一个错误

can';我在调用istream作为参数的函数中使用ifstream吗? 这是C++编程原理和实践中第10章的练习。 该程序与以下几点配合使用: 它提示用户输入(x,y)对。然后,它们存储在一个称为原始点的点向量中 原始点将打印到文件中 然后,程序读取文件以检索相同的点,这些点存储在一个称为已处理\u点的点向量中 比较两个向量,如果它们不相同,程序将抛出一个错误,c++,ifstream,C++,Ifstream,问题是processed\u points的size()值为0,只有使用get\u vector\u points()获取这些点时才会发生这种情况。这个函数有什么问题 //put any vector points from ist into points void get_vector_points(istream& ist, vector<Point>& points) { //this function is bad, it doesn't do any

问题是
processed\u points
size()
值为0,只有使用
get\u vector\u points()
获取这些点时才会发生这种情况。这个函数有什么问题

//put any vector points from ist into points
void get_vector_points(istream& ist, vector<Point>& points)
{
    //this function is bad, it doesn't do anything the second time you use it
    while(true)
    {
        Point p;
        if( ist >> p)
            points.push_back(p);
        else return;
    }
}
//
void write_to_file(string& path, vector<Point>& v_of_points)
{
    //open output file
    ofstream ofile(path);

    //output results to file
    for(Point p : v_of_points)
    {
        ofile << p <<endl;
    }

    //close output stream
    ofile.close();
}
void read_from_file(string& path, vector<Point>& v_of_points)
{
    //open input file
    ifstream ifile(path);

    //read from file : Here comes trouble
    get_vector_points(ifile, v_of_points);

    //But no problem if you do the following instead
    //    while(true)
    //    {
    //        Point p;
    //        if( ifile >> p)
    //            v_of_points.push_back(p);
    //        else break;
    //    }
}
//read point from specified istream ist, format:(0,0)
//output points from specified ostream ost, format:(0,0)
void points_drill(string path)
{
    //read from console a vector of points
    vector<Point> original_points;
    get_vector_points(cin, original_points);

    //write the same vector to file
    write_to_file(path,original_points);

    //read the same vector from file, but call it processed
    vector<Point> processed_points;
    read_from_file(path,original_points);

    //they should be the same
    for (int i= 0; i < original_points.size(); i++)
    {
        if (original_points.size()!=processed_points.size())
            throw runtime_error("Size Mismatch"); //error here and proccesed_point has size of 0
        if (original_points[i]!=processed_points[i])
            throw runtime_error("something is wrong!");
    }
}
//将ist中的任何向量点放入点中
无效获取向量点(istream&ist、向量和点)
{
//这个函数不好,你第二次使用它时它什么都不做
while(true)
{
p点;
如果(ist>>p)
点。推回(p);
否则返回;
}
}
//
无效将_写入_文件(_点的字符串和路径、向量和v_)
{
//打开输出文件
文件流(路径);
//将结果输出到文件
对于(p点:v点中的v点)
{
文件呼叫

read_from_file(path,original_points);

正在读取向量
原始\u点
,而不是
处理的\u点

您应该尝试不同的方式读取文件:

void get_vector_points(istream& ist, vector<Point>& points)
{
   std::string line;
   std::string first, second;
   while(std::getline(ist,line)) 
   // this should get every line in the file as a string.
   // if the formatting of the file is 2 doubles per line.
   // You will need to do the parsing yourself by grabbing each value
   {
   // example first line:    123   321
    first = line.split[0]; // this is not a real function this is just Psudo code
    //first = "123"
    second = line.split[1];// for parsing text and splitting into 2 doubles
    // second = "321"
    Point p;
    p.x = atoi(first); // atoi is = ascii to int.  Makes a string intager into a 
    p.y = atoi(second);// actual int data type
    // p.x = 123   p.y = 321
    points.push_back(p)
    else return;
   }
}
void获取向量点(istream&ist、向量和点)
{
std::字符串行;
std::字符串第一,第二;
while(std::getline(ist,line))
//这将使文件中的每一行都成为字符串。
//如果文件格式为每行2倍。
//您需要通过获取每个值自己进行解析
{
//第一行示例:123 321
first=line.split[0];//这不是真正的函数,这只是Psudo代码
//first=“123”
second=line.split[1];//用于解析文本并拆分为2个双精度
//second=“321”
p点;
p、 x=atoi(第一个);//atoi is=ascii到int。将字符串插入到
p、 y=atoi(秒);//实际的int数据类型
//p.x=123 p.y=321
点。推回(p)
否则返回;
}
}

此外,您的函数
获取向量点
将使输入流处于失败状态。您可以检查EOF并读取分隔符(空格)以避免它

这里是整数:

#include <iostream>
#include <sstream>
#include <vector>

std::istream& get_integers(std::istream& stream, std::vector<int>& integers)
{
    int n;
    // Read integers and trailing white spaces.
    while( ! stream.eof() && stream >> n) {
        integers.push_back(n);
        stream >> std::ws;
    }
    return stream;
}

int main()
{
    std::istringstream input(" 1 2 3 ");
    std::vector<int> integers;
    // Read and test the result:
    if( ! get_integers(input, integers)) std::cout << "Failure\n";
    else {
        for(int i : integers)
            std::cout << i;
        std::cout << '\n';
    }
}
#包括
#包括
#包括
std::istream&get_整数(std::istream&stream,std::vector&integers)
{
int n;
//读取整数和尾随空格。
而(!stream.eof()&&stream>>n){
整数。推回(n);
流>>std::ws;
}
回流;
}
int main()
{
std::istringstream输入(“1 2 3”);
向量整数;
//阅读并测试结果:

如果(!get_integers(input,integers))std::cout
get_vector_points
cin
读取,而不是从传递给它的流中读取,
ist
确定,我已更正该错误并尝试再次运行。相同的错误仍然存在。嗯……我刚刚发现另一个错误,我从文件(路径,原始点)写入了
read_)
而不是
从文件(路径,处理点)读取;
我不敢相信我错过了它。这就是问题的结束……这正是我在五分钟前的回答中写的我想页面的那部分没有刷新,抱歉