我有 10 個文本檔案,其中包含一些由 rollnumber、源、目標和生成時間表示的資料包,它們也在 struct 中:
typedef struct Node
{
int rollnumber, src, dst;
double gentime;
struct Node *next;
} Node;
我想在每個時間單位打開每個檔案并能夠檢查是否生成了任何資料包。這意味著,生成時間必須小于我所在的時間單位。例如,在時間單位0-1中,我必須找到一個0<生成時間<1的資料包。因此,如果發生這種情況,該資料包將使用插入函式進入一個串列:
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;
}
我的代碼如下:
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:-\t", current->rollnumber);
printf("src:-\t", current->src);
printf("dest:-\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?
uj5u.com熱心網友回復:
從我的熱門評論...
由于額外的 } [修復了我的編輯],您的 MRE 無法編譯。
static unsigned long pos[] = { 0 };是錯誤的(它具有 UB--未定義的行為,因為它太短了)。它應該是:static unsigned long pos[11] = { 0 };您從 1 開始對陣列進行索引。這意味著永遠不會使用第一個陣列元素,并且所有陣列都必須更大(例如
int array[11];,int array[10];您可能最好使用:for (i = 0; i < 10; i )然后:snprintf(to_open, 32, "fptg_%d.txt", i 1);使用 fseek/ftell 是有問題的,因為輸入檔案是可變長度的文本。你想做什么?我能想到的唯一想法是你試圖讓你的串列按時間排序?
即使未存盤記錄,您也始終設定
pos[i]為該值。ftell將線移動到pos[i] = ftell(file);線的下方/之后if (Time < gentime && gentime < Time 1.0) {。(即在通話上方insert_end)完成最后一次更改后,位置問題得到解決。我得到 3479 行,而不是 192 行輸出。
但是,老實說,雖然我不確定您嘗試使用
intention/stations代碼獲得什么效果,但多次重讀檔案并不是最好的方法。我會讀取每個檔案一次,如果(例如)將其添加到串列中
(gentime >= 1.0) && (gentime <= 50.0),然后在讀取所有檔案后,根據存盤的 gentime 對鏈接串列進行排序。似乎意圖代碼可以在讀取回圈之外/之后完成,因為它們不依賴于彼此的資料。
其他錯誤:
- 您永遠不會這樣做
fclose,因此您有大量懸空的檔案流指標。 - 您從不檢查 的回傳值
fgets,因此您沒有正確處理 EOF。 - 你打
sscanf了兩次電話。一次解碼該行,另一個只是列印回傳值sscanf - 不要強制轉換的回傳值
malloc。請參閱:我是否強制轉換 malloc 的結果?
這是修改/更正的代碼:
#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:-\t", current->rollnumber);
printf("src:-\t", current->src);
printf("dest:-\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[N] = { 0 };
FILE *file;
bool intention[N];
double Time = 12.0;
int lower = 0,
upper = 9,
count = 1;
srand(time(0));
// 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 N text files
for (i = 0; i < N; i ) {
char to_open[32];
snprintf(to_open, 32, "fptg_%d.txt", i 1);
printf("\n\nFILE/%d: %s Position:%ld (Time: %g)\n",
i,to_open,pos[i],Time);
if ((file = fopen(to_open, "r")) == NULL) {
perror(to_open);
break;
}
fseek(file, pos[i], SEEK_CUR);
#if 0
fgets(line, sizeof(line), file);
#else
char *cp = fgets(line, sizeof(line), file);
fclose(file);
if (cp == NULL)
continue;
#endif
int retval = sscanf(line, "%d %d %d %lf",
&rollnumber, &src, &dst, &gentime);
printf("%s", line);
printf("Return value=%d\n", retval);
printf("gentime=%.1f\n", gentime);
#if 0
pos[i] = ftell(file);
#endif
if (Time < gentime && gentime < Time 1.0) {
#if 1
pos[i] = ftell(file);
#endif
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 = 0; i < N; 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;
}
我看到并做了所有的改變,非常感謝你!您認為我應該在代碼中的任何位置使用倒帶嗎?那會有幫助嗎?– 瓦賈普
不rewind,[有效地]只是一個包裝fseek
正如我上面提到的,我會一次讀取所有檔案的所有行。然后,對串列進行排序。
#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;
struct Node *last = NULL;
new_node = malloc(sizeof(*new_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:-\t", current->rollnumber);
printf("src:-\t", current->src);
printf("dest:-\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;
}
int
sort_cmp(const void *vlhs,const void *vrhs)
{
const Node *lhs = vlhs;
const Node *rhs = vrhs;
int cmp;
do {
cmp = -1;
if (lhs->gentime < rhs->gentime)
break;
cmp = 1;
if (lhs->gentime > rhs->gentime)
break;
cmp = 0;
} while (0);
return cmp;
}
void
sort_list(Node **head)
{
size_t count;
size_t idx;
Node *cur;
Node *prev;
// get count of list
count = 0;
for (cur = *head; cur != NULL; cur = cur->next)
count;
// get flat array of node pointers
Node **arr = malloc(sizeof(*arr) * count);
// fill the array
idx = 0;
for (cur = *head; cur != NULL; cur = cur->next, idx)
arr[idx] = cur;
qsort(arr,count,sizeof(*arr),sort_cmp);
// repopulate linked list from array
prev = NULL;
for (idx = 0; idx < count; idx) {
cur = arr[idx];
cur->next = NULL;
if (prev != NULL)
prev->next = cur;
else
*head = cur;
}
free(arr);
}
#define N 10
#define TMIN 1.0
#define TMAX 50.0
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;
FILE *file;
char *cp;
bool intention[N] = { 0 };
double Time = 12.0;
int lower = 0,
upper = 9,
count = 1;
srand(time(0));
// because I have N text files
for (i = 0; i < N; i ) {
char to_open[32];
snprintf(to_open, 32, "fptg_%d.txt", i 1);
printf("\nFILE/%d: %s\n",i,to_open);
if ((file = fopen(to_open, "r")) == NULL) {
perror(to_open);
break;
}
while (1) {
cp = fgets(line, sizeof(line), file);
if (cp == NULL)
break;
int retval = sscanf(line, "%d %d %d %lf",
&rollnumber, &src, &dst, &gentime);
printf("%s", line);
printf("Return value=%d\n", retval);
printf("gentime=%.1f\n", gentime);
if ((gentime >= TMIN) && (gentime < TMAX))
insert_end(&link[i], rollnumber, src, dst, gentime);
else
printf("Not in the list\n");
}
fclose(file);
}
// sort all lists
for (i = 0; i < N; i )
sort_list(&link[i]);
// the time units checking them per one like: 0-1,1-2 etc..
for (Time = TMIN; Time < TMAX; Time = Time 1.0) {
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]);
}
}
}
if (stations == 1) {
for (int i = 0; i < N; 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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/417891.html
標籤:
上一篇:同心方陣
