views:

44

answers:

2

Hi, I am trying to extract data from a xml file, get rid of the xml tags, and store the values in a txt file (tab delimited format) using C. I was able to extract and manipulate the string but I am unable to paste the string to the txt file in a tab delimited format. Instead of getting a txt file that should read like:

x.xxxxxxx        x.xxxxxxxx    x.xxxxxxxx

I get a file that reads like

x.xxxxxxx

      x.xxxxxxx
      x.xxxxxxx

I think this is happening due to the way I am manipulating the string from x.xxxxxxx format to x.xxxxxxx format. As a beginner, I'm not entirely sure how to get around this problem. What can I do to fix it?

Thanks in advance.

#include <string.h>
#include <stdio.h>
#include <stddef.h>

#define MAX 10000

int main (int argc, char *argv[]){
    FILE *fp;
    char str[MAX];
    char *c[MAX];

    char *xcoor[MAX];
    char *ycoor[MAX];
    char *zcoor[MAX];
    char *z;
    int i =0;

    if((fp = fopen("550.txt", "r"))==NULL){
        printf("Cannot open file.\n");
        exit(1);
    }

    while(!feof(fp)) {
        while(fgets(str, sizeof str, fp)) {
            *(c+i)=strdup(str);
            i++;
        }
    }
    fclose(fp);
    int a = 0;

    for(i=5;i<1700;i=i+17){
        xcoor[a]=c[i];
        a++;
    }

    int b = 0;
    for(i=6;i<1700;i=i+17){
        ycoor[b]=c[i];
        b++;
    }

    char letterx = 'x';
    char lettery = 'y';
    char arrow1 = '<';
    char arrow2 = '>';
    char slash = '/';

    for (i=0;i<100;i++){
        z = *(xcoor+i);
        int g = 0;
        for(g;g<strlen(z);g++){
            if(z[g] == letterx)
            strcpy(z+g, z+g+1);
        }
        *(xcoor+i)=z;        
    }

    for (i=0;i<100;i++){
        z = *(xcoor+i);
        int g = 0;
        for(g;g<strlen(z);g++){
            if(z[g] == arrow1)
            strcpy(z+g, z+g+1);
        }
        *(xcoor+i)=z;    
    }

    for (i=0;i<100;i++){
        z = *(xcoor+i);
        int g = 0;
        for(g;g<strlen(z);g++){
            if(z[g] == arrow2)
            strcpy(z+g, z+g+1);
        }
        *(xcoor+i)=z;        
    }

    for (i=0;i<100;i++){
        z = *(xcoor+i);
        int g = 0;
        for(g;g<strlen(z);g++){
            if(z[g] == slash)
            strcpy(z+g, z+g+1);
         }
        *(xcoor+i)=z; 
    }

    for (i=0;i<100;i++){
        z = *(ycoor+i);
        int g = 0;
        for(g;g<strlen(z);g++){
            if(z[g] == lettery)
                strcpy(z+g, z+g+1);
        }
        *(ycoor+i)=z;        
    }

    for (i=0;i<100;i++){
        z = *(ycoor+i);
        int g = 0;
        for(g;g<strlen(z);g++){
            if(z[g] == arrow1)
                strcpy(z+g, z+g+1);
        }
        *(ycoor+i)=z;    
    }

    for (i=0;i<100;i++){
        z = *(ycoor+i);
        int g = 0;
         for(g;g<strlen(z);g++){
            if(z[g] == arrow2)
                strcpy(z+g, z+g+1);
        }
        *(ycoor+i)=z;        
    }

    for (i=0;i<100;i++){
        z = *(ycoor+i);
        int g = 0;
        for(g;g<strlen(z);g++){
            if(z[g] == slash)
                strcpy(z+g, z+g+1);
        }
        *(ycoor+i)=z;        
    }

    FILE *agentfile;
    agentfile = fopen("name.txt", "w");
    for(i=0;i<100;i++){
        fprintf(agentfile, "%s    %s", *(xcoor+i),    *(ycoor+i));
    }
    fclose(agentfile);
    printf("I'm happy that the program ended fine");

    return 0;
}
A: 

You are just filtering the tags, not extra newlines, carriage returns. Problem with your logic.

Here is what you can do: take the data which are only inside the tags, ignore anything outside.

Muktadir
A: 

fgets returns most a '\n', you must remove that:

while(!feof(fp)) {
    while(fgets(str, sizeof str, fp)) {
        *(c+i)=strdup(str);
        i++;
    }
}

better:

while( fgets(str, sizeof str, fp) ) {
  if( strchr(str,'\n') )
     *strchr(str,'\n')=0;
  *(c+i)=strdup(str);
  i++;
}

strdup is not ANSI C.