Q:

How to fix seg fault core dumped error for ifstream

input file
9
2 8
0 6
8 5
2 4
3 1
2 3
4 1
6 1
2 6
7 5
1 7

Code
#include<iostream>
#include<fstream>
using namespace std;

int main(void)
{
   ifstream in;

   char infile[40];
   int c, u, v;

   cout << "Please enter the input data file name(NO SPACES): ";
   cin >> infile;

   in.open(infile);
   while(in.fail()) {
      cout << "Please enter a CORRECT input data file name(NO SPACES): ";
      cin >> infile;
      in.open(infile);
   }

   //adj matrix
   c = in.get();
   int array[c-1][c-1];
   for(int i=0; i<c; i++) {
      for(int j=0; j<c; j++) {
         array[i][j] = 0;
      }
   }

   while(!in.eof()) {
      u = in.get();
      v = in.get();
      array[u][v] = 1;
   }

   cout << c << endl;
   for(int i=0;i<c;i++) {
      cout << i << "   ";
      for(int j=0;j<c;j++){
         cout << array[i][j] << " ";
      }
      cout << endl;
   }

   in.close();

   return 0;
}

What the output should look like
9
0   0 0 0 0 0 0 1 0 0
1   0 0 0 0 0 0 0 1 0 
2   0 0 0 1 1 0 1 0 1
3   0 1 0 0 0 0 0 0 0
4   0 1 0 0 0 0 0 0 0
5   0 0 0 0 0 0 0 0 0
6   0 1 0 0 0 0 0 0 0
7   0 0 0 0 0 1 0 0 0
8   0 0 0 0 0 1 0 0 0

When prompted for the file name, if I enter an incorrect file name it will continue to prompt me for a CORRECT input file, but when a CORRECT file name is entered I get "Segmentation fault (core dumped)"
I want an adjacency matrix for the corresponding edges given in the input file to be outputted.

A:

std::basic_istream::get will extract one character at a time . So '9' is not same as 9
Do like following assuming your file entities are correct and in known format :
   in >> c;
   int array[c][c]; // Indices from 0 to c-1
   for(int i=0; i<c; i++) {
      for(int j=0; j<c; j++) {
         array[i][j] = 0;
      }
   }

   while( in >> u >> v) {
      array[u][v] = 1;
   }

Also use std::string for file name instead of char array. Then you can use
in.open( infile.c_str() ); // Pre C++11

or directly
in.open(infile); // with C++11

