Solved

Question on reading and formating file

Posted on 2004-09-27
3
358 Views
Last Modified: 2010-05-19
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,&edges[ctr1].weight);
      }
}

// 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,edges[ctr1].vertex2,edges[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;ctr1++)
      {
            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*,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])
      {
   Link(set,edges[ctr1].vertex1,edges[ctr1].vertex2);
   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,*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) .");

    scanf("%d%d",&nVertices,&nEdges);

      // 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;

      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,edgesOfTree,set);

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

      display_Edges(edgesOfTree);

      fclose(foutput);

      return 0;
}
0
Comment
Question by:DancingFighterG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12158333
#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.
0
 

Author Comment

by:DancingFighterG
ID: 12162181
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
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 12166628
fp is of type FILE *.
Again it seems that you are using C++ and not C.

Please post your C++ questions here
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question