I want to read from a text file and show some output
Input file format:
0 0 3
0 1 2
1 3 4
2 1 4
3 2 3
3 1 2
4 3 4
1st digit of each line indicates a particular day (day 0 to 4) and the second and the 3rd digit of each line indicates actors who sends message to each other on that day. I have written the following c code to display the participating actor on each day:
My sample code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define B 5 // number of days
#define A 5 // number of total actors
main()
{
FILE *fp;
int end = 0;
int day1, day, i, j, k1, k2;
int esin[A][A];
for (i = 0; i < A; i++) // initializing array
{
for (j = 0; j < A; j++)
{
esin[i][j] = 0;
}
}
fp = fopen("test.txt", "r"); // the file contains the input
if (fp == NULL)
{
printf("\nError - Cannot open the designated file\n");
}
while (end == 0)
{
fscanf(fp, "%d", &day);
day1 = day; // day1 for comparing
while (day1 == day)
{
if (feof(fp))
{
end = 1;
break;
}
fscanf(fp, "%d %d", &k1, &k2);
esin[k1][k2] = 1;// edge creation
esin[k2][k1] = 1;// edge creation
fscanf(fp, "%d", &day);
}
printf("\nday: %d", day1); // for printing output of a particular day
for (i = 0; i < A; i++)
{
for (j = 0; j < A; j++)
{
if (esin[i][j] == 0)
continue;
else
printf("\n%d and %d have an edge", i, j);
}
}
for (i = 0; i < A; i++)
{
for (j = 0; j < A; j++)
{
esin[i][j] = 0;
}
}
}
}
But I am not getting the actual output. For example, I want to get an output like:
day 0
0 and 3 have an edge
1 and 2 have an edge
day 1
3 and 4 have an edge
day 2
1 and 4 have an edge
day 3
2 and 3 have an edge
1 and 2 have an edge
day 4
3 and 4 have an edge
But I am not getting this. I am getting:
day 0
0 and 3 have an edge
1 and 2 have an edge
day 3
4 and 2 have an edge
day 4
3 and 2 have an edge
day 3
1 and 2 have an edge
day 3
4 and 2 have an edge
Is there anything wrong in the above code? Which correction will I need to make to get the above like output?