Why does this code does not write the svg file?

Viewed 33

I have just started learning svd in C at the university, and I have this task due tonight, but I can't make this work. In theory, the only problem is that the svd file generates, but stays empty. I have done a lot of changes since the first try and the warnings have disappeared over time, and it generates a file, but still not write it. Can somebody please help me get this work fine?

The code:

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


int main(){
int ora, perc, mp;
//Beolvas
printf("Add meg az idot ora perc mp formatumban: ");
scanf("%d %d %d", &ora, &perc, &mp);
ora=ora%12;
//számolás (mp->rad)
double fokora, fokperc, fokmp;
    fokmp=((((double)mp/60))*360-90)*(M_PI/180);
    fokperc= ((((double)(mp+perc*60))/3600)*360-90)*(M_PI/180);
    fokora= ((((double)(mp+perc*60+ora*3600))/43200)*360-90)*(M_PI/180);
//Mutatók koordináltája
double oraX, oraY, percX, percY, mpX, mpY;
    oraX=60*cos(fokora);
    oraY=60*sin(fokora);
    percX=75*cos(fokperc);
    percY=75*sin(fokperc);
    mpX=70*cos(fokmp);
    mpY=70*sin(fokmp);
//rajz

//mutatok(0, 0, oraX, oraY);
//mutatok(0, 0, percX, percY);
//mutatok(0, 0, mpX, mpY);


FILE*fptr;
//fprintf("fptr,<svg>", "w");

fptr = (fopen("file:///C:/Users/madia/%C3%B3ra.svg", "w"));
fprintf(fptr,"<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
 fprintf(fptr,"<circle cx=\"0\" cy=\"0\" r=\"100\" stroke=\"black\" fill=\"blue\" />");
 fprintf(fptr,"<line x1=\"%d\" y1=\"%d\" x2=\"%f\" y2=\"%f\" stroke=\"black \"/>",0, 0,  oraX, oraY);
 fprintf(fptr,"<line x1=\"%d\" y1=\"%d\" x2=\"%f\" y2=\"%f\" stroke=\"black\"/>",0, 0,  percX, percY);
 fprintf(fptr,"<line x1=\"%d\" y1=\"%d\" x2=\"%f\" y2=\"%f\" stroke=\"black\"/>",0, 0,  mpX, mpY);
 int x=0;
 double beosztasX1, beosztasY1, beosztasX2, beosztasY2;
 while (x>60){
        if (x % 5==0){
                //orá beosztás
            beosztasX1= 90*cos(6*x);
            beosztasY1= 90*sin(6*x);
            beosztasX2= 100*cos(6*x);
            beosztasY2= 100*sin(6*x);
        }else{
                //perc beosztás
            beosztasX1= 95*cos(6*x);
            beosztasY1= 95*sin(6*x);
            beosztasX2= 100*cos(6*x);
            beosztasY2= 100*sin(6*x);
        }
double beosztasok(double x1, double y1, double x2, double y2){
beosztasok(beosztasX1, beosztasX2, beosztasY1, beosztasY2);
fprintf(fptr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke=\"black\"/>", beosztasX1, beosztasX2, beosztasY1, beosztasY2);
x+=1;
}



}
fprintf(fptr,"<\svg>");

return 0;

}
1 Answers

As noted in the comments, the main item that needs to be corrected is how your file is referenced in the opening of the file. Addressing that along with a couple of other refinements after testing out the code, following is a snippet of code I believe follows the spirit of the project.

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

int main()
{
    int ora, perc, mp;
    //Beolvas
    printf("Enter the time in hour minute second format: ");
    scanf("%d %d %d", &ora, &perc, &mp);
    ora=ora%12;
    //számolás (mp->rad)
    double fokora, fokperc, fokmp;
    fokmp=((((double)mp/60))*360-90)*(M_PI/180);
    fokperc= ((((double)(mp+perc*60))/3600)*360-90)*(M_PI/180);
    fokora= ((((double)(mp+perc*60+ora*3600))/43200)*360-90)*(M_PI/180);
    //Mutatók koordináltája
    double oraX, oraY, percX, percY, mpX, mpY;
    oraX=60*cos(fokora);
    oraY=60*sin(fokora);
    percX=75*cos(fokperc);
    percY=75*sin(fokperc);
    mpX=70*cos(fokmp);
    mpY=70*sin(fokmp);
    //rajz

    //mutatok(0, 0, oraX, oraY);
    //mutatok(0, 0, percX, percY);
    //mutatok(0, 0, mpX, mpY);

    FILE*fptr;
    //fprintf("fptr,<svg>", "w");

    fptr = (fopen("/home/craig/C_Programs/Console/SVG_Image/%C3%B3ra.svg", "w"));   /* Corrected this per comments - running this on a Linux machined */
    fprintf(fptr,"<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
    fprintf(fptr,"<circle cx=\"0\" cy=\"0\" r=\"100\" stroke=\"black\" fill=\"blue\" />");
    fprintf(fptr,"<line x1=\"%d\" y1=\"%d\" x2=\"%f\" y2=\"%f\" stroke=\"black \"/>",0, 0,  oraX, oraY);
    fprintf(fptr,"<line x1=\"%d\" y1=\"%d\" x2=\"%f\" y2=\"%f\" stroke=\"black\"/>",0, 0,  percX, percY);
    fprintf(fptr,"<line x1=\"%d\" y1=\"%d\" x2=\"%f\" y2=\"%f\" stroke=\"black\"/>",0, 0,  mpX, mpY);
    int x=0;
    double beosztasX1, beosztasY1, beosztasX2, beosztasY2;
    while (x>60)
    {
        if (x % 5==0)
        {
            //orá beosztás
            beosztasX1= 90*cos(6*x);
            beosztasY1= 90*sin(6*x);
            beosztasX2= 100*cos(6*x);
            beosztasY2= 100*sin(6*x);
        }
        else
        {
            //perc beosztás
            beosztasX1= 95*cos(6*x);
            beosztasY1= 95*sin(6*x);
            beosztasX2= 100*cos(6*x);
            beosztasY2= 100*sin(6*x);
        }

        /* Simplfied this - not sure if you were trying to define an inline function call, but the compiler was giving a warning */

        fprintf(fptr,"<line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" stroke=\"black\"/>", beosztasX1, beosztasX2, beosztasY1, beosztasY2);
        x+=1;

    }

    fprintf(fptr,"</svg>");     /* Corrected this to have a forward slash */

    return 0;

}

First off, I was running this code on a Linux machine, so the file path is formatted for references on a Linux machine. For a Windows machine you would adjust the format accordingly. Second, I was getting a compiler warning on your "beosztasok" function call. All that appeared to be happening there was that the program was supposed to output stroke coordinates, so it seemed the equivalent of what the original code was doing could be revised to just the "fprintf" call and the increment of the variable "x". Finally, just to correct a typo, instead of outputting "<\svg>" I corrected that to be "</svg>".

With those bits changed, I ran the sample code using a time value of "09 18 10"

@Una:~/C_Programs/Console/SVG_Image/bin/Release$ ./SVG_Image 
Enter the time in hour minute second format: 09 18 10

That resulted in an "SVG" file that could be viewed.

Sample SVG Image

Give that a try and see if it meets your project requirements.

Related