Hi all,
i m trying to read from a file and then copy it into an array...my
code is as follow..it runs fine but i cant understand y it doesnt show
me any output??
here is my code...
using namespace std;
int Adj[MAX][MAX]; //The matrix of adjacent nodes
int curr[MAX]; //it gives the current index to retrieve the
neigbours of a node
int n=100;
void read_file(FILE* );
int main()
{
FILE *pf;
int i,r;
if((pf=fopen("nick.txt","r"))== NULL){
printf("error opening nick.txt file");
return 1;
}
read_file(pf); //function to create the matrix Adj[][]
next_neighbour(1);
return 0;
}
void read_file(FILE *fd)
{
int c,
char buffer[500];
int t;
if (!fscanf (fd,"%d\n",&c))
printf("error reading the file");
for (int i=1; i<=n;i++){
fgets (buffer, sizeof(buffer), fd);
t=0;
for (int j=0; j<n;j++){
printf(" %d ", buffer[j]-'0');
if((buffer[j]-'0')== 1)
{
Adj[++t]= j+1;
printf(" %d ", Adj[j]);
//cout<<Ad[j].....but nothin works
}
}
//printf("\n");
//printf("\n");
curr= 1;
}
fclose(fd);
}
-----------------------------------------
the contents of my file looks like as follow:
nick.txt:
0 3 -1 3 -1 -1 -1
3 0 2 -1 -1 -1 -1
-1 2 0 2 2 -1 -1
3 -1 2 0 6 -1 -1
-1 -1 2 6 0 4 3
-1 -1 -1 -1 4 0 -1
-1 -1 -1 -1 3 -1 0
--------------------------------------------------
i want this input to b stored inside an array....how can i read this
file and store it inside array Adj??????any help will b greatly
appreciated...
regards.
i m trying to read from a file and then copy it into an array...my
code is as follow..it runs fine but i cant understand y it doesnt show
me any output??
here is my code...
using namespace std;
int Adj[MAX][MAX]; //The matrix of adjacent nodes
int curr[MAX]; //it gives the current index to retrieve the
neigbours of a node
int n=100;
void read_file(FILE* );
int main()
{
FILE *pf;
int i,r;
if((pf=fopen("nick.txt","r"))== NULL){
printf("error opening nick.txt file");
return 1;
}
read_file(pf); //function to create the matrix Adj[][]
next_neighbour(1);
return 0;
}
void read_file(FILE *fd)
{
int c,
char buffer[500];
int t;
if (!fscanf (fd,"%d\n",&c))
printf("error reading the file");
for (int i=1; i<=n;i++){
fgets (buffer, sizeof(buffer), fd);
t=0;
for (int j=0; j<n;j++){
printf(" %d ", buffer[j]-'0');
if((buffer[j]-'0')== 1)
{
Adj[++t]= j+1;
printf(" %d ", Adj[j]);
//cout<<Ad[j].....but nothin works
}
}
//printf("\n");
//printf("\n");
curr= 1;
}
fclose(fd);
}
-----------------------------------------
the contents of my file looks like as follow:
nick.txt:
0 3 -1 3 -1 -1 -1
3 0 2 -1 -1 -1 -1
-1 2 0 2 2 -1 -1
3 -1 2 0 6 -1 -1
-1 -1 2 6 0 4 3
-1 -1 -1 -1 4 0 -1
-1 -1 -1 -1 3 -1 0
--------------------------------------------------
i want this input to b stored inside an array....how can i read this
file and store it inside array Adj??????any help will b greatly
appreciated...
regards.