user-0 提问: 把输出的数据写入txt,为什么总是出错，求高手指点下下

回复:
user: user-1
content: 引用 16 楼 jinmengjue1989 的回复:这个动态的我好像有点看不明白，我能确定数据量，就是想从txt读出后保存在x数组里，然后通过smoth函数后得到的数据保存在y数组里，就是这样，该怎么做啊？  能确定数据量，如果数据用空格分割，如下程序就可以了. int main() {         FILE *fp;     fp = fopen("d:\\data.txt", "r+");     int i,n;     double x[61];      double y[61];     n = 61;        for(int i=0;i<n;i++)     {           if(fscanf(fp,"%lf",x+i)! = 1 ){               printf("Error! Cannot load data %d",i);              fclose (fp);              return -3;            }     }              smoth(x,n,y);     fseek(fp,0,SEEK_END);     /* y 写在文件后面，否则打开一个新文件写就行了 */     for (i=0; i<n; i++)     {         printf("%4.4f ",y[i]);         fprintf(fp,"%4.4f ",y[i]);     }     fclose(fp);     return 0; }

user: user-2
content: 引用 8 楼 lipoor1234 的回复:[quote=引用 4 楼 wangliangffaflgh 的回复:] [quote=引用 1 楼 max_min_ 的回复:] smoth函数原型还有这样写的？ 楼主能否找本基础的书看看先？ 版主大人敬上，藕见过那种写法，在老的Unix操作系统源代码里面看到过。旧式的C函数是可以这样写的 [/quote]你应该是看岔了[/quote] http://www.pudn.com/downloads2/sourcecode/unix_linux/detail5168.html 有代码有真相

user: user-0
content: 引用 14 楼 lm_whales 的回复:[quote=引用 9 楼 jinmengjue1989 的回复:] [quote=引用 5 楼 lipoor1234 的回复:]  #include "stdio.h" #include "Windows.h" #include "stdlib.h" #include<iostream>  #include "math.h" int smoth(double* x,int n,double* y) {	  	//FILE *fp1; 	//fp1 = fopen("result-juli.txt", "wr"); 	int i; 	if(n < 5) 	{ 		printf("n should be at least 5.\n"); 		return(0); 	}  	    	y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/ 	y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0; 	for(i=2; i<n-2; i++)                                          /* 中间点*/ 		y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0; 	y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0; 	y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0; 	return(1); 	//fclose(fp1); }  using namespace std;  int main() {	 	FILE *fp; 	fp = fopen("d:\\data.txt", "w+"); 	int i,n; 	double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,}; 	double y[61]; 	n = 61; 	smoth(x,n,y); 	for (i=0; i<n; i++) 	{ 		printf("%4.4f ",y[i]); 		fprintf(fp,"%4.4f ",y[i]); 	} 	fclose(fp); 	return 0; } 我稍微修改了一下代码 你看一下吧 主要问题是smoth那个函数 我这样子写可以正确打印 以及保存到文件哈哈，真心有用，还有个问题就是我的数组x里的数据本来是存在一个txt里头的，我想问怎么能把这个txt里头的数字读到数组x里头，在经过如上操作呢？[/quote]  把数据读到数组，可以如此做，如果不能确定数据量，而且可能比较大，动态分配内存即可。  int main() {         FILE *fp;     fp = fopen("d:\\data.txt", "w+");     int i,n;     double x[61];      double y[61];     n = 0;     //这么做吧     if(fscanf(fp,"%d",&n)!=1){            fclose (fp);            printf("Error! Cannot load data number!");           return -1;     }     if(n <= 0 || n > 61){            fclose (fp);            printf("Error! data number must be in range 0..61!,n =%d ",n);           return -2;     }     for(int i=0;i<n;i++)     {           if(fscanf(fp,"%lf",x+i)! = 1 ){               printf("Error! Cannot load data %d",i);              fclose (fp);              return -3;            }     }              smoth(x,n,y);     for (i=0; i<n; i++)     {         printf("%4.4f ",y[i]);         fprintf(fp,"%4.4f ",y[i]);     }     fclose(fp);     return 0; }[/quote]这个动态的我好像有点看不明白，我能确定数据量，就是想从txt读出后保存在x数组里，然后通过smoth函数后得到的数据保存在y数组里，就是这样，该怎么做啊？

