0

Creating a program that will read in an adjacency matrix, create a graph with it and the vertex count, and then use the necessary information to write a .dot file for GraphViz.

Every time I try to run the program, the last-accessed string fileNameX variable is always corrupted (stack around the variable fileName[x] is corrupt). I don't know what I'm doing wrong, and why it's doing this, as I've never had this error come up before. Any ideas?

On a lesser note, when I run the program, it does not create or open the files in question in the directory, but I assume that's part of this problem. If not, feel free to correct me.

The main function and the print function of the graph class is below.

#include <fstream>
#include "graph.h"
#include "string"
using namespace std;

int main()
{
ifstream infile;
infile.open("input.txt");
char c;
int i, count = 0;
string fileName1 = "graph0.dot", fileName2 = "graph1.dot", fileName3 = "graph2.dot";

while (infile>>c)
{
    bool adjacencies[10][10];

    infile.get(c);
    i = (int)c;

    // Loops looking for 1s and 0s to "store" in adjacency matrix
    for (int x = 0; x < i; x++)
    {
        for (int y = 0; y < i; y++)
        {
            infile.get(c);
            while (c != '1' && c != '0')
                infile.get(c);
            if (c == '1')
                adjacencies[x][y] = true;
            else if (c == '0')
                adjacencies[x][y] = false;
        }
    }

    graph G(adjacencies, i);
    if (count == 0)
        G.GraphVizOut(fileName1);
    else if (count == 1)
        G.GraphVizOut(fileName2);
    else if (count == 2)
        G.GraphVizOut(fileName3);
    count++;
}

    system("pause");
    return 0;
}

// Notes paths between vertices with "X -- Y" notation.
void graph::CreateEdges(std::ofstream &outfile)
{

// If matrix is symmetrical, graph is undirected
if (isSymmetrical == true)
{
    for (int x = 0; x < VertCount; x++)
    {
        for (int y = x; y < VertCount; y++)
        {
            if (adjacency[x][y] == true)
                outfile << x << " " << "--" << y << "\n";
        }
    }
}
// If matrix is not symmetrical, graph is directed
else
{
    for (int x = 0; x < VertCount; x++)
    {
        for (int y = 0; y < VertCount; y++)
        {
            if (adjacency[x][y] == true)
                outfile << x << " " << "--" << y << "\n";
        }
    }
}

return;
}

// Creates the file, writes header information, and then calls CreateEdges above necessary info write to it
void graph::GraphVizOut(std::string filename)
{
std::ofstream VizOut;
VizOut.open(filename);
VizOut << "// Trey Brumley \n";
VizOut << "// File created by C++ BST Project \n";
VizOut << "graph G { \n";
CreateEdges(VizOut);
VizOut << "} \n";
VizOut.close();

return;
}
  • 2
    Not necessarily related: `while (!infile.eof())` [is almost always wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Use `while(infile>>c)` instead. – vsoftco May 06 '15 at 12:50
  • If you say so. Went ahead and changed it. – Trey Brumley May 06 '15 at 12:52
  • The stack corruption probably comes from some out-of-bound access. Try to debug and make sure your array indexes are not larger than the size of the array minus one. Also, in your loop you have a whole bunch of `infile.get(c)`, but you're not testing whether you read `eof`. I suggest you use `getline` to read each line from the file, then pass the string to a `stringstream`, and finally extract the `stringstream` into the current line of the adjacency matrix. – vsoftco May 06 '15 at 12:53
  • The input values are no larger than 7. I had the adjacency go up to 10 as a precaution. The program compiles, but it never actually writes anything; even when I hit continue, it leaves nothing behind aside from logs and stuff. – Trey Brumley May 06 '15 at 12:57
  • `while (!infile.eof())` Where did you learn to do that? BTW instead of "if you say so" why not follow the link vsoftco gave you so that you can _learn_ why your code does what it does? – Lightness Races in Orbit May 06 '15 at 12:58
  • This is no testcase. For one, you did not define `VertCount`. – Lightness Races in Orbit May 06 '15 at 12:59
  • I did, but I didn't include the whole class definition or header; just the necessary print code, to see what I was doing wrong with that error in question. – Trey Brumley May 06 '15 at 13:04
  • And Lightness, the link wasn't there at the time I first read it. Or if it was, I didn't see. I'll read up on it now, though. Thanks for bringing it to my attention. – Trey Brumley May 06 '15 at 13:04
  • @TreyBrumley You could have at least prevented the problem if you changed your loops to not exceed 9. `for (int x = 0; x < i && x < 10; x++) { for (int y = 0; y < i && y < 10; y++)` It may not have given you the desired results, but it throttles the loop to not go beyond your array boundaries. – PaulMcKenzie May 06 '15 at 13:19

1 Answers1

3
infile.get(c);
i = (int)c;

This reads one character from the file, and stores its (typically ASCII) value in i. ASCII digits are in the range [48,57], hence the out-of-bound write in your adjacencies array when you index it from 0 to i.

To fix it quickly : translate the ASCII value so the number is correct.

infile.get(c);
i = c - '0';

To fix it properly : just let std::istream read the actual int.

infile >> i
if(i < 1 || i > 10) {
    // Invalid input !
}

To prevent it : use std::array instead of C-style arrays.

std::array<std::array<bool, 10>, 10> adjacencies;
Quentin
  • 58,778
  • 7
  • 120
  • 175