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 */

Recommended Answers

All 2 Replies

Line 24

*chptr='\0';

Is chptr pointing to valid memory?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.