I have a text file with a lots of records (about 20k lines) that is written like this:
00000000090020120200728012
00000010520020120200729012
00000002740020120200729012
00000002710020120200736012
00000001601020120200755012
00000002870020120200758012
00000010690020120200753013
00000001760020120200800013
00000008480020120200800013
00000009370020120200733014
00000001500020120200739014
00000012400020120200743014
00000008720020120200715015
00009100570020120200734017
00000002060020120200734017
00000002050020120200734017
00000003670020120200734017
these records contain data information of accesses of office in year 2020, and every record can be readed with this structure (im gonna take the first line as example):
reading a string character by character, the record is splitted in this way:
- 0000 (index [0-3] -> useless data)
- 000009 (index [4-9] -> Badge ID)
- 0 (index [10] -> acts like boolean, 0 for in - access - and 1 for out - exit -)
- 02012020 (index [11-18] -> date format)
- 0728 (index [19-23] -> hours and minutes of the access)
- 012 (index [24-26] -> just an information about the gate ID)
i have this code that i wrote for counting the records for a specified badge ID on a specified gate (in my case i need to read only 001, 002, 003 gates):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define NUMLEN 27
int main () {
printf("\n-- 32bit version\n");
int entriesCounter = 0;
char buff[NUMLEN];
char requiredBadge[7];
char badge[7];
char entry[2];
char day[3];
char month[3];
char year[5];
char hours[3];
char minutes[3];
char gate[4];
FILE *fp;
fp = fopen("Storico2020.txt", "r");
if (fp == NULL) {
printf("Error open");
exit(1);
}
printf("\nInsert ID badge for counting accesses: ");
scanf("%s", requiredBadge);
while(!feof(fp)) {
fgets(buff, NUMLEN, fp); // example -> init:0000 | badge:000352 | entry:1 | data:01012019 | time:0030 | gate:023
strncpy(badge, buff+4, 6);
badge[6] = '\0';
strncpy(entry, buff+10, 1);
entry[1] = '\0';
strncpy(day, buff+11, 2);
day[2] = '\0';
strncpy(month, buff+13, 2);
month[2] = '\0';
strncpy(year, buff+15, 4);
year[4] = '\0';
strncpy(hours, buff+19, 2);
hours[2] = '\0';
strncpy(minutes, buff+21, 2);
minutes[2] = '\0';
strncpy(gate, buff+23, 3);
gate[3] = '\0';
if (strcmp(requiredBadge, badge) == 0 && strcmp(entry, "1") == 0) {
printf("\nBadge: %s | in date: %s/%s/%s | gate: %s | hour: %s:%s", badge, day, month, year, gate, hours, minutes);
entriesCounter++;
}
}
fclose(fp);
printf("\n********** TOTAL ACCESSES OF BADGE ID %s: %d ***************\n" ,requiredBadge, entriesCounter);
system("PAUSE");
return 0;
}
but, the problem here is that it counts every records that finds TWICE, and i really don't know why. i put in the if condition also the entry == 1 because i need to count once the entrance in the office, not also the exit. can you help me? for example, if u count ID badge 002341 it counts 342 accesses, but it need to count only the half. help me please!