|
King of Portal
Join Date: Sep 2005
Posts: 403
Rep Power: 3 
|
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);
} I'm only including the search engine executable this time. For the database example file download the previous post's zip file.
__________________
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
|