• C

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++)

// 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,&edges[ctr1].weight);

// Prints out the edges, vertexs and weights to a file
void display_Edges(edge *edges)
      int ctr1;
      for(ctr1 = 1; ctr1 <= edges[0].vertex1;ctr1++)
      fprintf(foutput,"[ Vertex1: %d , Vertex2: %d , Weight: %d ]\n",edges[ctr1].vertex1,edges[ctr1].vertex2,edges[ctr1].weight);

// Sorts the edges and decides
void sort_Edges(edge edges[])
      int ctr1,ctr2,ctr3;
      edge temp;
            temp = edges[ctr1];
            ctr2 = ctr1;
            while(ctr2>1 && temp.weight<edges[ctr2-1].weight)

void implementKruskal(edge*,edge*,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])
  edgesOfTree[0].vertex1 = edgesOfTree[0].vertex2 = edgesOfTree[0].weight = ctr2-1;

int main()


      edge *edges,*edgesOfTree;

      int v1,v2,nEdges,nVertices,*set;

      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) .");


      // prints the values to a file
      fprintf(foutput,"\n No of Edges=%d ,No of Vertices=%d.\n",nEdges,nVertices);

      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;



      fprintf(foutput,"The Edges Entered ( in Ascending Order of Weights ) :\n");


      set[0] = nVertices;



      fprintf(foutput,"\nEdges Included in the Minimum Spanning Tree are:\n");



      return 0;
Who is Participating?
sunnycoderConnect With a Mentor Commented:
fp is of type FILE *.
Again it seems that you are using C++ and not C.

Please post your C++ questions here
#define MAX_BUF 64
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.
DancingFighterGAuthor Commented:
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\ProgramsandClasses\CS472\HW2\Kruskal.cpp(14) : error C2501: 'fp' : missing storage-class or type specifiers
\\excelsior\ug$\grichard\ProgramsandClasses\CS472\HW2\Kruskal.cpp(14) : error C2040: 'fp' : 'int' differs in levels of indirection from 'struct _iobuf *'
\\excelsior\ug$\grichard\ProgramsandClasses\CS472\HW2\Kruskal.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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.