Salve,
stavo progettando questo programma che carica una matrice da un file seguendo un andamento degli indici "a spirale".

Il programma, però, anche se mi sembra logicamente corretto, non funziona...

Create un file e riempitelo così:
codice:
4
1 8 7 2 3 4 9 1 5 7 3 2 5 4 7 8
Questo, invece, è il codice:
Codice PHP:
#include <stdio.h>
#include <stdlib.h>
#define M 20

int caricaSpirale(int mat[][M]);
void stampaMatrice(int mat[][M], int dim);

int main()
{
    
int dim;
    
int mat[M][M];
    
    
dim=caricaSpirale(mat);
    if (
dim==-1)
    {
        
printf("Errore\n");
        
system("PAUSE");
        exit(
1);
    }
    else
    {
        
stampaMatrice(mat,dim);

        
printf("\n");
        
system("PAUSE");
    }
}

int caricaSpirale(int mat[][M])
{
    
FILEf;
    
int dim,est,dimOLD,j,i,temp;
    
char nomeFile[M];
    
printf("\nInserisci nome file:\t");
    
scanf("%s"nomeFile);
    
f=fopen(nomeFile,"r");
    
    if (
f==NULL)
    {
        
fclose(f);
        return -
1;
    }
    else
    {
    
fscanf(f,"%d",&dimOLD);
    
dim=dimOLD-1;
    
    for (
i=0i<dimi++)
        for (
j=0j<dimj++)
            
mat[i][j]=0;
    
printf("OK");
    
    
    for (
est=0est<=dim/2est++)
    {
        for (
j=estj<=dim-estj++)
        {
            
fscanf(f"%d"temp);
            
mat[est][j]=temp;
//            printf("%d %d\n", est, j);
        
}
        for (
i=est+1i<=dim-esti++)
        {
            
fscanf(f"%d"temp);
            
mat[i][dim-est]=temp;
//            printf("%d %d\n", i, dim-est);
        
}
        for (
j=dim-est-1j>=estj--)
        {
            
fscanf(f"%d"temp);
            
mat[dim-est][j]=temp;
//            printf("%d %d\n", dim-est, j);
        
}
        for (
i=dim-est-1i>=est+1i--)
        {
            
fscanf(f"%d"temp);
            
mat[i][est]=temp;
//            printf("%d %d\n", i, est);
        
}
    }
    
fclose(f);
    return 
dim;
    }
}

void stampaMatrice(int mat[][M], int dim)
{
    
int i,j;
    
    
printf("\n");
    for (
i=0i<dimi++)
    {
        for (
j=0j<dimj++)
            
printf("%d\t"mat[i][j]);
        
printf("\n");
    }

Grazie degli eventuali aiuti.