问题描述
如何读取 ASCII STL 文件 cubea.stl 并将其打印到文件 output.dat 中?
我写了一部分程序,但是这个 main.cpp 不起作用。
我可能在 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);
}
解决方法
您不能包含 .stl 或 .dat 文件。
您需要删除它们的包含(您创建了两次)。只需确保cubea.stl 存在于您运行可执行文件的路径中,它应该可以工作并创建output.dat