| painkillerfff |
10Dec2007 14:48 |
need some help on reading a file in this program
Code:
#include <stdio.h> /* printf, fscanf etc. */
#include <stdlib.h> /* exit */
#include <string.h> /* strcmp */
#include <time.h> /* clock, clock_t, CLOCKS_PER_SEC */
#include <assert.h> /* Assertion checking */
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 1000000
#endif
#define max( a, b ) ( (a) > (b) ? (a) : (b) )
#define MALLOC(t) (t *) malloc( sizeof(t) )
#define MALLOC_ARRAY(t,n) (t *) malloc( (n)*sizeof(t) )
#define CHECKPTR(p) if (!p) Error("Out of memory")
static const char filename[30]="distance3.txt"; //filename to read from
static const char filename2[30]="name.txt"; //filename to write to
char line[100];
char delims[] =",";
char *result = NULL;
void Error(char *text)
{ printf("\nERROR: %s\n", text);
exit(1);
}
/***[ Graph Data structures ]****************************************/
typedef struct Edge {
int u;
int v;
float w;
struct Edge *next;
} Edge;
Edge **adj;
Edge **edges;
int n, m, m_actual; /* Number of vertices and edges */
void CreateEdge( int u, int v, float w )
{
Edge *e = MALLOC( Edge ); CHECKPTR( e );
if (u<0 || u>=n || v<0 || v>=n) Error("Wrong vertex numbers");
if ( m_actual >= m ) Error( "Too many edges" );
e->u = u;
e->v = v;
e->w = w;
e->next = adj[u];
adj[u] = e;
edges[m_actual++] = e;
}
Edge *FindEdge( int u, int v )
{
Edge *e;
for ( e = adj[u]; e; e = e->next )
if ( e->v == v )
return e;
return NULL;
}
void InsertEdge(int u, int v, float w)
{
Edge *e;
if (u<0 || u>=n || v<0 || v>=n) Error("Wrong vertex numbers");
if ( u != v ) {
e = FindEdge( u, v );
if ( e ) {
e->w = max( e->w, w );
e = FindEdge( v, u );
assert( e );
e->w = max( e->w, w );
} else {
CreateEdge( u, v, w );
CreateEdge( v, u, w );
}
}
}
void InitializeGraph()
{
adj = calloc( n, sizeof(Edge *) ); CHECKPTR( adj );
edges = MALLOC_ARRAY( Edge *, m ); CHECKPTR( edges );
m_actual = 0;
}
/****[ Disjoint Set Datastructure ]********************************/
typedef struct DisjointSet {
struct DisjointSet *p; /* Parent pointer */
int rank; /* Rank */
} DisjointSet;
DisjointSet **ds; /* The disjoint Set for each of the n vertices */
DisjointSet *MakeSet()
{ DisjointSet *x = MALLOC( DisjointSet ); CHECKPTR( x );
x->p = x;
x->rank = 0;
return x;
}
void InitializeDS()
{ int i;
ds = MALLOC_ARRAY( DisjointSet *, n ); CHECKPTR( ds );
for (i=0; i<n; i++) ds[i] = MakeSet();
}
static void Link( DisjointSet *x, DisjointSet *y )
{ if ( x->rank > y->rank )
y->p = x;
else {
x->p = y;
if ( x->rank == y->rank )
y->rank++;
}
}
DisjointSet *FindSet( DisjointSet *x )
{ if ( x != x->p )
x->p = FindSet( x->p );
return x->p;
}
void Union( DisjointSet *x, DisjointSet *y )
{ Link( FindSet( x ), FindSet( y ));
}
/***[ EdgeSet: a very simple set of edges ]************************/
typedef struct EdgeSet {
Edge *e;
struct EdgeSet *next;
} EdgeSet;
EdgeSet *new_EdgeSet( Edge *e, EdgeSet *next )
{
EdgeSet *es = MALLOC( EdgeSet );
es->e = e;
es->next = next;
return es;
}
/***[ QuickSort ]**************************************************/
int Partition( int p, int r )
{
float x = edges[p]->w;
int i = p-1;
int j = r+1;
while ( 1 ) {
do {
j--;
} while ( edges[j]->w > x );
do {
i++;
} while ( edges[i]->w < x );
if ( i < j ) {
register Edge *e = edges[i];
edges[i] = edges[j];
edges[j] = e;
} else
return j;
}
}
void QuickSortEdges( int p, int r )
{
if ( p < r ) {
int q = Partition( p, r );
QuickSortEdges( p, q );
QuickSortEdges( q+1, r );
}
}
/***[ Kruskals alg. ]************************************************/
EdgeSet* MST_Kruskal()
{
EdgeSet *A = NULL;
int i;
/* sort the edges of E by non-decreasing weight */
{
clock_t cl = clock();
QuickSortEdges( 0, m_actual-1);
printf("Time to sort: %f\n", (float)(clock()-cl)/(float)CLOCKS_PER_SEC);
}
for ( i = 0; i < m_actual; i++ ) {
Edge *e = edges[i];
int u = e->u;
int v = e->v;
if ( FindSet( ds[u] ) != FindSet( ds[v] ) ) {
A = new_EdgeSet( e, A ); /* Add edge e to A set */
Union( ds[u], ds[v] );
}
}
return A;
}
void print_MST( EdgeSet *A )
{
float w = 0.0;
EdgeSet *h;
printf( "MST: " );
for ( h = A; h; h = h->next ) {
printf( "(%d, %d) ", h->e->u, h->e->v );
w += h->e->w;
}
printf("\nWeight of MST: %.1f\n", w );
}
void main()
{
clock_t cl1, cl2;
EdgeSet *A;
int u, v;
float w;
FILE *infile = fopen(filename,"r");
if (!infile) Error("Could not open file");
fscanf(infile,"%d %d", &n, &m);
m *= 2; /* Since graph is undirected */
printf("Reading %s with %d vertices and %d edges \n", filename, n, m);
InitializeGraph();
while (fscanf(infile,"%d %d %f",&u,&v,&w) != EOF)
InsertEdge( u, v, w);
//int u, v, i, j, rowCounter,test;
// float w;
/****************************************************************************************************/
/****************************************************************************************************/
/****************************************************************************************************/
/*
FILE *filePointer1; // filepointer to the input file
srand(time(NULL)); // randomize the seed of the random number generator
filePointer1=fopen(filename,"r"); //open input file in read mode
if(filePointer1==NULL) // if file does not exist in same folder, exit with error message
{
printf("Error opening file! Exiting now!\n");
exit(0);
}
if ( filePointer1!=NULL ) // if input file is present in same folder
{
rowCounter =0;
while (fgets(line,100,filePointer1) != NULL) // repeatedly read a line till the end of file has been reached
{
printf(line);
printf("\n");
result=strtok(line,delims);
j=0;
while( result != NULL ) {
test = atoi(result);
result = strtok( NULL, delims );
j++;
}
rowCounter++;
}
fclose(filePointer1); // close file pointer after reading entire file
}
n=rowCounter;
m=n*n;
InitializeGraph();
if ( filePointer1!=NULL ) // if input file is present in same folder
{
rowCounter =0;
while (fgets(line,100,filePointer1) != NULL) // repeatedly read a line till the end of file has been reached
{
printf(line);
printf("\n");
result=strtok(line,delims);
j=0;
while( result != NULL ) {
test = atoi(result);
test=(float)test;
result = strtok( NULL, delims );
InsertEdge( j, rowCounter, test);
j++;
}
rowCounter++;
}
fclose(filePointer1); // close file pointer after reading entire file
}
/****************************************************************************************************/
/****************************************************************************************************/
/****************************************************************************************************/
cl1 = clock ();
InitializeDS();
A = MST_Kruskal();
cl2 = clock ();
print_MST( A );
printf("Total time: %.1f sec\n", (float)(cl2-cl1)/(float) CLOCKS_PER_SEC);
}
I NEED SOME HELP OVER WHY THIS PROGRAM DOES NOT ACCEPT MY TEXT FILE I DO NOT EXACTLY HOW FSCANF IS WORKING PLEASE CAN ANYONE HELP ME ON THIS THANKS
|