C 当我们在txt文件中搜索单词时,我们如何在txt文件中着色
Hi在c上生成代码,在txt文件中查找字符串 因此,我的代码从第一个字符中找到了字符串和pos的次数 但是我想在查找字符串时给txt文件中的字符串上色 但我不知道我该怎么做 守则:C 当我们在txt文件中搜索单词时,我们如何在txt文件中着色,c,search,colors,C,Search,Colors,Hi在c上生成代码,在txt文件中查找字符串 因此,我的代码从第一个字符中找到了字符串和pos的次数 但是我想在查找字符串时给txt文件中的字符串上色 但我不知道我该怎么做 守则: #include<stdio.h> #include<stdlib.h> #include<string.h> main() { FILE*fp; char fname[100]; char data[20]; char choice; c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE*fp;
char fname[100];
char data[20];
char choice;
char key[10];
char*ptr;
int counter=0;
int pos_start;
printf("Program start-up....\n");
printf("Enter file name: ");
gets(fname);
fp=fopen(fname,"r");
if (fp==NULL)
{
printf("Error:File can not be opened\n");
printf("program closed...\n");
exit(1);
}
printf("File is opened successfully\n");
printf("Do you want search for string(y/n)");
scanf(" %c",&choice);
while ((choice!='y') && (choice!='n'))
{
printf("Do you want search for string(y/n)");
scanf(" %c",&choice);
}
if (choice=='n')
{
printf("Search cancelled\n");
printf("Program closed...\n");
exit(1);
}
if (choice=='y')
{
printf("Search start-up...\n");
printf("Please insert word: ");
scanf("%s",key);
while(!feof(fp))
{
fgets(data,sizeof(data),fp);
ptr = strstr(data,key);
while(ptr!=NULL)
{
counter++;
pos_start=(ptr-data);
printf("Position: %d\n",pos_start);
ptr= strstr(ptr+1,key);
}
}
}
if (counter==0)
printf("Word not found\n");
if(counter>0)
printf("Word: %s found: %d times\n",key,counter);
printf("Search end\n");
printf("Progarm closed...\n");
fclose(fp);
return 0;
}
#包括
#包括
#包括
main()
{
文件*fp;
char-fname[100];
字符数据[20];
字符选择;
字符键[10];
char*ptr;
int计数器=0;
int pos_启动;
printf(“程序启动…”\n);
printf(“输入文件名:”);
获取(fname);
fp=fopen(fname,“r”);
如果(fp==NULL)
{
printf(“错误:无法打开文件\n”);
printf(“程序关闭…\n”);
出口(1);
}
printf(“文件已成功打开\n”);
printf(“是否要搜索字符串(y/n)”);
scanf(“%c”,选择(&c));
而((选择!='y')&&(选择!='n'))
{
printf(“是否要搜索字符串(y/n)”);
scanf(“%c”,选择(&c));
}
如果(选项=='n')
{
printf(“搜索已取消”);
printf(“程序关闭…\n”);
出口(1);
}
如果(选项=='y')
{
printf(“搜索启动…\n”);
printf(“请插入单词:”);
scanf(“%s”,键);
而(!feof(fp))
{
fgets(数据、sizeof(数据)、fp);
ptr=strstr(数据、键);
while(ptr!=NULL)
{
计数器++;
pos_开始=(ptr数据);
printf(“位置:%d\n”,位置开始);
ptr=strstr(ptr+1,键);
}
}
}
如果(计数器==0)
printf(“找不到单词”);
如果(计数器>0)
printf(“单词:%s找到:%d次\n”,键,计数器);
printf(“搜索结束\n”);
printf(“程序已关闭…\n”);
fclose(fp);
返回0;
}
不能,文本文件是一个文本文件,它只包含字符,没有任何格式(颜色、字体等)
您可能希望查看输出的其他文本文件格式,例如:HTML
编辑:
如果要将结果输出到控制台而不是文件,某些终端支持。因为它是离题的(您要求编写文件),所以您可以参考以在终端上打印颜色。您必须使用环境相关的库很难吗?我能举个例子吗?例如windows。对于Linux,如果我的第一个和最后一个位置是字符串,我不能着色?你怎么能?文本文件没有嵌入颜色信息。如果您想尝试输出到控制台,请参阅我的编辑。您的意思是我可以将所有文本和字符串的颜色作为cosole中的输出?很抱歉我的问题,但我在编程方面是新手,英语很差。如果您的终端支持,可以。我使用linux(特别是zorin),我不知道是否支持