I have a large .xml file and need to pull specific bits out of it. The things I need to pull out are encapsulated by a substring on either side. I need to write the output to a file.
I'm searching for the starting sub and from there for the ending sub, then copying it and putting it out over fprintf. I'm setting the start-pointer to the position of the last end pointer and it continues to search until it runs into the sigsegv.
I don't know how to stop the loop at the last occurrence of the substrings I'm searching for before it runs into the sigsegv.
An interesting problem I encountered is that if I output to stdout it prints everything I want to pull out and then breaks down. If I want to write it to a file it doesn't write the same thing but breaks down before it finishes and in the process losing the last 37 lines of output.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
FILE *fp;
fp = fopen("C:/Users/entin/Desktop/IHP/Auswerte_Marko/TEMP/20190605204730250_S210D_PQ41701_TM2_TV2_MARK21Single_21Single.ega_rslt", "r");
FILE *fw;
fw = fopen("C:/Users/entin/Desktop/IHP/Auswerte_Marko/TEMP/t1.xml", "w");
int f_length;
fseek(fp, 0, SEEK_END);
f_length = ftell(fp);
char file[f_length + 1];
rewind(fp);
fread(file, f_length, 1, fp);
file[f_length] = 0;
const char *SPattern = "<MeasData "; // start of substring
const char *EPattern = "</MeasData>"; // end of substring
char *start, *end;
char *target = NULL;
if (start = strstr(file, SPattern)) { // search for start substring
start += strlen(SPattern);
if (end = strstr(start, EPattern)) { // search for end substring
target = (char *) malloc(end - start + 1);
memcpy(target, start, end - start); // copying content between start and end pointers
target[end - start] = '\0';
start = end; // setting new start to old end
}
}
if (target) fprintf(stdout, "%s%s%s\n", SPattern, target, EPattern); // assembling everything back together
free(target);
//while (end <= EOF) { // repeating till end of file is reached
while (end != NULL && *end != 0){ //EDIT from comments
char *target = NULL;
if (start = strstr(start, SPattern)) { // startig search from last end pointer
start += strlen(SPattern);
if (end = strstr(start, EPattern)) {
target = (char *) malloc(end - start + 1);
memcpy(target, start, end - start);
target[end - start] = '\0';
start = end;
}
}
if (target) fprintf(stdout, "%s%s%s\n", SPattern, target, EPattern);
free(target);
}
fclose(fp);
fclose(fw);
getchar();
return 0;
}
Here are the files:
Output to stdout that I want in a file
Output that I get when I write to a file
(only the last lines of the output matter)