高手来看看我在运行这个程序时为什么系统说遇到一个问题需要关闭呢?(VC++)

来源:百度知道 编辑:UC知道 时间:2024/06/06 15:49:44
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int x, p;
struct files
{
char input_filename[50],
output_filename[50];
};
struct coordinates
{
double c[50];
double r[50];
double ac[1100];
double ar[1100];
};
struct coordinates *coordinates_ptr;

int main(void)
{
struct coordinates *read_points(char[]);

struct files *read_filenames(void);
struct files *io_ptr;

io_ptr=read_filenames();
read_points(io_ptr->input_filename);

}
struct files *read_filenames(void)
{
struct files *io_ptr;
io_ptr=(struct files*)malloc(sizeof(struct files));
fprintf(stdout, "Input file name:");
fscanf(stdin, "%s", io_ptr->input_filename);

fprintf(stdout, "Output file name:");
fscanf(stdin, "%s", io_ptr->output_filename);
return

仔细想了想,原程序对io_ptr, coordinates_ptr的处理应该是可行的。不管是局部变量,全局变量还是静态变量,return已经把指针值传回去了,只要不free(), 就应该没有问题。因此问题确实不在这儿。
仔细读原程序知,程序未对x赋值就使用了。也许这是原因之一。 如果x=0, 则c[i],r[i]就没有读取过;如果x>0, 也不能保证input_file中恰好就存放x对数据。这只要看看
Please input your guidance parameters ,as the format x,y, by turn
输出了多少次就可以印证。顺便说一句:
fprintf(stdout, "Please input your guidance parameters ,as the format x,y, by turn");
是不必要的。因为是从文件读入,不是从控制台读入。
还要注意读入文件确实存在(因为你没有检查input_file==NULL的语句。由于文件名是键入的,难免错误,一定要加上。很可能是运行错误之一),而且其中的数据是以%lf,%lf的格式存放的。
建议
for(i=0;i<x;i++)
{
fprintf(stdout, "Please input your guidance parameters ,as the format x,y, by turn");
fscanf(input_file, "%lf,%lf", &coordinates_ptr->c[i], &coordinates_ptr->r[i]);
}
改为:
if (input_file==NULL) {fprintf(stderr, "Error open %s.\n", input_filename); return NULL; }
for(i=0;i<x;i++)
{
fscanf(input_file, "%lf,%lf", &coordi