I have 10 text files with some packets which are represented by rollnumber, source, destination and generation time, those are also in a struct :
typedef struct Node
{
int rollnumber, src, dst;
double gentime;
struct Node *next;
} Node;
I want to open each file in every time unit and be able to check if any packet was generated. This means, that the generation time must be less than the Time unit I am in. For example, in the time unit 0-1, I must find a packet with 0<generation time<1. So, if this happens, that packet enters a list with the insert function:
void insert_end ( Node **head, int rollnumber, int src, int dst, double gentime){
struct Node * new_node = NULL;
struct Node * last = NULL;
new_node = (struct Node *)malloc(sizeof(struct Node));
if (new_node == NULL)
{
printf("Failed to insert element. Out of memory");
return;
}
new_node->rollnumber=rollnumber;
new_node->src = src;
new_node->dst=dst;
new_node->gentime=gentime;
new_node->next = NULL;
if( *head == NULL)
{
*head = new_node;
return;
}
last = *head;
while(last->next) last = last->next;
last->next = new_node;
}
My code is below:
for (Time=1.0; Time<10.0; Time=Time+1.0){ //the time units checking them per one like: 0-1,1-2 etc..
for(i=1;i<=10;i++){ //because I have 10 text files
char to_open[32];
snprintf(to_open,32, "fptg_%d.txt", i);
printf("\n\nFPTG_%d.txt\n", i);
if ((file = fopen(to_open, "r")) == NULL)
{
break;
}else{
fseek(file , pos[i], SEEK_CUR);
fgets(line, sizeof(line), file);
sscanf(line,"%d %d %d %lf",&rollnumber, &src, &dst, &gentime);
printf("%s", line);
printf("Return value=%d\n",sscanf(line, " %d %d %d %lf", &rollnumber, &src, &dst, &gentime));
printf("gentime=%.1f\n", gentime);
pos[i] = ftell(file);
if(Time<gentime && gentime<Time+1.0){
insert_end ( &link[i], rollnumber, src, dst, gentime );
printf("Time=%0.1f\n", Time);
}else{
//do something else here and in the next time unit check the same packet again
}
}
}
}
}
My question is how will I be able if a packet does not insert in to the list to check the same packet in the next time unit? If a packet is inserting in the list, reading the next one is correct for what I want to do. But if a packet does not insert in to the list I do not want to go to the next one in the next time unit. Any help will be appreciated, thanks in advance!
Minimal Reproducible Example:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#define MAX_LINE_LENGTH 105
typedef struct Node {
int rollnumber, src, dst;
double gentime;
struct Node *next;
} Node;
void
insert_end(Node **head, int rollnumber, int src, int dst, double gentime)
{
struct Node *new_node = NULL;
struct Node *last = NULL;
new_node = (struct Node *) malloc(sizeof(struct Node));
if (new_node == NULL) {
printf("Failed to insert element. Out of memory");
return;
}
new_node->rollnumber = rollnumber;
new_node->src = src;
new_node->dst = dst;
new_node->gentime = gentime;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
}
last = *head;
while (last->next)
last = last->next;
last->next = new_node;
}
void
output(Node *head)
{
for (Node *current = head; current != NULL; current = current->next) {
// printf("%d ", current->data);
printf("Roll Number:%2d\t", current->rollnumber);
printf("src:%2d\t", current->src);
printf("dest:%2d\t", current->dst);
printf("gentime:%0.1f\n", current->gentime);
}
}
void
display(Node **set, int i)
{
output(set[i]);
putchar('\n');
}
int
remove_node_in_list(Node **set, size_t pos)
{
int success = set[pos] != NULL;
if (success) {
Node *tmp = set[pos];
set[pos] = set[pos]->next;
free(tmp);
}
return success;
}
#define N 10
int
main(void)
{
char line[MAX_LINE_LENGTH] = { 0 };
int src, dst;
int rollnumber;
double gentime;
int stations = 0;
Node *link[N] = { 0 };
int i = 1;
static unsigned long pos[] = { 0 };
FILE *file;
char filename_format[] = "fptg_%d.txt";
char filename[sizeof(filename_format) + 4];
bool intention[10];
double Time = 12.0;
int lower = 0, upper = 9, count = 1;
srand(time(0));
int num;
int k = 1;
struct node *head = NULL;
// the time units checking them per one like: 0-1,1-2 etc..
for (Time = 1.0; Time < 50.0; Time = Time + 1.0) {
// because I have 10 text files
for (i = 1; i <= 10; i++) {
char to_open[32];
snprintf(to_open, 32, "fptg_%d.txt", i);
printf("\n\nFPTG_%d.txt\n", i);
if ((file = fopen(to_open, "r")) == NULL) {
break;
}
else {
fseek(file, pos[i], SEEK_CUR);
fgets(line, sizeof(line), file);
sscanf(line, "%d %d %d %lf", &rollnumber, &src, &dst, &gentime);
printf("%s", line);
printf("Return value=%d\n", sscanf(line, " %d %d %d %lf", &rollnumber, &src, &dst, &gentime));
printf("gentime=%.1f\n", gentime);
pos[i] = ftell(file);
if (Time < gentime && gentime < Time + 1.0) {
insert_end(&link[i], rollnumber, src, dst, gentime);
printf("Time=%0.1f\n", Time);
for (int j = 0; j < count; j++) {
int num = (rand() % (upper - lower + 1)) + lower;
printf("Random number:%d\n", num);
if (num == 1 || num == 6 || num == 8) {
intention[i] = true;
printf("ok\n");
stations++;
printf("stations=%d\n", stations);
printf("intention[%d]=%d\n", i, intention[i]);
double offtime = gentime + 12.0;
printf("channel off until: %.1f starting from: %.1f\n\n", offtime, gentime);
}
else {
intention[i] = false;
printf("intention[%d]=%d\n", i, intention[i]);
}
}
}
else {
printf("Not in the list\n");
}
}
}
}
if (stations == 1) {
for (int i = 1; i <= 10; i++) {
printf("intention[%d]=%d\n", i, intention[i]);
if (intention[i] == true) {
printf("link[%d]:\n", i);
display(link, i);
printf("i=%d\n", i);
remove_node_in_list(link, i);
printf("NEW:\n");
display(link, i);
}
}
}
stations = 0;
return 0;
}
The result I get for the first &second time unit is:
FPTG_1.txt
1 1 3 1.6
Return value=4
gentime=1.6
ok
Time=1.0
Random number:2
intention[1]=0
FPTG_2.txt
1 2 4 1.9
Return value=4
gentime=1.9
ok
Time=1.0
Random number:8
ok
stations=1
intention[2]=1
channel off until: 13.9 starting from: 1.9
FPTG_3.txt
1 3 7 1.2
Return value=4
gentime=1.2
ok
Time=1.0
Random number:7
intention[3]=0
FPTG_4.txt
1 4 18 0.2
Return value=4
gentime=0.2
Random number:9
intention[4]=0
FPTG_5.txt
1 5 19 0.2
Return value=4
gentime=0.2
Random number:0
intention[5]=0
FPTG_6.txt
1 6 3 0.1
Return value=4
gentime=0.1
Random number:0
intention[6]=0
FPTG_7.txt
1 7 6 0.0
Return value=4
gentime=0.0
Random number:0
intention[7]=0
FPTG_8.txt
1 8 17 0.5
Return value=4
gentime=0.5
Random number:4
intention[8]=0
FPTG_9.txt
1 9 6 0.1
Return value=4
gentime=0.1
Random number:1
ok
stations=2
intention[9]=1
channel off until: 12.1 starting from: 0.1
FPTG_10.txt
1 10 7 0.1
Return value=4
gentime=0.1
Random number:4
intention[10]=0
FPTG_1.txt
2 1 15 13.9
Return value=4
gentime=13.9
Random number:1
ok
stations=1
intention[1]=1
channel off until: 25.9 starting from: 13.9
FPTG_2.txt
2 2 19 14.0
Return value=4
gentime=14.0
Random number:6
ok
stations=2
intention[2]=1
channel off until: 26.0 starting from: 14.0
FPTG_3.txt
2 3 18 13.4
Return value=4
gentime=13.4
Random number:8
ok
stations=3
intention[3]=1
channel off until: 25.4 starting from: 13.4
FPTG_4.txt
2 4 12 12.8
Return value=4
gentime=12.8
Random number:8
ok
stations=4
intention[4]=1
channel off until: 24.8 starting from: 12.8
FPTG_5.txt
2 5 4 12.3
Return value=4
gentime=12.3
Random number:0
intention[5]=0
FPTG_6.txt
2 6 11 13.1
Return value=4
gentime=13.1
Random number:1
ok
stations=5
intention[6]=1
channel off until: 25.1 starting from: 13.1
FPTG_7.txt
2 7 13 12.8
Return value=4
gentime=12.8
Random number:2
intention[7]=0
FPTG_8.txt
2 8 14 13.5
Return value=4
gentime=13.5
Random number:4
intention[8]=0
FPTG_9.txt
2 9 11 14.0
Return value=4
gentime=14.0
Random number:0
intention[9]=0
FPTG_10.txt
2 10 9 12.1
Return value=4
gentime=12.1
Random number:7
intention[10]=0
and so it goes for the next time units. For example for the fptg_4.txt, in the first time unit it checks the first line of it: 1 4 18 0.2 but in the next time unit it goes to the next line: 2 4 12 12.8, even though it should have checked the same line because that packet represented by the previous line did not enter the list. So, my question is how is this possible?