-2

How to read an ASCII STL file cubea.stl to print it to a file output.dat?

I wrote a part of the program, but this main.cpp doesn't work.

I'm probably missing some programs in main.cpp.

main.cpp

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cubea.stl"
#include "output.dat"
#define MAXLINE 128
#define PSLD "SOLID"
#define PNML "FACET NORMAL"
#define POUT "OUTER LOOP"
#define PVTX "VERTEX"
#define PELP "ENDLOOP"
#define PEFC "ENDFACET"
#define PESL "ENDSOLID"
#define FINAME "cubea.stl"
#define FONAME "output.dat"
#include "cubea.stl"
#include "output.dat"

int main();

int main() {
    char *cp;
    float n[3], v[3][3];
    FILE *fin, *fout;
    char txtline[MAXLINE];
    if ((fin = fopen(FINAME, "r")) == NULL) exit(1);
    if ((fout = fopen(FONAME, "w")) == NULL) exit(2);
    while ((cp = fgets(txtline, MAXLINE, fin)) != NULL);
    {
        if (strstr(cp, PSLD) == cp)
            fprintf(fout, "%s", cp);
        else if (strstr(cp, PNML) == cp) 
        {
            cp += strlen(PNML);
            sscanf(cp, "%f %f %f", &n[0], &n[1], &n[2]);
            fprintf(fout, "%s %f %f %f\n", PNML, n[0], n[1], n[2]);
        }
    }
    fclose(fin);
    fclose(fout);
}
  • 4
    please pick one language. C and C++ are two different languages. Then please explain what your code does and how that differs from what it should do – 463035818_is_not_an_ai Apr 12 '21 at 10:38
  • As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Apr 12 '21 at 10:38
  • What is the content of stl and dat file? You are using both files both as include files (even twice) for your code and as input/output files. I have some doubts this will fly. – Gerhardh Apr 12 '21 at 10:49
  • I think you need to read about what `#include` does. – molbdnilo Apr 12 '21 at 11:05
  • "doesn't work" is no useful description of what is happening. Please edit your question to provide in which way it fails. Please also include missing information that was already requested in previous comments like input, output, expected output... – Gerhardh Apr 12 '21 at 13:04

1 Answers1

0

you can't include .stl or .dat files.

You need to remove their include(which you made twice). just make sure cubea.stl exist in the path you're running the executable from and it should work and create output.dat

Shlomi Agiv
  • 1,183
  • 7
  • 17