Home
About the Author
General Programming
Pascal
Delphi
C&C++
Visual Basic
SQL
JAVA Script
Links
| |
[ C Graphics ] [ C Math ] [ C Misc ] [ Common C&C++ Pitfalls ] [ Ponter Tutorial ]
Description:Delete a pattern from the string. Also
illustrates how gets() may be implemented
Any questions mail them to cpp@teentwo.8m.com
1. Delete a pattern from
the string. Also illustrates how gets() may be implemented
#define SIZE 1024
void *my_get(char user[], int size, FILE *fp);
int pattern(char origin[], char pattern[]);
main(int argc, char *argv[])
{
char *user;
FILE *fp;
if(argc !=3 )
{
printf("Usage: %s <file> <pattern>\n", argv[0]);
exit(0);
}
if((fp = fopen(argv[1], "r")) == NULL)
{
perror(argv[1]);
exit(1);
}
user = (char *)calloc(SIZE, sizeof(char));
while((my_get(user, 1024, fp))!=NULL)
{
pattern(user, argv[2]);
printf("%s", user);
}
return 0;
}
int pattern(char origin[], char pattern[])
{
int o, k, p;
int f;
for(o=0,f=0;origin[o];o++)
{
for(k=o,p=0;pattern[p] && pattern[p] == origin[k];k++,p++);
if(pattern[p] == NULL)
o += strlen(pattern)-1;
else
origin[f++] = origin[o];
}
origin[f] = '\0';
return 0;
}
void *my_get(char user[], int size, FILE *fp)
{
int c, i;
for(i=0;i < size-1 && (c = getc(fp)) != '\n' && c != EOF; i++)
user[i] = c;
if(c != EOF)
{
user[i++] = c;
user[i] = '\0';
return c;
}
return NULL;
}
Any questions mail them to cpp@teentwo.8m.com
| |
Newsgroups |
comp.lang.c
comp.lang.c++
comp.programming |
|