Originally Posted by
Adak
I've got some running around to do, but I'll post something up, tonight.
Unfortunately, I strained my elbow and can't keyboard further now. This is what I have done, so far. File for data is at bottom of program. filename is log1.txt
Always should show last log entries, no matter how many there are. Log enyties still need tobe written out to file. only write out rowN entries from log arrasy
i was going to Windowize it for you, but we'll see.
Code:
#include <stdio.h>
#include <conio.h>
#define ROW 6
#define COL 100
int loadIt(char log[ROW][COL], FILE *);
int menu(char log[ROW][COL], int *);
void removeIt(char log[ROW][COL], int entryNum);
void showIt(char log[ROW][COL], int*);
int main() {
FILE * fp;
int i, n, rowN;
char log[ROW][COL] = { {'\0'}};
rowN = loadIt(log, fp);
menu(log, &rowN);
printf("\n\n\t\t\t press enter when ready");
i = getchar(); ++i;
return 0;
}
/* All loading entries go into ROW-1, first. Then they
are popped up, into the lower indexes of the array.
This allows the array to be large or small, and the
data to be large or small, it doesn't matter. This
array will have the last ROW log entries.
*/
int loadIt(char log[ROW][COL], FILE* fp) {
int r, j, i;
fp = fopen("log1.txt", "rt");
r = 0;
while(fgets(log[ROW-1], COL-1, fp)) {
++r;
for(i = 0; i < ROW; i++) { //pop all entries, 1 row
strcpy(log[i], log[i+1]);
log[i+1][0] = '\0';
}
}
fclose(fp);
return r;
}
int menu(char log[ROW][COL], int *rowN) {
int choice, entryNum;
char user[5] = { '\0' };
do {
clrscr();
//getchar();
gotoxy(2,1);
printf("\n\t\t\t Main Menu \n");
printf("\n 1: View or Remove an Entry ");
printf("\n 2: Add an Entry (?)");
printf("\n\n Q to Quit");
printf("\n\n Choose [1/2/q]: ");
choice = 0;
fgets(user, 3, stdin);
choice = atoi(user);
if((user[0] == 'q') || (user[0] == 'Q')) break;
showIt(log, rowN);
if(rowN)
printf("\n\n Remove which entry [0 for none] ?: ");
else
printf("\n\n There are no entries that can be removed");
scanf("%d", &entryNum);
getchar();
if((entryNum < ROW) && (entryNum > 0)) {
removeIt(log, entryNum);
(*rowN)--;
showIt(log, rowN);
}
}while(user[0] != 'q');
return 0;
}
/* From the entryNum on up, bring all the entries down 1 row,
overwriting the deleted log entry
*/
void removeIt(char log[ROW][COL], int entryNum) {
int i;
--entryNum; //for zero based arrays
for(i = entryNum; i < ROW; i++) {
strcpy(log[i], log[i+1]);
if(log[i+i] == "")
break;
}
}
void showIt(char log[ROW][COL], int *rowN) {
int r;
printf("\n ********* Log Entries You May Delete ********** \n\n");
for(r = 0; r < *rowN-1; r++) { //-1 because the first entry is
printf("%d\) %s", r+1, log[r]); //always popped off the array
if(r % 15 == 0 && r) //during loadIt().
getchar();
}
}
/*
Contents of file named log1.txt:
192.168.1.1\t0123812123712871181\t21:15 \t 22:00
122.54.3.1\t 09834823309092340996\t11:25 \t 12:00
192.168.1.1\t0123812123712871181\t21:15\t22:00
122.54.3.1\t09834823309092340996\t11:25\t12:00
172.152.12.2\t0123129239843294239181\t9:26\t10:00
111.24.2.2\t098783498341236224\t1:25\t2:00
*/
i could barely test any of it.