#include "pch.h"
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(int argc, char * argv[])
{
if (argc != 3)
{
printf("useage: cfind.ex <text> <filename>\n");
return -1;
}
//Get existing console color
int OldColor = 15;
CONSOLE_SCREEN_BUFFER_INFO csbi = { 0 };
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
{
OldColor = csbi.wAttributes;
}
char*pStringToMatch = argv[1];
char*pFileName = argv[2];
int len = strlen(pStringToMatch);
FILE * fp = fopen(pFileName, "r");
if (NULL == fp)
{
printf("failed to open file %s\n", pFileName);
return -2;
}
While (!feof(fp))
{
int c = fgetc(fp);
if (EOF == c) break;
//Let's find the string to match
if (c == pStringToMatch[0])
{
Char wword[1000];
int index = 0;
do
{
word[index++] = c;
c = fgetc(fp);
} while (!feof(fp) && index < len && index < 1000);
word[index] = '\0';
if (0 == strcmp(word, pStringToMatch))
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 206);
printf("%s", word);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), OldColor);
}
else
printf("%s", word);
}
printf("%c", c);
}
fclose(fp);
fp = NULL;
return 0;
}