【发布时间】:2015-02-20 06:25:30
【问题描述】:
我正在尝试通过迭代、进行一些计算并将索引添加到文件名来编写一堆文件,这是我的代码的一部分(我强调了代码停止编译的位置):
float AltAzCalc(int d, float t, float Lon, float RA, float Dec, float Lat){
FILE *in;
-----> char filename[30] = ("hiparcos_horizontal_%lf_%lf.csv",Lon,Lat);
in = fopen(filename, "w");
float PI = 3.14159265;// pi
float G = 6.5949997;
float Alt , Az;
float GST = G + 0.0657098244*d + 1.00273791*t;
if (GST > 24){
GST = GST - 24;
}
float LST = GST*360/24 + Lon;
Alt = (180/PI)*(asin(sin(PI*Dec/180)*sin(PI*Lat/180) + cos(PI*Dec/180)*cos(PI*Lat/180)*cos(PI*(LST-RA*360/24)/180)));
if(sin(PI*(LST-RA*360/24)/180) <= 0){
Az = (180/PI)*(acos((sin(PI*Dec/180)-(sin(PI*Alt/180)*sin(PI*Lat/180)))/(cos(PI*Alt/180)*cos(PI*Lat/180))));
}else{
Az = 360 - (180/PI)*(acos((sin(PI*Dec/180)-(sin(PI*Alt/180)*sin(PI*Lat/180)))/(cos(PI*Alt/180)*cos(PI*Lat/180))));
}
fprintf(in," %lf %lf \n",Alt,Az);
}
int main{
for(int i = -180 ; i < 181 ; i++){
for(int j = -180 ; j < 181 ; j++){
for(int k = 0; k < 119616 ; k++){
AltAzCalc(97,9.2,i,AscensionRecta.array[k],Declinacion.array[k],j);
}
}
}
}
我之前使用过这样的语法,只是没有添加任何我想要更改的额外数字,这是一个字符串文字,这就是它所要求的,知道如何解决这个问题吗?
【问题讨论】:
-
看来你需要
sprintf()的帮助。 -
能否提供一个简单的sintaxis示例?
-
也许您为了简单起见已对代码进行了精简,但您必须在打开文件时检查错误。
in = fopen(filename,"w"); if(in == NULL) {perror(filename); ... } -
@WilliamPursell 为什么? (我开始学习 C)
-
如果fopen失败(例如,文件存在且您没有写权限)并且您不检查,程序将出现段错误。当用户将
Segmentation fault视为错误消息时,远不如hiparcos_horizontal_1_1.csv: permission denied有用