user: user-3
content: 引用 14 楼 lm_whales 的回复:[quote=引用 9 楼 jinmengjue1989 的回复:] [quote=引用 5 楼 lipoor1234 的回复:]  #include "stdio.h" #include "Windows.h" #include "stdlib.h" #include<iostream>  #include "math.h" int smoth(double* x,int n,double* y) {	  	//FILE *fp1; 	//fp1 = fopen("result-juli.txt", "wr"); 	int i; 	if(n < 5) 	{ 		printf("n should be at least 5.\n"); 		return(0); 	}  	    	y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/ 	y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0; 	for(i=2; i<n-2; i++)                                          /* 中间点*/ 		y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0; 	y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0; 	y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0; 	return(1); 	//fclose(fp1); }  using namespace std;  int main() {	 	FILE *fp; 	fp = fopen("d:\\data.txt", "w+"); 	int i,n; 	double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,}; 	double y[61]; 	n = 61; 	smoth(x,n,y); 	for (i=0; i<n; i++) 	{ 		printf("%4.4f ",y[i]); 		fprintf(fp,"%4.4f ",y[i]); 	} 	fclose(fp); 	return 0; } 我稍微修改了一下代码 你看一下吧 主要问题是smoth那个函数 我这样子写可以正确打印 以及保存到文件哈哈，真心有用，还有个问题就是我的数组x里的数据本来是存在一个txt里头的，我想问怎么能把这个txt里头的数字读到数组x里头，在经过如上操作呢？[/quote]  把数据读到数组，可以如此做，如果不能确定数据量，而且可能比较大，动态分配内存即可。  int main() {         FILE *fp;     fp = fopen("d:\\data.txt", "w+");     int i,n;     double x[61];      double y[61];     n = 0;     //这么做吧     if(fscanf(fp,"%d",&n)!=1){            fclose (fp);            printf("Error! Cannot load data number!");           return -1;     }     if(n <= 0 || n > 61){            fclose (fp);            printf("Error! data number must be in range 0..61!,n =%d ",n);           return -2;     }     for(int i=0;i<n;i++)     {           if(fscanf(fp,"%lf",x+i)! = 1 ){               printf("Error! Cannot load data %d",i);              fclose (fp);              return -3;            }     }              smoth(x,n,y);     for (i=0; i<n; i++)     {         printf("%4.4f ",y[i]);         fprintf(fp,"%4.4f ",y[i]);     }     fclose(fp);     return 0; }[/quote] 那个txt的文本是怎么样的 就像你输出的那个txt的格式么 用空格分开的?

user: user-1
content: 引用 9 楼 jinmengjue1989 的回复:[quote=引用 5 楼 lipoor1234 的回复:]  #include "stdio.h" #include "Windows.h" #include "stdlib.h" #include<iostream>  #include "math.h" int smoth(double* x,int n,double* y) {	  	//FILE *fp1; 	//fp1 = fopen("result-juli.txt", "wr"); 	int i; 	if(n < 5) 	{ 		printf("n should be at least 5.\n"); 		return(0); 	}  	    	y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/ 	y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0; 	for(i=2; i<n-2; i++)                                          /* 中间点*/ 		y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0; 	y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0; 	y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0; 	return(1); 	//fclose(fp1); }  using namespace std;  int main() {	 	FILE *fp; 	fp = fopen("d:\\data.txt", "w+"); 	int i,n; 	double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,}; 	double y[61]; 	n = 61; 	smoth(x,n,y); 	for (i=0; i<n; i++) 	{ 		printf("%4.4f ",y[i]); 		fprintf(fp,"%4.4f ",y[i]); 	} 	fclose(fp); 	return 0; } 我稍微修改了一下代码 你看一下吧 主要问题是smoth那个函数 我这样子写可以正确打印 以及保存到文件哈哈，真心有用，还有个问题就是我的数组x里的数据本来是存在一个txt里头的，我想问怎么能把这个txt里头的数字读到数组x里头，在经过如上操作呢？[/quote]  把数据读到数组，可以如此做，如果不能确定数据量，而且可能比较大，动态分配内存即可。  int main() {         FILE *fp;     fp = fopen("d:\\data.txt", "w+");     int i,n;     double x[61];      double y[61];     n = 0;     //这么做吧     if(fscanf(fp,"%d",&n)!=1){            fclose (fp);            printf("Error! Cannot load data number!");           return -1;     }     if(n <= 0 || n > 61){            fclose (fp);            printf("Error! data number must be in range 0..61!,n =%d ",n);           return -2;     }     for(int i=0;i<n;i++)     {           if(fscanf(fp,"%lf",x+i)! = 1 ){               printf("Error! Cannot load data %d",i);              fclose (fp);              return -3;            }     }              smoth(x,n,y);     for (i=0; i<n; i++)     {         printf("%4.4f ",y[i]);         fprintf(fp,"%4.4f ",y[i]);     }     fclose(fp);     return 0; }

user: user-0
content: 引用 10 楼 lipoor1234 的回复:[quote=引用 9 楼 jinmengjue1989 的回复:] [quote=引用 5 楼 lipoor1234 的回复:]  #include "stdio.h" #include "Windows.h" #include "stdlib.h" #include<iostream>  #include "math.h" int smoth(double* x,int n,double* y) {	  	//FILE *fp1; 	//fp1 = fopen("result-juli.txt", "wr"); 	int i; 	if(n < 5) 	{ 		printf("n should be at least 5.\n"); 		return(0); 	}  	    	y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/ 	y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0; 	for(i=2; i<n-2; i++)                                          /* 中间点*/ 		y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0; 	y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0; 	y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0; 	return(1); 	//fclose(fp1); }  using namespace std;  int main() {	 	FILE *fp; 	fp = fopen("d:\\data.txt", "w+"); 	int i,n; 	double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,}; 	double y[61]; 	n = 61; 	smoth(x,n,y); 	for (i=0; i<n; i++) 	{ 		printf("%4.4f ",y[i]); 		fprintf(fp,"%4.4f ",y[i]); 	} 	fclose(fp); 	return 0; } 我稍微修改了一下代码 你看一下吧 主要问题是smoth那个函数 我这样子写可以正确打印 以及保存到文件哈哈，真心有用，还有个问题就是我的数组x里的数据本来是存在一个txt里头的，我想问怎么能把这个txt里头的数字读到数组x里头，在经过如上操作呢？[/quote]int main() {         FILE *fp;     fp = fopen("d:\\data.txt", "w+");     int i,n;     double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,};     double y[61];     n = 61;     smoth(x,n,y);     for (i=0; i<n; i++)     {        		x[i]= y[i]; 		printf("%4.4f ",x[i]);         fprintf(fp,"%4.4f ",y[i]);     }     fclose(fp);     return 0; }你现在不是已经在y里了么 为什么还要从txt读取呢 直接把y的每个元素一次赋值给x就可以了[/quote]不是的，我的x里头的数字本来是保存在一个txt里头的，我只是复制到数组x里头，我的本意是想从txt中读取数字，经过smoth函数后把计算得到的数字保存在一个新的txt里头，但是不知道怎么读，就只好把数字复制到数组X里头了，但是，由于数据量很大，这样做很麻烦，就想直接把数字读进去，但是不知道怎么做！！我是这么写的，但是计算出来的结果貌似不对啊。高手，指点我下下！！！ using namespace std;  int main() {         FILE *fp1;     fp1 = fopen("d:\\data.txt", "w+");     int i,n; 	FILE *fp; 	double x[61]; 	fp=fopen("d:\\data1.txt","r");   for (i = 0; i < 61; i++) {   fscanf(fp,"%4.4f",&x[i]);   printf("%4.4f ",x[i]);      }  fclose(fp);  return 0;     double y[61];     n = 61;     smoth(x,n,y);     for (i=0; i<n; i++)     {         printf("%4.4f ",y[i]);         fprintf(fp1,"%4.4f ",y[i]);     }     fclose(fp1);     return 0; }

