![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
King of Portal
|
My LCD laptop screen messed up again, and this prompted me to find a solution to keeping track of the backups I've made of my computer. I'm a habitual reformatter and start over from scratch. Needless to say, I have quite a few backup CDs and DVDs of my data. So I've created a search engine for the files I have archived.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ENTRIES 1
int main (void)
{
int i;
int n;
int perform;
char search[80];
char ***database;
database = (char ***) calloc(ENTRIES, sizeof(char **));
for(i = 0; i < ENTRIES; i++)
{
database[i] = (char **) calloc(ENTRIES * 3, sizeof(char*));
}
n = 0;
// database[n][0] = (char *) calloc(strlen("\0"), sizeof(char));
// strcpy(database[n][0], "\0");
// database[n][1] = (char *) calloc(strlen("\0"), sizeof(char));
// strcpy(database[n][1], "\0");
// database[n][2] = (char *) calloc(strlen("\0"), sizeof(char));
// strcpy(database[n][2], "\0");
// n++;
/////////////////
// DATABASE //
////////////////
database[n][0] = (char *) calloc(strlen("Backup 01\0"), sizeof(char));
strcpy(database[n][0], "Backup01\0");
database[n][1] = (char *) calloc(strlen("Contains a backup of various things.\0"), sizeof(char));
strcpy(database[n][1], "Contains a backup of various things.\0");
database[n][2] = (char *) calloc(strlen("backup, applications, games, etc.\0"), sizeof(char));
strcpy(database[n][2], "backup, applications, games, etc.\0");
n++;
////////////////////////////////////////////////////////////////////////////////////////////
printf("Grim Pirate's Backup Archive Search Engine\n\n");
printf("COMMANDS");
printf("\n !quit - terminate program");
printf("\n !about - program info\n\n");
while(1)
{
perform = 1;
printf("Please enter your search terms\n-> ");
gets(search);
if(strstr(search, "!quit"))
{
break;
}
if(strstr(search, "!about"))
{
printf("\nAuthor: The Grim Pirate");
printf("\nHomepage: http://grimpirate.t35.com/");
printf("\nVersion: 2006.06.02\n\n");
perform = 0;
}
if(perform)
{
printf("\n--------------------------------------------------------------------------------");
printf(" RESULTS\n");
printf("--------------------------------------------------------------------------------");
for(i = 0; i < ENTRIES; i++)
{
if(strstr(database[i][2], search))
{
printf("Location: ");
puts(database[i][0]);
printf("Description: ");
puts(database[i][1]);
}
else
{
printf("NONE\n");
}
}
printf("--------------------------------------------------------------------------------\n");
}
}
return (0);
}
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Hmm. Intresting. Shouldn't this have been in the "Current Project Work" section of the forums?
__________________
The world's first athletic computer geek! The home of PrProgramsStudios How not to post a question: <-- Please don't reply |
|
|
|
|
|
#3 |
|
King of Portal
|
Works with text files now
I updated the search engine so that now it works with text files meaning that anyone can use it. Basically all you've gotta do is structure a text file with various entries as follows:
Location of file/folder Description of file/folder Keywords related to file/folder Location of file/folder Description of file/folder Keywords related to file/folder Location of file/folder Description of file/folder Keywords related to file/folder 1 - its location: I use a particular format of the following type [physical storage device] >> {folder} >> {folder} >> file 2 - its description: A useful sentence or so regarding what the actual file is for your own benefit. 3 - keywords: these are just various words (do not separate with commas) that are relevant to what the file is about. Here is a sample of what the text file would look like: [Backup 1] >> {Folder 1} >> file1.txt
This is a text file that contains some experimental data.
experimental data txt
[Backup 1] >> {Folder 1} >> file2.txt
This is a text file that contains more experimental data.
experimental data txtWarnings: Do not deviate from the 3 line format and do not type very long descriptions. Furthermore, the text file must be named database.txt and it must be ANSI encoded NO UNICODE allowed. Here is the source code: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
unsigned long i;
unsigned long ENTRIES;
unsigned long count;
int perform;
char search[80];
char ***database;
FILE *fp;
fp = fopen("database.txt", "r");
if(fp == NULL)
{
printf("Error opening file.\n");
return(0);
}
ENTRIES = 0;
do
{
fgets(search, 512, fp);
ENTRIES++;
}while(!feof(fp));
ENTRIES /= 3;
database = (char ***) calloc(ENTRIES, sizeof(char **));
for(i = 0; i < ENTRIES; i++)
{
database[i] = (char **) calloc(ENTRIES * 3, sizeof(char*));
}
fsetpos(fp, 0);
for(i = 0; i < ENTRIES; i++)
{
fgets(search, 512, fp);
database[i][0] = (char *) calloc(strlen(search) + 1, sizeof(char));
strncpy(database[i][0], search, strlen(search));
fgets(search, 512, fp);
database[i][1] = (char *) calloc(strlen(search) + 1, sizeof(char));
strncpy(database[i][1], search, strlen(search));
fgets(search, 512, fp);
database[i][2] = (char *) calloc(strlen(search) + 1, sizeof(char));
strncpy(database[i][2], search, strlen(search));
}
printf("Grim Pirate's Backup Archive Search Engine\n\n");
printf("COMMANDS");
printf("\n !help - displays the available commands");
printf("\n !quit - terminate program");
printf("\n !list - lists all entries in the database");
printf("\n !count - lists the total amount of entries in the database");
printf("\n !about - program info\n\n");
while(1)
{
perform = 1;
count = 0;
printf("Please enter your search terms\n-> ");
gets(search);
if(strstr(search, "!quit"))
{
break;
}
if(strstr(search, "!help"))
{
printf("\nCOMMANDS");
printf("\n !help - displays the available commands");
printf("\n !quit - terminate program");
printf("\n !list - lists all entries in the database");
printf("\n !count - lists the total amount of entries in the database");
printf("\n !about - program info\n\n");
perform = 0;
}
if(strstr(search, "!about"))
{
printf("\nAuthor: The Grim Pirate");
printf("\nHomepage: http://grimpirate.t35.com/");
printf("\nHistory: 2006.07.02 - Did not work with text files.");
printf("\nVersion: 2006.07.03\n\n");
perform = 0;
}
if(strstr(search, "!count"))
{
printf("\n%li entries in database.\n\n", ENTRIES);
perform = 0;
}
if(strstr(search, "!list"))
{
printf("\n--------------------------------------------------------------------------------");
printf(" RESULTS\n");
printf("--------------------------------------------------------------------------------\n");
for(i = 0; i < ENTRIES; i++)
{
printf("Location:\n");
printf("%s", database[i][0]);
printf("Description:\n");
printf("%s", database[i][1]);
printf("\n");
count++;
}
printf("--------------------------------------------------------------------------------");
printf("%li entries in database.\n\n", count);
perform = 0;
}
if(perform)
{
printf("\n--------------------------------------------------------------------------------");
printf(" RESULTS\n");
printf("--------------------------------------------------------------------------------\n");
for(i = 0; i < ENTRIES; i++)
{
if(strstr(database[i][2], search))
{
count++;
printf("ENTRY %74li", count);
printf("Location:\n");
printf("%s", database[i][0]);
printf("Description:\n");
printf("%s", database[i][1]);
printf("\n");
}
}
if(count == 0)
{
printf("NONE\n\n");
}
printf("--------------------------------------------------------------------------------\n");
}
}
return (0);
}
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#4 |
|
King of Portal
|
console output fix
Yeah I just realized while searching my own database that the console window doesn't have enough buffer room to display certain entries if the number of results gets too high. Therefore, I rewrote the program to output the results of the search to a file called results.txt which it automatically generates and overwrites if it doesn't exist. Everything else works the same.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
unsigned long i;
unsigned long ENTRIES;
unsigned long count;
int perform;
char search[80];
char ***database;
FILE *fp;
fp = fopen("database.txt", "r");
if(fp == NULL)
{
printf("Error opening file.\n");
return(0);
}
ENTRIES = 0;
do
{
fgets(search, 512, fp);
ENTRIES++;
}while(!feof(fp));
ENTRIES /= 3;
database = (char ***) calloc(ENTRIES, sizeof(char **));
for(i = 0; i < ENTRIES; i++)
{
database[i] = (char **) calloc(ENTRIES * 3, sizeof(char*));
}
rewind(fp);
for(i = 0; i < ENTRIES; i++)
{
fgets(search, 512, fp);
database[i][0] = (char *) calloc(strlen(search) + 1, sizeof(char));
strncpy(database[i][0], search, strlen(search));
fgets(search, 512, fp);
database[i][1] = (char *) calloc(strlen(search) + 1, sizeof(char));
strncpy(database[i][1], search, strlen(search));
fgets(search, 512, fp);
database[i][2] = (char *) calloc(strlen(search) + 1, sizeof(char));
strncpy(database[i][2], search, strlen(search));
}
fclose(fp);
printf("Grim Pirate's Backup Archive Search Engine\n\n");
printf("COMMANDS");
printf("\n !help - displays the available commands");
printf("\n !quit - terminate program");
printf("\n !count - lists the total amount of entries in the database");
printf("\n !about - program info\n\n");
while(1)
{
perform = 1;
count = 0;
printf("Please enter your search terms\n-> ");
gets(search);
if(strstr(search, "!quit"))
{
break;
}
if(strlen(search) == 0)
{
printf("\nYou did not enter anything to search for.\n\n");
perform = 0;
}
if(strstr(search, "!help"))
{
printf("\nCOMMANDS");
printf("\n !help - displays the available commands");
printf("\n !quit - terminate program");
printf("\n !count - lists the total amount of entries in the database");
printf("\n !about - program info\n\n");
perform = 0;
}
if(strstr(search, "!about"))
{
printf("\nAuthor: The Grim Pirate");
printf("\nHomepage: http://grimpirate.t35.com/");
printf("\nHistory: 2006.07.02 - Did not work with text files.");
printf("\n 2006.07.03a - no !list command, outputs to file.");
printf("\nVersion: 2006.07.03b\n\n");
perform = 0;
}
if(strstr(search, "!count"))
{
printf("\n%li entries in database.\n\n", ENTRIES);
perform = 0;
}
if(perform)
{
fp = fopen("results.txt", "w");
for(i = 0; i < ENTRIES; i++)
{
if(strstr(database[i][2], search))
{
count++;
fprintf(fp, "Location:\n");
fprintf(fp, database[i][0]);
fprintf(fp, "Description:\n");
fprintf(fp, database[i][1]);
fprintf(fp, "\n");
}
}
if(count == 0)
{
printf("\nNO RESULTS\n\n");
}
else
{
printf("\nOUTPUT SUCCESSFUL\n\n");
}
fclose(fp);
}
}
return (0);
}
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|