[ create a new paste ] login | about

Link: http://codepad.org/ratuFrx4    [ raw code | output | fork ]

C, pasted on Dec 6:
#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;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
Line 16: error: pch.h: No such file or directory
Line 19: error: stdafx.h: No such file or directory
Line 19: error: iostream: No such file or directory
Line 18: error: fstream: No such file or directory
Line 20: error: windows.h: No such file or directory
In function 'main':
Line 18: error: 'CONSOLE_SCREEN_BUFFER_INFO' undeclared (first use in this function)
Line 18: error: (Each undeclared identifier is reported only once
Line 18: error: for each function it appears in.)
Line 18: error: expected ';' before 'csbi'
Line 19: error: 'STD_OUTPUT_HANDLE' undeclared (first use in this function)
Line 19: error: 'csbi' undeclared (first use in this function)
Line 38: error: expected ';' before '{' token


Create a new paste based on this one


Comments: