Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Question on reading and formating file

Posted on 2004-09-27
Medium Priority
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++)

// 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;
Question by:DancingFighterG
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
LVL 45

Expert Comment

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.

Author Comment

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
LVL 45

Accepted Solution

sunnycoder earned 200 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

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

636 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