0

I keep getting a segmentation fault error with this program. It is supposed to read in a file that reads:

ABC=EFG$#$HIJ
#$#KLM=NOP

The program will then take out the all the "=" "$#$" and "#$#" and write it all back into another file.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char*argv[])
{
char Str1[256];
char Str2[256];
char Str3[256];
char Str4[256];
char *strptr1;
char *chptr;
char *strptr2;
char *delete1;
char *delete2;
char *delete3;
FILE *fp2;
FILE *fp;
fp = fopen(argv[1], "r");
fp2 = fopen(argv[2], "w");
fgets(Str1, 256, fp);
while(!feof(fp)) {
strcpy(Str2, "");
while(((delete1 = strchr(Str1, '='))!=NULL))  {
   *chptr='\0';
   strcat(Str2, Str1);
   strcpy(Str2, delete1+1);
}

while(((delete2 = strstr(Str2, "$#$"))!=NULL)) {
   *strptr1='\0';
   strcat(Str3, Str2);
   strcpy(Str3, delete2+3);
}
strcat(Str3, Str2);

while(((delete3 = strstr(Str3, "#$#"))!=NULL)) {
   *strptr2='\0';
   strcat(Str4, Str3);
   strcpy(Str4, delete3+3);
}
strcat(Str4, Str3);
fputs(Str4, fp2);
fgets(Str1, 256, fp);
} /* End of while loop */

} /* End of main */
3
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by rajeevpareek
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.