方法和详细的操作步骤如下:
1、第一步,依次单击visual C ++ 6.0的“文件”-->“新建”-->“文件”-->“C++ Source File”选项,见下图,转到下面的步骤。
2、第二步,执行完上面的操作之后,定义变量,代码见下图,转到下面的步骤。
3、第三步,执行完上面的操作之后,输入一个字符,使用getche函数接收,代码见下图,转到下面的步骤。
4、第四步,执行完上面的操作之后,输入如下代码,见下图,转到下面的步骤。
5、第五步,执行完上面的操作之后,再输入如下代码,见下图,转到下面的步骤。
6、第六步,执行完上面的操作之后,输出最终结果,见下图。这样,就解决了这个问题了。
给你个单词替换的例子,稍微改改就可以达到你的要求了。
/* Filename: word-replace.c
* Description: 替换文件中指定的单词为其他字符串并输出到指定的文件中
*/
#include
#include
#include
#include
#define MAX_WORDS_LEN 50 // 单词的最大长度限制
typedef enum { FALSE = 0, TRUE = 1 } BOOL;
struct Word {
char str[MAX_WORDS_LEN + 1]; // 存储字符串
BOOL isWord; // 是否为单词
};
typedef struct Node { // 使用链表存储解析后的字符串
struct Word word;
struct Node *next;
} *List;
enum Option { // 单词替换选项
MATCH_WORD_CASE, // 精确匹配单词区分大小写
MATCH_WORD_NOCASE, // 匹配单词但不区分大小写
};
// 函数功能:出错处理方法,打印出错信息并退出程序
void complain(const char *msg)
{
printf("%s\n", msg);
exit(EXIT_FAILURE);
}
// 函数功能:将结构体单词 w 插入不带头节点的单向不循环链表 L 的末尾
// 参数L:不带头节点的单向不循环链表的表头指针
// 参数w:指向待插入链表 L 的结构体单词信息的指针
void insert_list(List * L, const struct Word *w)
{
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
node->word = *w, node->next = NULL;
if (*L != NULL) { // 链表非空,则寻找链表的末尾并插入
struct Node *p;
for (p = *L; p->next != NULL; p = p->next) ;
p->next = node;
} else // 链表为空,直接插入
*L = node;
}
// 函数功能:打印不带头节点的单向不循环链表,参数L为该链表的表头指针
void print_list(List L)
{
for (struct Node * p = L; p; p = p->next)
printf("%s|%d\n", p->word.str, p->word.isWord);
}
// 函数功能:销毁不带头节点的单向不循环链表,参数L为该链表的表头指针
void dump_list(List L)
{
for (struct Node * p = L, *n; p; p = n) {
n = p->next;
free(p);
}
}
// 函数功能:不区分大小写的字符串比较函数,该函数不是标准C语言的库函数
int stricmp(const char *dst, const char *src)
{
int ch1, ch2;
do {
if (((ch1 = (unsigned char)(*(dst++))) >= 'A') && (ch1 <= 'Z'))
ch1 += 0x20;
if (((ch2 = (unsigned char)(*(src++))) >= 'A') && (ch2 <= 'Z'))
ch2 += 0x20;
} while (ch1 && (ch1 == ch2));
return (ch1 - ch2);
}
// 函数功能:解析文件指针fp_ro所指文件中的字符串,将其中的单词和非单词分离
// 出来,并将分离的结果存储到不带头节点的单向不循环链表L中。如果
// 函数成功执行,返回TRUE,否则返回FALSE。
BOOL word_parse(FILE * fp_ro, List * L)
{
if (fseek(fp_ro, 0L, SEEK_END))
return FALSE;
const long fsize = ftell(fp_ro);
if (fseek(fp_ro, 0L, SEEK_SET))
return FALSE;
char *buf = (char *)malloc(fsize + 1);
if (buf && fread(buf, fsize, 1, fp_ro) != 1 && ferror(fp_ro))
complain("Internal error.");
struct Word w;
char pword[MAX_WORDS_LEN + 1];
for (size_t i = 0, index = 0; i < (size_t) fsize;) {
index = 0;
while (!isalpha(buf[i]) && i < (size_t) fsize) { // 非字母
pword[index++] = buf[i++];
if (index == MAX_WORDS_LEN) { // 缓冲区溢出情况的处理
pword[index] = '\0'; // strncpy不自动添加'\0'
strncpy(w.str, pword, index + 1);
w.isWord = FALSE;
insert_list(L, &w);
index = 0;
}
}
if (index != 0) {
pword[index] = '\0'; // strncpy不自动添加'\0'
strncpy(w.str, pword, index + 1);
w.isWord = FALSE;
insert_list(L, &w);
}
index = 0;
while (isalpha(buf[i]) && i < (size_t) fsize) { // 单词
pword[index++] = buf[i++];
if (index == MAX_WORDS_LEN) // 缓冲区溢出情况的处理
complain("Too long word in source file.");
}
if (index != 0) {
pword[index] = '\0'; // strncpy不自动添加'\0'
strncpy(w.str, pword, index + 1);
w.isWord = TRUE;
insert_list(L, &w);
}
}
free(buf);
return TRUE;
}
// 函数功能:根据替换选项opt,替换在不带头节点的单向不循环链表L中的
// 单词fnd为新的字符串rep,并返回替换的次数。
int word_replace(List L, enum Option opt, const char *fnd, const char *rep)
{
int rep_cnt = 0; // 替换发生的次数
switch (opt) {
case MATCH_WORD_CASE:
for (struct Node * p = L; p; p = p->next)
if (p->word.isWord == TRUE && strcmp(p->word.str, fnd) == 0)
strcpy(p->word.str, rep), rep_cnt++;
break;
case MATCH_WORD_NOCASE:
for (struct Node * p = L; p; p = p->next)
if (p->word.isWord == TRUE && stricmp(p->word.str, fnd) == 0)
strcpy(p->word.str, rep), rep_cnt++;
break;
default:
fprintf(stderr, "Invalid option for function %s.", __func__);
}
return rep_cnt;
}
// 函数功能:将不带头节点的单向不循环链表 L 中的单词(字符串)按顺序存入由
// fp_wr所指的文件中。如果函数成功执行,返回TRUE,否则返回FALSE。
BOOL word_save(FILE * fp_wr, List L)
{
if (fseek(fp_wr, 0L, SEEK_SET))
return FALSE;
for (struct Node * p = L; p; p = p->next)
fprintf(fp_wr, "%s", p->word.str);
return TRUE;
}
// 程序功能:以文件为单位,执行单词替换
// 参数格式:命令 源文件 目标文件 查找的单词 替换的单词
int main(int argc, const char *argv[])
{
// 参数合法性检查
if (argc != 5 || strcmp(argv[1], argv[2]) == 0 ||
strlen(argv[3]) > MAX_WORDS_LEN || strlen(argv[4]) > MAX_WORDS_LEN)
complain("参数错误!\n"
"参数格式:命令 源文件 目标文件 查找的单词 替换的单词");
FILE *fin = fopen(argv[1], "rt");
FILE *fout = fopen(argv[2], "wt");
const char *const fnd = argv[3];
const char *const rep = argv[4];
if (fin == NULL || fout == NULL)
complain("文件输入输出错误!\n");
List L = NULL; // 不带头结点的单向链表的表头指针
if (word_parse(fin, &L) == FALSE)
complain("Parse error.");
print_list(L);
int rep_cnt = word_replace(L, MATCH_WORD_CASE, fnd, rep);
printf("共发生替换 %d 次。\n", rep_cnt);
word_save(fout, L);
dump_list(L);
fclose(fin);
fclose(fout);
return 0;
}
/*文件字符串替换实用程序,算法简练有效*/
编译环境vc2005/dev-c++
#include
#include
#include
#define N 10000 /* 设定文件最大字符个数不超过10000,可更改 */
int main()
{
int i=0,j,k=0,m=0,pos=0,max,len1,len2;
char s[N],t[N],a[200],s1[200],s2[200];
FILE *fp;
printf("Please input file name:\n");/*输入文件名*/
gets(a);
printf("Please input original string:\n");/*输入要替换的字符串*/
gets(s1);
printf("Please input new string:\n");/*输入被替换成的字符串*/
gets(s2);
len1=strlen(s1);
len2=strlen(s2);
if ((fp=fopen(a,"r"))==NULL)/*设定文件位于当前目录下,可更改为绝对路径*/
{ printf("Open file %s error! Strike any key to exit!",a);
system("pause");
exit(1);
}
s[i++]=fgetc(fp);
while(!feof(fp))
{s[i++]=fgetc(fp);
}
fclose(fp);
max=i-1;/* 函数feof()最后会读两次,所以必须减1 */
for(i=0;i
continue;/* 不相等则i加1进行下一次外循环 */
else
{ for(k=0;k
pos=i+len1;
for(k=0;k
}
}
for(k=pos;k
max=m;/* max是替换后文件的字符总数 */
fp=fopen(a,"w");
printf("\n\nThe results are:\n\n");
for(j=0;j
fputc(t[j],fp); /* 同时结果写入到当前目录下的新文件001.txt中 */
}
printf("\n\n");
fclose(fp);
system("pause");
return 0;
}