用文本替换占位符 - C? [英] Replace placeholders with text - C?
问题描述
我使用以下函数从 FILE 流中一个一个地检索字符.现在,该文件中的文本有时会包含诸如 !first_name! 之类的位置持有者!文中.检测此类占位符何时开始和何时结束的最佳方法是什么,以便我可以在 !!具有来自另一个存储的适当字符?
I use the below function to retrieve characters one by one from a FILE stream. Now the text in that file will sometimes include places holders like !first_name! within the text. What's the best way to detect when such a place holder start and when it ends so that I can swap each character between !! with proper character from another storage?
void print_chars(FILE *file) {
fseek(file, 0L, 0);
int cr;
do {
cr = fgetc(file);
putchar(cr);
} while (cr != EOF);
}
推荐答案
您也可以采用更原始的方法,一次读取 1 个字符,然后在 if char=='!' 时中断.然后建立一个字符串直到另一个 '!'然后将该字符串与您的替换列表进行比较..
you could also take a more primitive approach also by reading it in 1 character at a time and then breaking out if char=='!' and then build a string up til the other '!' and then just compare that string with your list of replacements..
@goe 的回复说推一个字符..这里是一些代码
@goe's reply To say push a char.. here is some code
//c is char read from file
if(c=='!'){
char str[100];
c=fgetc
int i=0;
while(c!='!'){
str[i]=c;
i++;
c=fgetc
}
i++;
str[i]='\0'; //null terminate
if(strcmp(str,"....")... //continue...
}
这篇关于用文本替换占位符 - C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!