#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <math.h>
#include <ctime>
#include <thread>
#include <Windows.h>
using namespace std;
bool compare(const char *s, const char *q, int n)
{
int i = 0;
while (i<n)
{
if (q[i++] != s[i++])
return false;
}
if (i = n && (s[n] - ' ' == 0 || s[n] - '\n' == 0 || s[n] == NULL))
return true;
else
return false;
}
int Triangle_num(char* fname)
{
FILE *fp;
if ((fp = fopen(fname, "r ")) == NULL)
{
printf("Can't open file");
exit(1);
}
int npoint = 0; //点个数
int count = 0; //三角面片个数
while (!feof(fp)) //feof()函数为判断文件是否结束,C语言
{
char f[256];
fgets(f, 256, fp);
if (compare(strtok(f, " "), "vertex ", 6))
{
if (++npoint == 3)
{
count++;
npoint = 0;
}
}
}
fclose(fp);
return count;
}
void main()
{
char *a, *b;
a = new char[50];
b = new char[50];
a = "C:\\Users\\Huan\\Desktop\\stl\\12workpiece.STL";
b = a;
Triangle_num(a);
system("pause");
delete[] b;//增加delete之后出现报错
}