user: user-4
content: 应该是  #include "stdio.h" #include "Windows.h" #include "stdlib.h" #include<iostream.h> #include<math.h> #include "smoth.c" void main() {	     FILE *fp; int i,n;     double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,};     double y[61]; n = 61;     fp = fopen("d:\\data.txt", "w+");     smoth(x,n,y);      for (i=0; i<n; i++)      printf("%4.4f ",y[i]); {fprintf(fp,"%4.4f ",y[i]);}    fclose(fp); }

user: user-4
content: 你写的是C语言(我看到是.c文件)，变量声明要在开头。 .cpp文件的话变量声明位置没有要求。      FILE *fp; //   fp = fopen("d:\\data.txt", "w+");//把这句放到变量声明后面去 	int i,n;     double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,};     double y[61]; 	n = 61; //   fp = fopen("d:\\data.txt", "w+");//把这句放到变量声明后面去  引用 楼主 jinmengjue1989 的回复:程序如下：#include "stdio.h" #include "Windows.h" #include "stdlib.h" #include<iostream.h> #include<math.h> #include "smoth.c" void main() {	     FILE *fp;     fp = fopen("d:\\data.txt", "w+"); 	int i,n;     double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,};     double y[61]; 	n = 61;     smoth(x,n,y);      for (i=0; i<n; i++)      printf("%4.4f ",y[i]); 	{fprintf(fp,"%4.4f ",y[i]);}    fclose(fp); } 这个程序就是将数组x里的数据经过smoth（）函数平滑后，把数据再保存在数组y中，不用文件时，在DOS下输出结果都是正确的，没有报错，但是我想用文件把输出结果保存在txt时就出错了，错误如下： C:\Users\Diao\Desktop\五点三次平滑滤波\smoth0.c(13) : error C2143: syntax error : missing ';' before 'type' C:\Users\Diao\Desktop\五点三次平滑滤波\smoth0.c(14) : error C2143: syntax error : missing ';' before 'type' C:\Users\Diao\Desktop\五点三次平滑滤波\smoth0.c(16) : error C2065: 'x' : undeclared identifier C:\Users\Diao\Desktop\五点三次平滑滤波\smoth0.c(16) : error C2065: 'y' : undeclared identifier C:\Users\Diao\Desktop\五点三次平滑滤波\smoth0.c(21) : error C2109: subscript requires array or pointer type C:\Users\Diao\Desktop\五点三次平滑滤波\smoth0.c(22) : error C2109: subscript requires array or pointer type 执行 cl.exe 时出错. 下面是smoth函数的程序： /*============================================================= // 函 数 名：smoth(x,n,y) // 功能描述：五点三次平滑 // 输入参数：x（输入数据），n（系数个数）、y（平滑后序列） // 返 回 值：整型数字。计算成功则返回1，否则返回0 //==============================================================*/ #include "stdio.h" #include "stdlib.h" #include "math.h" int smoth(x,n,y) int n; double *x,*y; {    //FILE *fp1; 	   //fp1 = fopen("result-juli.txt", "wr");    int i;    if(n < 5)    { 	   printf("n should be at least 5.\n"); 	   return(0);    }  		      y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/    y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0;    for(i=2; i<n-2; i++)                                          /* 中间点*/ 	  y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0;    y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0;    y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0;    return(1);    //fclose(fp1); } 求高手指点下小妹吧！！！！！

