#include <stdio.h>
#include <iostream>
using namespace std;
int main(void)
{
bool premiereLignefaite = false;
//Lire le fichier
FILE * graphe = fopen("graphe.txt", "r");
//Fichier de sortie
FILE * resultat = fopen("resultat.txt", "w");
int nbr1, nbr2;
int *matrice; //pointeur vers la matrice d'adjacence
//Ligne lue
static char ligne[50];
while (fgets(ligne, 50, graphe) != NULL) //retourne 0 quand on a end-of-file
{
//La premiere ligne est différente
if (premiereLignefaite == false) {
//Initialiser une matrice d'adjacence NxN
sscanf(ligne, "%d %d", &nbr1, &nbr2);
matrice = new int(nbr1 * nbr1); //Memoire dynamique pour la matrice dadjacence n x n
premiereLignefaite = true;
continue;
}
//On construit notre matrice d'adjacence
sscanf(ligne, "%d %d", &nbr1, &nbr2);
matrice[nbr1][nbr2] = 1;
}
int u = 2+2;
return 0;
}
Итак, я получаю сообщение об ошибке в этой строке:
Matrice [nbr1] [nbr2] = 1;
Я просто пытаюсь построить список смежности из текстового файла. Я не понимаю, что я делаю не так. Спасибо.
РЕДАКТИРОВАТЬ: Поскольку люди спрашивают об этом, это мой файл графика. Первая строка - количество вершин и количество ребер (бесполезно imo)
Следующие строки - мои края, я использую первую строку для выделения памяти для графа NxN и следующие строки для заполнения моей матрицы смежности.
9 20
0 1
0 2
1 0
1 2
1 3
1 5
2 0
2 1
2 3
3 1
3 2
3 4
4 3
5 1
5 6
5 7
6 5
6 8
7 5
8 6