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


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))  {
   strcat(Str2, Str1);
   strcpy(Str2, delete1+1);

while(((delete2 = strstr(Str2, "$#$"))!=NULL)) {
   strcat(Str3, Str2);
   strcpy(Str3, delete2+3);
strcat(Str3, Str2);

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

} /* End of main */

Line 24


Is chptr pointing to valid memory?

yes rightly mentioned, chptr and other pointer type variables are declared but not allocated...