user: user-3
content: 引用 9 楼 jinmengjue1989 的回复:[quote=引用 5 楼 lipoor1234 的回复:]  #include "stdio.h" #include "Windows.h" #include "stdlib.h" #include<iostream>  #include "math.h" int smoth(double* x,int n,double* y) {	  	//FILE *fp1; 	//fp1 = fopen("result-juli.txt", "wr"); 	int i; 	if(n < 5) 	{ 		printf("n should be at least 5.\n"); 		return(0); 	}  	    	y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/ 	y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0; 	for(i=2; i<n-2; i++)                                          /* 中间点*/ 		y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0; 	y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0; 	y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0; 	return(1); 	//fclose(fp1); }  using namespace std;  int main() {	 	FILE *fp; 	fp = fopen("d:\\data.txt", "w+"); 	int i,n; 	double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,}; 	double y[61]; 	n = 61; 	smoth(x,n,y); 	for (i=0; i<n; i++) 	{ 		printf("%4.4f ",y[i]); 		fprintf(fp,"%4.4f ",y[i]); 	} 	fclose(fp); 	return 0; } 我稍微修改了一下代码 你看一下吧 主要问题是smoth那个函数 我这样子写可以正确打印 以及保存到文件哈哈，真心有用，还有个问题就是我的数组x里的数据本来是存在一个txt里头的，我想问怎么能把这个txt里头的数字读到数组x里头，在经过如上操作呢？[/quote]int main() {         FILE *fp;     fp = fopen("d:\\data.txt", "w+");     int i,n;     double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,};     double y[61];     n = 61;     smoth(x,n,y);     for (i=0; i<n; i++)     {        		x[i]= y[i]; 		printf("%4.4f ",x[i]);         fprintf(fp,"%4.4f ",y[i]);     }     fclose(fp);     return 0; }你现在不是已经在y里了么 为什么还要从txt读取呢 直接把y的每个元素一次赋值给x就可以了

user: user-0
content: 引用 5 楼 lipoor1234 的回复: #include "stdio.h" #include "Windows.h" #include "stdlib.h" #include<iostream>  #include "math.h" int smoth(double* x,int n,double* y) {	  	//FILE *fp1; 	//fp1 = fopen("result-juli.txt", "wr"); 	int i; 	if(n < 5) 	{ 		printf("n should be at least 5.\n"); 		return(0); 	}  	    	y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/ 	y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0; 	for(i=2; i<n-2; i++)                                          /* 中间点*/ 		y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0; 	y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0; 	y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0; 	return(1); 	//fclose(fp1); }  using namespace std;  int main() {	 	FILE *fp; 	fp = fopen("d:\\data.txt", "w+"); 	int i,n; 	double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,}; 	double y[61]; 	n = 61; 	smoth(x,n,y); 	for (i=0; i<n; i++) 	{ 		printf("%4.4f ",y[i]); 		fprintf(fp,"%4.4f ",y[i]); 	} 	fclose(fp); 	return 0; } 我稍微修改了一下代码 你看一下吧 主要问题是smoth那个函数 我这样子写可以正确打印 以及保存到文件哈哈，真心有用，还有个问题就是我的数组x里的数据本来是存在一个txt里头的，我想问怎么能把这个txt里头的数字读到数组x里头，在经过如上操作呢？

user: user-3
content: 引用 4 楼 wangliangffaflgh 的回复:[quote=引用 1 楼 max_min_ 的回复:] smoth函数原型还有这样写的？ 楼主能否找本基础的书看看先？ 版主大人敬上，藕见过那种写法，在老的Unix操作系统源代码里面看到过。旧式的C函数是可以这样写的 [/quote]你应该是看岔了

user: user-5
content: 引用 4 楼 wangliangffaflgh 的回复:[quote=引用 1 楼 max_min_ 的回复:] smoth函数原型还有这样写的？ 楼主能否找本基础的书看看先？ 版主大人敬上，藕见过那种写法，在老的Unix操作系统源代码里面看到过。旧式的C函数是可以这样写的 [/quote]   学习了！以前没遇到遇到过这样的！

user: user-6
content: 函数 有这种写法吗？你应该去看看 基本语法

