gpt4 book ai didi

c - Scanf突然停止工作

转载 作者:行者123 更新时间:2023-11-30 15:13:32 24 4
gpt4 key购买 nike

我正在尝试从Stl文件读取数据,一次使用scanf / fscanf读取一个字符。我在程序中多次调用scanf,但是在第一次输入所有数据的时候。它可以在一段时间内完美运行,但是好像scanf被调用为1022或1023rd时,该程序将停止并提示输入更多内容,即使还有很多要扫描的地方。

我之前在一个输入上编写了一个程序,调用scanf超过40,000次,所以我不知道这笔交易是什么...

scanf时,它似乎在getnormal()中的第一个z == 7处停止工作;

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

void getnormal(char[], double[]);
void getvertices(char[], double[], int);

int main (int argc, char *argv[]) {
unsigned char trash = 0;
int z = 0;

char num[20];
for (int i = 0; i < 20; i++)
num[i] = 48;

double normal [3] = {0,0,0};
double vertices [9] = {0,0,0,0,0,0,0,0,0};

//FILE *in = fopen(argv[1], "r");
//FILE *out = fopen(argv[2], "w");

while (trash != '\n')
scanf("%c", &trash);

while (1) {
getnormal(num, normal);
for (int i = 1; i <= 3; i++)
getvertices(num, vertices, i);
z++;
}
}

double getnumber(char num[]) {
unsigned char trash = 0;

for (int i = 0; i < 20; i++)
num[i] = 48;

scanf("%c", &trash);

for (int j = 0; trash != ' ' && trash != '\n'; j++) {
num[j] = trash;
scanf("%c", &trash);

if (trash == 'e') {
for (int h = 0; h < 4; h++)
scanf("%c", &trash);
break;
}
}
return atof(num);
}

void getnormal(char num[], double normal[]) {
unsigned char trash = 0;

while (trash != 'm')
scanf("%c", &trash);
while (trash != 'l')
scanf("%c", &trash);

scanf("%c", &trash);

for (int i = 0; i < 3; i++)
normal[i] = getnumber(num);

printf("\nnormal:");
for (int i = 0; i < 3; i++)
printf(" %f", normal[i]);
printf("\n");
}

void getvertices(char num[], double vertices[], int vertex) {
unsigned char trash = 0;

vertex = (vertex-1) * 3;

while (trash != 'x')
scanf("%c", &trash);
scanf("%c", &trash);

for (int i = 0; i < 3; i++)
vertices[vertex+i] = getnumber(num);

printf("vertex:");
for (int i = 0; i < 3; i++) {
printf(" %f", vertices[vertex+i]);
}
printf("\n");
}


这是输入:

