S
S
SatanistArtem2018-10-15 21:47:58
C++ / C#
SatanistArtem, 2018-10-15 21:47:58

Why is the file not being written?

There is no writing to the file way (created or not created does not matter) from the this-> way vector, although the file is open for reading and writing. The if(way.peek() == EOF) condition is processed correctly. What could be causing this?

void Grid::input_ways(){

    for(int i = 0; i < height; ++i){
        for(int j = 0; j < width; ++j){

            if(grid_nodes[i][j].pass){

                for(int l = 0; l < height; ++l){
                    for(int k = 0; k < width; ++k){

                        if(grid_nodes[l][k].pass){

                            if(i == l && j == k){++k;}

                            fstream way;
                            way.open(path_to_ways_folder + "/way" + to_string(i * width + j) + "to" + to_string(l * width + k) + ".txt", fstream::in | fstream::out);

                            cout << path_to_ways_folder + "/way" + to_string(i * width + j) + "to" + to_string(l * width + k) + ".txt" << endl;

                            if(way.peek() == EOF){

                                astar(i * width + j, l * width + k);

                                cout << "Запись пути от " << (i * width + j) << " к "<< (l * width + k) << endl;

                                for(int p = this-> way.size() - 1; p >= 0; --p){

                                    way << this-> way[p].x;
                                    way << ' ';
                                    way << this-> way[p].y;
                                }

                            }

                            ways.resize(ways.size() + 1);

                            while(!way.eof()){

                                way >> ways.back().x;
                                way >> ways.back().y;
                            }

                            way.close();
                        }
                    }
                }
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2018-10-17
@SatanistArtem

Most likely, the problem is that if the file being opened for fstream::in | fstream::out does not exist, then it will not be created, for this you need to specify either fstream::trunc (the file will be cleared), or fstream::app (data will be appended to the file).
In general, I recommend using more actively overloading the << and >> operators to serialize your own classes.

Somehow so
#include <vector>
#include <iostream>
#include <iterator>
#include <fstream>

struct Node
{
  int x;
  int y;
  Node() : x(0), y(0) {}
  Node(int x, int y) : x(x), y(y) {}

  friend std::ostream& operator<<(std::ostream& os, const Node& node)
  {
    os << node.x << " " << node.y << " ";
    return os;
  }
  friend std::istream& operator>>(std::istream& is, Node& node)
  {
    is >> node.x >> node.y;
    return is;
  }
};

class Way
{
private:
  std::vector<Node> path;

public:
  Way(){}
  Way(const std::vector<Node>& path) : path(path) {}
  Way(const Way& otherWay) : path(otherWay.path) {}

  // в файл пишем количество элементов в path и сам path
  friend std::fstream& operator<<(std::fstream& os, const Way& way)
  {
    os << way.path.size() << " ";
    std::copy(way.path.begin(), way.path.end(), std::ostream_iterator<Node>(os, " "));
    os << std::endl;
    return os;
  }
  friend std::fstream& operator>>(std::fstream& is, Way& way)
  {
    size_t size = 0;
    is >> size;
    way.path.resize(size);
    std::copy_n(std::istream_iterator<Node>(is), size, way.path.begin());
    return is;
  }
  // на экран выводим только содержимое path
  friend std::ostream& operator<<(std::ostream& os, const Way& way)
  {
    std::copy(way.path.begin(), way.path.end(), std::ostream_iterator<Node>(os, " "));
    os << std::endl;
    return os;
  }
};



int main()
{
  std::vector<Node> path0{ Node(3,2), Node(2,4), Node(4,5) };
  std::vector<Node> path1{ Node(1,4), Node(4,3), Node(3,2) };
  Way way0(path0);
  Way way1(path1);

  std::fstream ways;
  ways.open("1.txt", std::ios_base::out | std::ios_base::trunc);
  ways << way0 << way1;
  ways.close();

  Way way0r;
  Way way1r;

  ways.open("1.txt", std::ios_base::in);
  ways >> way0r >> way1r;
  ways.close();

  std::cout << way0r << way1r;
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question