DancingFighterG
asked on
Question on reading and formating file
Hello, I want to modify my program to get the inputs that I ask the user from a file rather than the user. Based off my setup how would I format the data and what things would I have to change in my program to make it read all input in the line. Right now I have the format of the data like this:
1 2 3
4 5 6
7 8 9
The first column represents node1, column is node2 and the last row the weight of the edge. Here is my program:
# include <stdio.h>
# include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
void Make_set(int set[]);
int Find_set(int set[],int x);
void Link(int set[],int x1,int x2);
void Display_set(int set[]);
// This makes the set of variables in the array
void Make_set(int set[])
{
int ctr1;
for(ctr1 = 1;ctr1 <= set[0];ctr1++)
set[ctr1]=ctr1;
};
// This finds the set of variables in the array
int Find_set(int set[],int x)
{
return set[x];
}
// This associates the nodes with the edges
void Link(int set[],int x1,int x2)
{
int ctr1,ctr2,prevVal;
if(set[x1] == set[x2])
return ;
if(set[x1] < set[x2])
{
prevVal = set[x2];
set[x2] = set[x1];
for(ctr1 = 1; ctr1 <= set[0]; ctr1++)
if(set[ctr1] == prevVal)
set[ctr1] = set[x2];
return ;
}
if(set[x2] < set[x1])
{prevVal = set[x1];
set[x1] = set[x2];
for(ctr1 = 1; ctr1 <= set[0];ctr1++)
if(set[ctr1] == prevVal) set[ctr1] = set[x1];
return ;
}
}
// Struct for vertexs and weights
struct EdgeOfGraph {
int vertex1,vertex2;
int weight;
};
typedef EdgeOfGraph edge;
void get_Edges(edge *);
void display_Edges(edge *);
void sort_Edges(edge edges[]);
FILE *foutput;
// Gets the edges,vertex's, and weight from the user
void get_Edges(edge *edges)
{
int ctr1;
for(ctr1 = 1; ctr1 <= edges[0].vertex1; ctr1++)
{
printf("\nEnter in the following: Starting Vertex , Ending Vertex , Weight of the Edge.\n");
scanf("%d%d%d",&edges[ctr1 ].vertex1, &edges[ctr1].vertex2,&edge s[ctr1].we ight);
}
}
// Prints out the edges, vertexs and weights to a file
void display_Edges(edge *edges)
{
int ctr1;
printf("\n");
for(ctr1 = 1; ctr1 <= edges[0].vertex1;ctr1++)
fprintf(foutput,"[ Vertex1: %d , Vertex2: %d , Weight: %d ]\n",edges[ctr1].vertex1,e dges[ctr1] .vertex2,e dges[ctr1] .weight);
fprintf(foutput,"\n");
}
// Sorts the edges and decides
void sort_Edges(edge edges[])
{
int ctr1,ctr2,ctr3;
edge temp;
for(ctr1=2;ctr1<=edges[0]. weight;ctr 1++)
{
temp = edges[ctr1];
ctr2 = ctr1;
while(ctr2>1 && temp.weight<edges[ctr2-1]. weight)
{
edges[ctr2]=edges[ctr2-1];
ctr2--;
};
if(ctr2!=ctr1)
edges[ctr2]=temp;
}
}
void implementKruskal(edge*,edg e*,int set[]);
// Implemenation of Kruskal
void implementKruskal(edge edges[],edge edgesOfTree[],int set[])
{
int ctr1,ctr2=1;
for(ctr1 = 1; ctr1 <= edges[0].weight; ctr1++)
{
if(set[edges[ctr1].vertex1 ] != set[edges[ctr1].vertex2])
{
Link(set,edges[ctr1].verte x1,edges[c tr1].verte x2);
edgesOfTree[ctr2++]=edges[ ctr1];
};
}
edgesOfTree[0].vertex1 = edgesOfTree[0].vertex2 = edgesOfTree[0].weight = ctr2-1;
}
int main()
{
edge *edges,*edgesOfTree;
int v1,v2,nEdges,nVertices,*se t;
foutput = fopen("kruskalalgo.txt","a ");
// Ask the user for the amount of vertices and edges
cout << "\nALL OUTPUT WILL BE DIRECTED TO THE FILE kruskalalgo.txt .\n";
printf("\nEnter the Number of Vertices , Edges . (Vertices are from 1.....n) .");
scanf("%d%d",&nVertices,&n Edges);
// prints the values to a file
fprintf(foutput,"\n No of Edges=%d ,No of Vertices=%d.\n",nEdges,nVe rtices);
set = (int *)malloc(sizeof(int) * (nVertices+1));
edges = (edge *)malloc(sizeof(edge) * (nEdges+1));
edgesOfTree = (edge *)malloc(sizeof(edge) * (nEdges+1));
edges[0].vertex1 = edges[0].vertex2 = edges[0].weight = nEdges;
edgesOfTree[0].vertex1 = edgesOfTree[0].vertex2 = edgesOfTree[0].weight = nEdges;
get_Edges(edges);
sort_Edges(edges);
fprintf(foutput,"The Edges Entered ( in Ascending Order of Weights ) :\n");
display_Edges(edges);
set[0] = nVertices;
Make_set(set);
implementKruskal(edges,edg esOfTree,s et);
fprintf(foutput,"\nEdges Included in the Minimum Spanning Tree are:\n");
display_Edges(edgesOfTree) ;
fclose(foutput);
return 0;
}
1 2 3
4 5 6
7 8 9
The first column represents node1, column is node2 and the last row the weight of the edge. Here is my program:
# include <stdio.h>
# include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
void Make_set(int set[]);
int Find_set(int set[],int x);
void Link(int set[],int x1,int x2);
void Display_set(int set[]);
// This makes the set of variables in the array
void Make_set(int set[])
{
int ctr1;
for(ctr1 = 1;ctr1 <= set[0];ctr1++)
set[ctr1]=ctr1;
};
// This finds the set of variables in the array
int Find_set(int set[],int x)
{
return set[x];
}
// This associates the nodes with the edges
void Link(int set[],int x1,int x2)
{
int ctr1,ctr2,prevVal;
if(set[x1] == set[x2])
return ;
if(set[x1] < set[x2])
{
prevVal = set[x2];
set[x2] = set[x1];
for(ctr1 = 1; ctr1 <= set[0]; ctr1++)
if(set[ctr1] == prevVal)
set[ctr1] = set[x2];
return ;
}
if(set[x2] < set[x1])
{prevVal = set[x1];
set[x1] = set[x2];
for(ctr1 = 1; ctr1 <= set[0];ctr1++)
if(set[ctr1] == prevVal) set[ctr1] = set[x1];
return ;
}
}
// Struct for vertexs and weights
struct EdgeOfGraph {
int vertex1,vertex2;
int weight;
};
typedef EdgeOfGraph edge;
void get_Edges(edge *);
void display_Edges(edge *);
void sort_Edges(edge edges[]);
FILE *foutput;
// Gets the edges,vertex's, and weight from the user
void get_Edges(edge *edges)
{
int ctr1;
for(ctr1 = 1; ctr1 <= edges[0].vertex1; ctr1++)
{
printf("\nEnter in the following: Starting Vertex , Ending Vertex , Weight of the Edge.\n");
scanf("%d%d%d",&edges[ctr1
}
}
// Prints out the edges, vertexs and weights to a file
void display_Edges(edge *edges)
{
int ctr1;
printf("\n");
for(ctr1 = 1; ctr1 <= edges[0].vertex1;ctr1++)
fprintf(foutput,"[ Vertex1: %d , Vertex2: %d , Weight: %d ]\n",edges[ctr1].vertex1,e
fprintf(foutput,"\n");
}
// Sorts the edges and decides
void sort_Edges(edge edges[])
{
int ctr1,ctr2,ctr3;
edge temp;
for(ctr1=2;ctr1<=edges[0].
{
temp = edges[ctr1];
ctr2 = ctr1;
while(ctr2>1 && temp.weight<edges[ctr2-1].
{
edges[ctr2]=edges[ctr2-1];
ctr2--;
};
if(ctr2!=ctr1)
edges[ctr2]=temp;
}
}
void implementKruskal(edge*,edg
// Implemenation of Kruskal
void implementKruskal(edge edges[],edge edgesOfTree[],int set[])
{
int ctr1,ctr2=1;
for(ctr1 = 1; ctr1 <= edges[0].weight; ctr1++)
{
if(set[edges[ctr1].vertex1
{
Link(set,edges[ctr1].verte
edgesOfTree[ctr2++]=edges[
};
}
edgesOfTree[0].vertex1 = edgesOfTree[0].vertex2 = edgesOfTree[0].weight = ctr2-1;
}
int main()
{
edge *edges,*edgesOfTree;
int v1,v2,nEdges,nVertices,*se
foutput = fopen("kruskalalgo.txt","a
// Ask the user for the amount of vertices and edges
cout << "\nALL OUTPUT WILL BE DIRECTED TO THE FILE kruskalalgo.txt .\n";
printf("\nEnter the Number of Vertices , Edges . (Vertices are from 1.....n) .");
scanf("%d%d",&nVertices,&n
// prints the values to a file
fprintf(foutput,"\n No of Edges=%d ,No of Vertices=%d.\n",nEdges,nVe
set = (int *)malloc(sizeof(int) * (nVertices+1));
edges = (edge *)malloc(sizeof(edge) * (nEdges+1));
edgesOfTree = (edge *)malloc(sizeof(edge) * (nEdges+1));
edges[0].vertex1 = edges[0].vertex2 = edges[0].weight = nEdges;
edgesOfTree[0].vertex1 = edgesOfTree[0].vertex2 = edgesOfTree[0].weight = nEdges;
get_Edges(edges);
sort_Edges(edges);
fprintf(foutput,"The Edges Entered ( in Ascending Order of Weights ) :\n");
display_Edges(edges);
set[0] = nVertices;
Make_set(set);
implementKruskal(edges,edg
fprintf(foutput,"\nEdges Included in the Minimum Spanning Tree are:\n");
display_Edges(edgesOfTree)
fclose(foutput);
return 0;
}
ASKER
Question, For some reason I'm getting an error on the:
fp = fopen("input_file", "r");
It's giving me the following errors:
\\excelsior\ug$\grichard\P rogramsand Classes\CS 472\HW2\Kr uskal.cpp( 14) : error C2501: 'fp' : missing storage-class or type specifiers
\\excelsior\ug$\grichard\P rogramsand Classes\CS 472\HW2\Kr uskal.cpp( 14) : error C2040: 'fp' : 'int' differs in levels of indirection from 'struct _iobuf *'
\\excelsior\ug$\grichard\P rogramsand Classes\CS 472\HW2\Kr uskal.cpp( 14) : error C2440: 'initializing' : cannot convert from 'struct _iobuf *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
fp = fopen("input_file", "r");
It's giving me the following errors:
\\excelsior\ug$\grichard\P
\\excelsior\ug$\grichard\P
\\excelsior\ug$\grichard\P
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
FILE * fp;
int num1, num2, num3;
char buffer [MAX_BUF];
fp = fopen ( "input_file", "r" );
while ( fgets (buffer, MAX_BUF, fp) != NULL )
{
sscanf ( buffer, "%d %d %d", &num1, &num2, &num3 );
...
}
This will read one line at a time and put the 3 values in num1, num2 and num3 respectively. Use these values in the loop. Successive iterations will overwrite the previous values.