solid Untitled-56807ca1
facet normal 0.0 0.0 -1.0
outer loop
vertex 100.0 50.0 0.0
vertex 0.0 0.0 0.0
vertex 0.0 50.0 0.0
endloop
endfacet
facet normal 0.0 0.0 -1.0
outer loop
vertex 0.0 0.0 0.0
vertex 100.0 50.0 0.0
vertex 100.0 0.0 0.0
endloop
endfacet
facet normal -1.0 0.0 0.0
outer loop
vertex 0.0 50.0 67.80499999999999
vertex 0.0 0.0 0.0
vertex 0.0 0.0 67.80499999999999
endloop
endfacet
facet normal -1.0 0.0 0.0
outer loop
vertex 0.0 0.0 0.0
vertex 0.0 50.0 67.80499999999999
vertex 0.0 50.0 0.0
endloop
endfacet
facet normal -0.0 1.0 0.0
outer loop
vertex 0.0 50.0 67.80499999999999
vertex 100.0 50.0 0.0
vertex 0.0 50.0 0.0
endloop
endfacet
facet normal -0.0 1.0 0.0
outer loop
vertex 100.0 50.0 0.0
vertex 0.0 50.0 67.80499999999999
vertex 100.0 50.0 67.80499999999999
endloop
endfacet
facet normal 1.0 0.0 0.0
outer loop
vertex 100.0 50.0 0.0
vertex 100.0 0.0 67.80499999999999
vertex 100.0 0.0 0.0
endloop
endfacet
facet normal 1.0 0.0 0.0
outer loop
vertex 100.0 0.0 67.80499999999999
vertex 100.0 50.0 0.0
vertex 100.0 50.0 67.80499999999999
endloop
endfacet
facet normal -0.0 -1.0 -0.0
outer loop
vertex 100.0 0.0 67.80499999999999
vertex 0.0 0.0 0.0
vertex 100.0 0.0 0.0
endloop
endfacet
facet normal -0.0 -1.0 -0.0
outer loop
vertex 0.0 0.0 0.0
vertex 100.0 0.0 67.80499999999999
vertex 0.0 0.0 67.80499999999999
endloop
endfacet
facet normal 0.8393626319551987 8.038371845750061e-18 0.5435718646851044
outer loop
vertex 100.0 50.0 67.80499999999999
vertex 68.99999999999989 15.49999999999995 115.67399999999996
vertex 100.0 0.0 67.80499999999999
endloop
endfacet
facet normal 0.8393626319551987 8.038371845750061e-18 0.5435718646851044
outer loop
vertex 68.99999999999989 15.49999999999995 115.67399999999996
vertex 100.0 50.0 67.80499999999999
vertex 68.99999999999989 34.49999999999993 115.67399999999996
endloop
endfacet
facet normal -0.8393626319552003 -1.4889432537737797e-17 0.5435718646851019
outer loop
vertex 30.9999999999999 15.49999999999995 115.67399999999996
vertex 0.0 50.0 67.80499999999999
vertex 0.0 0.0 67.80499999999999
endloop
endfacet
facet normal -0.8393626319552003 -1.4889432537737797e-17 0.5435718646851019
outer loop
vertex 0.0 50.0 67.80499999999999
vertex 30.9999999999999 15.49999999999995 115.67399999999996
vertex 30.9999999999999 34.49999999999993 115.67399999999996
endloop
endfacet
facet normal -5.5971483140148576e-18 -0.9513689920122365 0.30805363337837904
outer loop
vertex 68.99999999999989 15.49999999999995 115.67399999999996
vertex 0.0 0.0 67.80499999999999
vertex 100.0 0.0 67.80499999999999
endloop
endfacet
facet normal -5.5971483140148576e-18 -0.9513689920122365 0.30805363337837904
outer loop
vertex 0.0 0.0 67.80499999999999
vertex 68.99999999999989 15.49999999999995 115.67399999999996
vertex 30.9999999999999 15.49999999999995 115.67399999999996
endloop
endfacet
facet normal 9.646494768713815e-18 0.9513689920122359 0.30805363337838126
outer loop
vertex 30.9999999999999 34.49999999999993 115.67399999999996
vertex 100.0 50.0 67.80499999999999
vertex 0.0 50.0 67.80499999999999
endloop
endfacet
facet normal 9.646494768713815e-18 0.9513689920122359 0.30805363337838126
outer loop
vertex 100.0 50.0 67.80499999999999
vertex 30.9999999999999 34.49999999999993 115.67399999999996
vertex 68.99999999999989 34.49999999999993 115.67399999999996
endloop
endfacet
facet normal 0.0 -0.8819736246769688 0.47129876445219904
outer loop
vertex 49.99999999999989 24.99999999999994 133.45199999999997
vertex 30.9999999999999 15.49999999999995 115.67399999999996
vertex 68.99999999999989 15.49999999999995 115.67399999999996
endloop
endfacet
facet normal 0.6832351395351168 5.963605871623595e-18 0.7301984278978073
outer loop
vertex 68.99999999999989 34.49999999999993 115.67399999999996
vertex 49.99999999999989 24.99999999999994 133.45199999999997
vertex 68.99999999999989 15.49999999999995 115.67399999999996
endloop
endfacet
facet normal -0.6832351395351168 5.963605871623845e-18 0.7301984278978073
outer loop
vertex 49.99999999999989 24.99999999999994 133.45199999999997
vertex 30.9999999999999 34.49999999999993 115.67399999999996
vertex 30.9999999999999 15.49999999999995 115.67399999999996
endloop
endfacet
facet normal -0.0 -0.0 1.0
outer loop
vertex 68.99999999999989 15.49999999999995 115.67399999999996
vertex 30.9999999999999 34.49999999999993 115.67399999999996
vertex 30.9999999999999 15.49999999999995 115.67399999999996
endloop
endfacet
facet normal -0.0 -0.0 1.0
outer loop
vertex 30.9999999999999 34.49999999999993 115.67399999999996
vertex 68.99999999999989 15.49999999999995 115.67399999999996
vertex 68.99999999999989 34.49999999999993 115.67399999999996
endloop
endfacet
facet normal -0.0 0.8819736246769686 0.47129876445219915
outer loop
vertex 49.99999999999989 24.99999999999994 133.45199999999997
vertex 68.99999999999989 34.49999999999993 115.67399999999996
vertex 30.9999999999999 34.49999999999993 115.67399999999996
endloop
endfacet
endsolid Untitled-56807ca1


这是输出:

normal: 0.000000 0.000000 -1.000000
vertex: 100.000000 50.000000 0.000000
vertex: 0.000000 0.000000 0.000000
vertex: 0.000000 50.000000 0.000000

normal: 0.000000 0.000000 -1.000000
vertex: 0.000000 0.000000 0.000000
vertex: 100.000000 50.000000 0.000000
vertex: 100.000000 0.000000 0.000000

normal: -1.000000 0.000000 0.000000
vertex: 0.000000 50.000000 67.805000
vertex: 0.000000 0.000000 0.000000
vertex: 0.000000 0.000000 67.805000

normal: -1.000000 0.000000 0.000000
vertex: 0.000000 0.000000 0.000000
vertex: 0.000000 50.000000 67.805000
vertex: 0.000000 50.000000 0.000000

normal: -0.000000 1.000000 0.000000
vertex: 0.000000 50.000000 67.805000
vertex: 100.000000 50.000000 0.000000
vertex: 0.000000 50.000000 0.000000

normal: -0.000000 1.000000 0.000000
vertex: 100.000000 50.000000 0.000000
vertex: 0.000000 50.000000 67.805000
vertex: 100.000000 50.000000 67.805000

normal: 1.000000 0.000000 0.000000
vertex: 100.000000 50.000000 0.000000
vertex: 100.000000 0.000000 67.805000
vertex: 100.000000 0.000000 0.000000

最佳答案

您的函数getnumber有几个问题:

缓冲区num被初始化为所有'0',而没有空终止符。您在其中修补一些字节,直到看到空格或换行符,然后调用atof


没有用于防止缓冲区溢出的测试,无效的输入很可能在输入文件中的某些时候导致该溢出。
缓冲区不是以null终止的。 atof将在与浮点语法不匹配的第一个字符处停止,但是如果所有位置都是数字,它将继续读取num数组的末尾。


整个代码非常繁琐,语义也不明确。

您永远不会测试scanf的返回值:在文件结束时,其地址作为scanf参数传递的无符号字符的内容将是不确定的,很可能与前一个值保持不变...可能导致缓冲区溢出在getnumber中也是如此。

您的程序无法脱离while (1)循环。迟早,您将到达文件末尾,并且您的代码将调用未定义的行为或陷入无限循环。

getchar()fgets重写代码,并正确检查文件结尾。

关于c - Scanf突然停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34522831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com