1

I write a graph to a simple text file format (adjacency list). In one instance, I discovered that the resulting file contains a strange very long line of "NUL" characters.

enter image description here

Where might these null characters come from? What do they mean?

The code that produces the file is

void GraphIO::writeAdjacencyList(Graph& G, std::string path) {
    std::ofstream file;
    file.open(path.c_str());

    G.forallNodes([&](node v) {
        file << v;
        G.forallNeighborsOf(v, [&](node x) {
            file << " " << x;
        });
        file << std::endl;
    });
    INFO("wrote graph to file: " << path);
}

Iteration over all nodes is realized as:

template<typename Callback>
inline void EnsembleClustering::Graph::forallNodes(Callback func, std::string par) {
    assert ((par == "") || (par == "parallel"));
    int64_t n  = this->numberOfNodes();
    #pragma omp parallel for if (par == "parallel")
    for (node v = this->firstNode(); v <= n; ++v) {
        // call node function
        func(v);
    }
}

EDIT: the type node is simply a typedef for int64_t

clstaudt
  • 21,436
  • 45
  • 156
  • 239
  • 2
    And output operator for node? – ForEveR Jan 28 '13 at 11:03
  • 2
    parallel writes to an ofstream? is that safe? – Dariusz Jan 28 '13 at 11:05
  • 1
    `forallNodes` accepts a string but you don't pass it one inside `writeAdjacencyList`. What gives? – jrok Jan 28 '13 at 11:05
  • @jrok The string `""` is passed as default argument. – clstaudt Jan 28 '13 at 11:06
  • @DariuszWawer I think that the write is not parallel since the argument `par` is `""` by default, not `"parallel"`. – clstaudt Jan 28 '13 at 11:07
  • @ForEveR `node` is `int64_t` – clstaudt Jan 28 '13 at 11:07
  • 1
    I don't see anything inherently wrong in this code, perhaps the bug is in `forallNeighborsOf`. In any case, it's hard to visually debug partial code, so try producing a minimal testcase. – jrok Jan 28 '13 at 11:16
  • @jrok I don't think I can produce a testcase which you would consider minimal, since `forallNeighborsOf` depends on library code (research code) which is not my own. It's almost as difficult as building the entire project. – clstaudt Jan 28 '13 at 11:32
  • 1
    You can try creating a minimal graph that reproduces the problem and then check after each write when the nulls are written. – dutt Jan 28 '13 at 11:56
  • @dutt Okay I'll try. But I do not know on which kind of graphs this problem appears. – clstaudt Jan 28 '13 at 11:59
  • but you gave us a screenshot of such a file? otherwise it seems you have a bit of work to do :) – dutt Jan 28 '13 at 12:04
  • 1
    Such messed up output is probably the result of lacking synchronization around writing the output. – vonbrand Jan 30 '13 at 12:43
  • I'm also facing this problem. Did you found the reason for this? – Jan Mar 30 '22 at 09:10

0 Answers0