3

I have created a program in c to read the temperature of a specific number of days, and then save all the data inputed into an XML file. I have handled to code the program, and make it generate an XML file with the data inputed in it, however the output displayed in the XML file is wrong, it is somehow badly formatted, so I would like to know if someone could help me with it, it is the first time I am working with XML.

My code is basically this:

#include <stdio.h>
#include <stdlib.h>

typedef struct{
    double minimum;
    double maximum;
    double average;
    int daynumber;
}Measurement;

#define QUANT 31

void measure(Measurement *);
void output(FILE *, Measurement *);

int main(int argc, char** argv) {

    Measurement m[QUANT];
    int i;
    FILE *f;

    printf("Input data:\n");
    for(i=0; i<1; i++)
        measure(&m[i]);

    printf("\nData was saved into xml file\n");
    f = fopen("data.xml","w");
    if(f==NULL){
        printf("Error");
        return 0;
    }
    fprintf (f,"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
    for(i=0;i<1;i++){
        output(f,m);
    }
    fclose(f);

    return (EXIT_SUCCESS);
}

void measure(Measurement *x){
    printf("Input minimum temperature: ");
    scanf("%f",&x->minimum);
    printf("Input maximum temperature: ");
    scanf("%f",&x->maximum);
    printf("Input average temperature: ");
    scanf("%f",&x->average);
    printf("Input day number: ");
    scanf("%f",&x->daynumber);
}
void output(FILE *f, Measurement *x){
    fprintf(f,"<day>\n");
    fprintf(f,"<minimum>%f</minimum>\n",x->minimum);
    fprintf(f,"<maximum>%f</maximum>\n",x->maximum);
    fprintf(f,"<average>%f</average>\n",x->average);
    fprintf(f,"<daynumber>%d</daynumber>\n",x->daynumber);
    fprintf(f,"</day>\n");
}

My output then is always like this:

<?xml version="1.0" encoding="utf-8"?>
<day>
  <minimum>0.000000</minimum>
  <maximum>0.000000</maximum>
  <average>0.000000</average>
  <daynumber>1065353216</daynumber>
</day>

Could you see what may be wrong in my code? Thanks in advance.

6
  • 2
    and what is wrong in the output? Commented Dec 23, 2019 at 14:20
  • 3
    side note: the output() always access m[0]. Commented Dec 23, 2019 at 14:22
  • It is never registering the input I give. I give for example: minimum = 32.0, maximum = 34.0, average = 33.0, daynumber=1; but the output is always as I showed above. Commented Dec 23, 2019 at 14:23
  • OT: regarding: printf("Error"); 1) error messages should be output to stderr, not stdout. 2) when the errorscanf("%f",&x->average); is from a C library function, should also output (to stderr) the text reason the system thinks thscanf("%f",&x->average);e error occurred. The function perror() is made to handle this scenario. Suggest: perror( "fopen failed" ); Commented Dec 24, 2019 at 4:21
  • OT: regarding this kind of statement: scanf("%f",&x->average); when calling any of the scanf() family of functions, always check the returned value (not the parameter values) In the current scenario, suggest: if( scanf("%f",&x->average) != 1 ) { fprintf( stderr, "failed to read average\n" ); exit( EXIT_FAILURE ); } Commented Dec 24, 2019 at 4:24

1 Answer 1

4

The problem appears to be in your function call:

 output(f,m);

This way, for each iteration, you're essentially accessing the first element m[0] in the function call.

You want to pass the address of each individual elements in the array, like

 output(f,&(m[i]));

or, to simplify, you can pass the element itself (not the address), like

 output(f,m[i]);

and change the function like

void output(FILE *f, Measurement x){ // second argument is not a pointer
    fprintf(f,"<day>\n");
    fprintf(f,"<minimum>%f</minimum>\n",x.minimum);  // x is not a pointer
    fprintf(f,"<maximum>%f</maximum>\n",x.maximum); .....

That said, the scan statements

  scanf("%f",&x->daynumber);

should be

  scanf("%d",&x->daynumber);

as daynumber is of type int, and for the others

scanf("%f",&x->minimum);

should be

scanf("%lf",&x->minimum);

as minimum and other members are of type double.

Sign up to request clarification or add additional context in comments.

2 Comments

Your approach makes much sense, based on other examples I have already seen, but I am not sure why it is still outputing the same thing.
Now I see the problem is with my "double" variables, since the output of daynumber was correct this time, but the others gave "0.000000". I will keep trying here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.