Home > Back-end >  How to use fscanf_s obtain two-dimensional array in TXT
How to use fscanf_s obtain two-dimensional array in TXT

Time:09-19

I want to use fscanf_s function, called the inside of the TXT two-dimensional arrays, but don't know how to use, consult
//read weight matrix
Int * * readWeight (int & amp; N, char * filename)
{
FILE *fp;
Fp=fopen (filename, "r");
If (fp==NULL)
{
Printf (" an error occurred when open the file % s \ n ", filename);
exit(1);
}

Int I, j, w * *;
The fscanf (fp, "% d", & amp; n);
W=(int * *) calloc (n, sizeof (int *));
for(i=0; i {
W [I]=(int *) calloc (n, sizeof (int));
}

for(i=0; i {
for(j=0; J & lt; n; J++)
{
The fscanf (fp, "% d", & amp; W [I] [j]);
}
}
fclose(fp);
Return w;
}
Don't know how to change, the paper may be because of my weight is wrong, the weight information below
0,2,7,2,5
2,0,3,5,1
7,3,0,4,6
2,5,4,0,3
5,1,6,3,0

CodePudding user response:

All weights in the file, comma try replacing Spaces,

CodePudding user response:

Tried it on, it is no problem, but still error, I don't know you don't understand the TSP problem, I attach, the code is run out as a result, the distance matrix was
2 5 0 2 7
2 0 3 5 1
7 3 0 4 6
2 5 4 0 3
5 1 6 3 0
But the interrupt, so I changed it to
65535 7 2 5
2 65535 3 5 1
July 3, 65535 6
2 5 4 3 65535
5 1 6 3 65535
To this purpose is to let each city without their own
Source code is attached, hope the great god directions:
# include "stdafx. H"
# include
# include
# include
# include


Typedef struct tf * cnode.

Struct tf {
Int currentNode;//the current node
Int the set;//the current state of the collection, use binary representation
Int pathLength.//the path of the current value of the
The cnode last;//record on a node, in order to save a information
Cnode next;//record the next node
};

Struct TSP
{
int count;//the number of the current city
LTF cnode *;//record struct tf array
};


Void output1 (int * a, int n)
{
int i;
for (i=0; i Printf (" % d ", a [I]);
printf("\n");
}

* * a void output2 (int, int n)
{
Int I, j;
for (i=0; i {
For (j=0; J & lt; n; J++)
{
Printf (" % d ", a [I] [j]);
}
printf("\n");
}
}


//custom power function & lt; Used for the collection of said after the & gt;
Int mypow (int x, int y)
{
Return (int) pow ((double), x (double) y);
}

//whether the element I in the set in the collection
Int an inSet (int set, int I)
{
If ((set& Mypow (2, I - 1)) & gt; 0)
return 1;
The else
return 0;
}

//insert element I set the set in the
Void insertSet (int & amp; The set, int I)
{
Int p=mypow (2, I - 1);
The set=set | p;
}

//remove the set of I
Void deleteSet (int & amp; The set, int I)
{
The set=~ (mypow (2, I - 1)) & amp; set;
}

//read weight matrix
Int * * readWeight (int & amp; N, char * filename)
{
FILE *fp;
Errno_t err;
If ((err=fopen_s (& amp; Fp, filename, "r"))!=0)
{
Printf (" an error occurred when open the file % s \ n ", filename);
exit(1);
}
Int I, j, w * *;
Fscanf_s (fp, "% d", & amp; n);
W=(int * *) calloc (n, sizeof (int *));
for (i=0; i {
W [I]=(int *) calloc (n, sizeof (int));
}

for (i=0; i {
For (j=0; J & lt; n; J++)
{
Fscanf_s (fp, "% d", & amp; W [I] [j]);
}
}
fclose(fp);
Return w;
}

//return combinations of C (n, r) value
Int ZuHeShu (int n, int r)
{
Int I, c, k, * a;
A=(int *) calloc (n, sizeof (int));
for (i=0; i {
A [I]=I + 1;
}

C=0;
Do {
I=1;
For (k=0; K & lt; r; K++)
If (a [k] I=k;
if (i !=1)
{
A [I] + +;
For (k=I + 1; K & lt; r; K++)
A [k]=a [k - 1) + 1;

c++;
}
} while (I!=1);
Free (a);
Return c + 1;
}


//specified combination c (n, r) in all possible combinations, returns an int array of 01 after its transformation format
Int * ZuHeArray (a0 int * and an int n, int r, int sn)
{
Int I, c, k, * set, * a;
C=0;
The set=(int *) calloc (sn, sizeof (int));
for (i=0; i {
The set [I]=0;
}

A=(int *) calloc (n, sizeof (int));
for (i=0; i {
A [I]=I + 1;
}

Int t;

for (i=0; i {
T=a, [I] 1.
InsertSet (set [c], a0 [t]);
}

Do {
I=1;
For (k=0; K & lt; r; K++)
If (a [k] I=k;
if (i !=1)
{
A [I] + +;
For (k=I + 1; K & lt; r; K++)
A [k]=a [k - 1) + 1;

c++;
For (k=0; K & lt; r; K++)
{
T=a, [k] 1.
InsertSet (set [c], a0 [t]);
}
}
} while (I!=1);

Free (a);
Return the set;

}

//set the last collection
Int setLastSet (int n)
{
Int I, set;
The set=0;
for (i=0; i {
The set=set | mypow (2, I);
}
Return the set;
}

//gets the first row of all elements in the set set, the number of total elements for nInt * getSetNum (int the set, int row, int n)
{
Int I, j, * a;
A=(int *) calloc (row, sizeof (int));
J=0;

For (I=1; i {
If (an inSet (set, I)==1)
{
A [j]=I;
J++;
}
If (j==row)
{
break;
}
}
return a;
}

//will be set into a binary number
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related