user: user-3
content: #include "stdio.h" #include "Windows.h" #include "stdlib.h" #include<iostream>  #include "math.h" int smoth(double* x,int n,double* y) {	  	//FILE *fp1; 	//fp1 = fopen("result-juli.txt", "wr"); 	int i; 	if(n < 5) 	{ 		printf("n should be at least 5.\n"); 		return(0); 	}  	    	y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/ 	y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0; 	for(i=2; i<n-2; i++)                                          /* 中间点*/ 		y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0; 	y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0; 	y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0; 	return(1); 	//fclose(fp1); }  using namespace std;  int main() {	 	FILE *fp; 	fp = fopen("d:\\data.txt", "w+"); 	int i,n; 	double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,}; 	double y[61]; 	n = 61; 	smoth(x,n,y); 	for (i=0; i<n; i++) 	{ 		printf("%4.4f ",y[i]); 		fprintf(fp,"%4.4f ",y[i]); 	} 	fclose(fp); 	return 0; } 我稍微修改了一下代码 你看一下吧 主要问题是smoth那个函数 我这样子写可以正确打印 以及保存到文件

user: user-2
content: 引用 1 楼 max_min_ 的回复:smoth函数原型还有这样写的？ 楼主能否找本基础的书看看先？ 版主大人敬上，藕见过那种写法，在老的Unix操作系统源代码里面看到过。旧式的C函数是可以这样写的

user: user-5
content: 参考下吧  #include <stdlib.h> #include<iostream> #include<math.h> #include <stdio.h> int smoth(double *x,int n,double *y)  {     //FILE *fp1;     //fp1 = fopen("result-juli.txt", "wr");     int i;     if(n < 5)     {            printf("n should be at least 5.\n");         return(0);     }              y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/     y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0;     for(i=2; i<n-2; i++)                                          /* 中间点*/         y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0;     y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0;     y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0;     return(1);     //fclose(fp1); }  int main() {     FILE *fp;     fp = fopen("d:\\data.txt", "w+");     int i,n;     double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,                     41,41,41,42,41,42,42,42,42,42,42,42,43,42,                     43,43,43,43,43,43,43,43,43,43,43,43,43,43,                     43,43,43,43,43,43,43,42,42,42,42,42,42,42,                     42,41,41,41,38,};     double y[61];     n = 61;      smoth(x,n,y);     for (i=0; i<n; i++)     {            printf("%4.4f ",y[i]);         fprintf(fp,"%4.4f ",y[i]);     }        fclose(fp);     return 0; }

user: user-2
content: #include "stdio.h"  int smoth(x,n,y) int n; double *x,*y; {    int i;    if(n < 5)    {        printf("n should be at least 5.\n");        return(0);    }  	       y[0] = (69.0*x[0]+4.0*(x[1]+x[3])-6.0*x[2]-x[4])/70.0;        /* 前两点*/    y[1] = (2.0*x[0]+27.0*x[1]+12.0*x[2]-8.0*x[3]+2.0*x[4])/35.0;    for(i=2; i<n-2; i++)    {        y[i] = (-3.0*x[i-2]+12.0*x[i-1]+17.0*x[i]+12.0*x[i+1]-3.0*x[i+2])/35.0;    }/* 中间点*/        y[n-2] = (2.0*x[n-5]-8.0*x[n-4]+12.0*x[n-3]+27.0*x[n-2]+2.0*x[n-1])/35.0;    y[n-1] = (x[n-5]+4.0*x[n-4]-6.0*x[n-3]+4.0*x[n-2]+69.0*x[n-1])/70.0;    return(1); }    #include "stdlib.h" #include <math.h> #include "smoth.c"  int main(void) {     FILE *fp;     fp = fopen("/Users/Scottie/Desktop/data.txt", "w+");     int i,n;     double x[61] = {37,38,38,39,38,39,39,40,39,40,40,40,41,40,41,41,41,42,41,42,42,42,42,42,42,42,43,42,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,41,41,41,38,};     double y[61];     n = 61;     smoth(x,n,y);     for (i=0; i<n; i++)     {         printf("%4.4f ",y[i]);         fprintf(fp,"%4.4f ",y[i]);     }     fclose(fp);          return 0; }

user: user-5
content: smoth函数原型还有这样写的？ 楼主能否找本基础的书看看先？