有没有办法让我在 DevC++ 的 C 程序中更改特定文本的颜色?
Is there a way for me to change the color of specific text in a C program in DevC++?
基本上,我正在为一个学校项目开发这个文本编辑器程序,它使用简单的归档和字符串函数来创建、追加、删除、显示、复制或从文本文件中搜索。我已经设法将所有函数放在一起,我的问题与我的 Search()
函数有关,我将在本文末尾附上该函数以供参考。如果找到搜索关键字,该函数会打印在哪一行找到它,并打印该行本身。我想做的是,如果可能的话,在打印该行时更改搜索关键字的颜色,使其突出,用户可以立即分辨出他的关键字在该行中的位置。我不是在没有对此事进行任何研究的情况下问这个问题;我做了一些挖掘,似乎我可以利用 ANSI 转义序列或 Windows 控制台虚拟终端序列来实现我想要做的事情,我确实浏览了 Microsoft 网站上的文档,但不幸的是我还不太习惯调用 windows API,我真的需要一些帮助来弄清楚如何去做这件事。另外,如果我附加的代码有时看起来效率低下或不必要,请提前道歉,几个月前才开始学习 C。我在 Windows 10 上使用带有 DevC++ 的 gcc 编译器。
TIA.
void Search(void) {
FILE *fptr;
char name[20], key[30], line[100];
int linenum=1, found=0;
printf("Enter name of text file to search from, or enter 'exit' to cancel and return to menu: ");
gets(name);
if(strcmp(name,"exit") == 0)
return;
strcat(name,".txt");
fptr = fopen(name,"r");
while(!fptr) { // just some input validation
printf("Error. Text file not found. Make sure it exists and is placed in the same folder as the .exe file for this program.");
fclose(fptr);
printf("\n\nEnter name of text file to search from, or enter 'exit' to cancel and return to menu: ");
gets(name);
if(strcmp(name,"exit") == 0)
return;
strcat(name,".txt");
fptr = fopen(name,"r");
}
printf("Enter your search keyword (remember, it is case sensitive): "); //searching staarts here
gets(key);
while(!feof(fptr)) {
fgets(line,100,fptr);
if(strstr(line,key)) {
printf("Keyword found in line %d:\n", linenum);
printf("----------------------------------------------------------------------------------------------\n");
printf("%s",line);
printf("----------------------------------------------------------------------------------------------\n\n");
found=1;
}
linenum++;
}
if(!found)
printf("Keyword not found in file.");
printf("\nPress any key to return to main menu: ");
getch();
}
我使用长 ---------- 分隔符的原因是我想将文本文件的内容与控制台中写入的内容区分开来 window .
基本上可以依靠字符串class来完成这个任务,使用string :: find
提取键的位置,然后使用string:: substr
打印值关键。
参考以下代码:
void Search(void) {
...
printf("Enter your search keyword (remember, it is case sensitive): "); //searching staarts here
gets_s(key);
while (!feof(fptr)) {
fgets(line, 100, fptr);
if (strstr(line, key)) {
std::string str(line);
printf("Keyword found in line %d:\n", linenum);
printf("----------------------------------------------------------------------------------------------\n");
int pos = str.find(key);
int len = strlen(key);
int len_t = strlen(line);
if (pos != 0)
{
printf("%s", str.substr(0, pos).c_str());
printf("\x1b[31m%s", str.substr(pos, len).c_str());
printf("\x1b[0m%s\r\n", str.substr(len+pos, len_t).c_str());
}
else
{
printf("\x1b[31m%s", str.substr(pos, len).c_str());
printf("\x1b[0m%s\r\n", str.substr(pos+len, len_t).c_str());
}
printf("\x1b[0m----------------------------------------------------------------------------------------------\n\n");
found = 1;
}
linenum++;
}
if (!found)
printf("Keyword not found in file.");
printf("\nPress any key to return to main menu: ");
getch();
}
调试:
或者您可以简单地使用 Windows API console functions:
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hstdout, FOREGROUND_RED|FOREGROUND_INTENSITY);
printf("hello ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
printf("world");
return 0;
}
基本上,我正在为一个学校项目开发这个文本编辑器程序,它使用简单的归档和字符串函数来创建、追加、删除、显示、复制或从文本文件中搜索。我已经设法将所有函数放在一起,我的问题与我的 Search()
函数有关,我将在本文末尾附上该函数以供参考。如果找到搜索关键字,该函数会打印在哪一行找到它,并打印该行本身。我想做的是,如果可能的话,在打印该行时更改搜索关键字的颜色,使其突出,用户可以立即分辨出他的关键字在该行中的位置。我不是在没有对此事进行任何研究的情况下问这个问题;我做了一些挖掘,似乎我可以利用 ANSI 转义序列或 Windows 控制台虚拟终端序列来实现我想要做的事情,我确实浏览了 Microsoft 网站上的文档,但不幸的是我还不太习惯调用 windows API,我真的需要一些帮助来弄清楚如何去做这件事。另外,如果我附加的代码有时看起来效率低下或不必要,请提前道歉,几个月前才开始学习 C。我在 Windows 10 上使用带有 DevC++ 的 gcc 编译器。
TIA.
void Search(void) {
FILE *fptr;
char name[20], key[30], line[100];
int linenum=1, found=0;
printf("Enter name of text file to search from, or enter 'exit' to cancel and return to menu: ");
gets(name);
if(strcmp(name,"exit") == 0)
return;
strcat(name,".txt");
fptr = fopen(name,"r");
while(!fptr) { // just some input validation
printf("Error. Text file not found. Make sure it exists and is placed in the same folder as the .exe file for this program.");
fclose(fptr);
printf("\n\nEnter name of text file to search from, or enter 'exit' to cancel and return to menu: ");
gets(name);
if(strcmp(name,"exit") == 0)
return;
strcat(name,".txt");
fptr = fopen(name,"r");
}
printf("Enter your search keyword (remember, it is case sensitive): "); //searching staarts here
gets(key);
while(!feof(fptr)) {
fgets(line,100,fptr);
if(strstr(line,key)) {
printf("Keyword found in line %d:\n", linenum);
printf("----------------------------------------------------------------------------------------------\n");
printf("%s",line);
printf("----------------------------------------------------------------------------------------------\n\n");
found=1;
}
linenum++;
}
if(!found)
printf("Keyword not found in file.");
printf("\nPress any key to return to main menu: ");
getch();
}
我使用长 ---------- 分隔符的原因是我想将文本文件的内容与控制台中写入的内容区分开来 window .
基本上可以依靠字符串class来完成这个任务,使用string :: find
提取键的位置,然后使用string:: substr
打印值关键。
参考以下代码:
void Search(void) {
...
printf("Enter your search keyword (remember, it is case sensitive): "); //searching staarts here
gets_s(key);
while (!feof(fptr)) {
fgets(line, 100, fptr);
if (strstr(line, key)) {
std::string str(line);
printf("Keyword found in line %d:\n", linenum);
printf("----------------------------------------------------------------------------------------------\n");
int pos = str.find(key);
int len = strlen(key);
int len_t = strlen(line);
if (pos != 0)
{
printf("%s", str.substr(0, pos).c_str());
printf("\x1b[31m%s", str.substr(pos, len).c_str());
printf("\x1b[0m%s\r\n", str.substr(len+pos, len_t).c_str());
}
else
{
printf("\x1b[31m%s", str.substr(pos, len).c_str());
printf("\x1b[0m%s\r\n", str.substr(pos+len, len_t).c_str());
}
printf("\x1b[0m----------------------------------------------------------------------------------------------\n\n");
found = 1;
}
linenum++;
}
if (!found)
printf("Keyword not found in file.");
printf("\nPress any key to return to main menu: ");
getch();
}
调试:
或者您可以简单地使用 Windows API console functions:
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hstdout, FOREGROUND_RED|FOREGROUND_INTENSITY);
printf("hello ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
printf("world");
return 0;
}