C 项目赢得';为结果添加fopen时不起作用
我正试图使用下面的代码为1000个数据点生成一个表格文件,当我不添加文件创建和打开部分时,程序会工作,但当添加了该部分时,程序不会在输入后结束C 项目赢得';为结果添加fopen时不起作用,c,input,orbit,C,Input,Orbit,我正试图使用下面的代码为1000个数据点生成一个表格文件,当我不添加文件创建和打开部分时,程序会工作,但当添加了该部分时,程序不会在输入后结束 #include <stdio.h> #include <stdlib.h> #include <math.h> void mapfun(int imap, double *c, double xi, double yi, double *xo, double *yo); void mapspin(int imap
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void mapfun(int imap, double *c, double xi, double yi, double *xo, double *yo);
void mapspin(int imap, double *c, int nspin, double *xo, double *yo);
int main(){
/* Defining values */
int imap;
int nspin=1000;
double xo;
double yo;
double c[4];
float mu;
/* Ask for an input of 1 or 2 */
printf("Input 1 or 2\n");
scanf("%d", &imap);
/* Assigning c when input is 1 and assigning mu */
if(imap==1){
c[1]=0.04;
c[2]=0.04;
printf("Input a value for mu between 2.8 and 3.8\n");
scanf("%e:%d", &mu);
if(mu>2.8 && mu<3.8){
c[0]=mu;
c[3]=mu;
} else{
printf("Invalid value for mu\n");
exit(0);
}
/* Assigning c when input is 2 and assigning mu */
} else if(imap==2){
c[1]=1;
c[2]=1;
printf("Input a value for mu between 1 and 3\n");
scanf("%e:%d", &mu);
if(mu>1 && mu<3){
c[0]=mu;
c[3]=mu;
} else{
printf("Invalid value for mu\n");
exit(0);
}
} else{
printf("Invalid value entered\n");
exit(0);
}
mapspin(imap, c, nspin, &xo, &yo);
/*File Creation and Opening*/
FILE*orbit;
orbit = fopen("orbit.dat", "rb+");
if(orbit == NULL) {
/* creates file if not there */
orbit = fopen("orbit.dat", "wb");
}
double xi=xo;
double yi=yo;
int i;
for(i=0; i<nspin; i+1){
mapfun(imap, c, xi, yi, &xo, &yo);
xi=xo;
yi=yo;
fprintf(orbit, "xo = %3f, yo = %3f\n", xo, yo);
}
orbit = fopen("orbit.dat", "r");
printf("c[0]= %.2f, c[1]= %.2f, c[2]= %.2f, c[3]= %.2f\n", c[0], c[1], c[2], c[3]);
exit(0);
}
#包括
#包括
#包括
MaFun(int IMAP,双*C,双席,双彝,双*XO,双*YO);
void mapspin(intimap、double*c、intnspin、double*xo、double*yo);
int main(){
/*定义值*/
int imap;
int-nspin=1000;
双xo;
双溜溜球;
双c[4];
浮亩;
/*要求输入1或2*/
printf(“输入1或2\n”);
scanf(“%d”和&imap);
/*输入为1时分配c并分配mu*/
if(imap==1){
c[1]=0.04;
c[2]=0.04;
printf(“输入介于2.8和3.8之间的mu值\n”);
scanf(“%e:%d”、&mu);
如果(mu>2.8&&mu1&&mu主要问题:
循环的格式不正确
for(i=0; i<nspin; i+1){
^^^ This does not change the value of i
^^^ That explains why the loop never ends.
这似乎是不必要的
其他信息
以下程序说明了如何意外修改从main
调用的函数中的值
#include <stdio.h>
void foo(int* x)
{
x[1] = 20;
}
int main()
{
int a = 10;
int b;
printf("Value of a before call to foo: %d\n", a);
foo(&b); // foo has no direct access to "a" but it can
// indirectly access "b" by using an out of
// bounds index.
printf("Value of a after call to foo: %d\n", a);
}
#包括
void foo(int*x)
{
x[1]=20;
}
int main()
{
INTA=10;
int b;
printf(“调用foo之前的值:%d\n”,a);
foo(&b);//foo无法直接访问“a”,但它可以
//通过使用out of of间接访问“b”
//边界索引。
printf(“调用foo后的a值:%d\n”,a);
}
我使用gcc 4.8.2构建了该程序。当我运行该程序时,输出为:
Value of a before call to foo: 10
Value of a after call to foo: 20
调用foo之前的值:10
对foo的事后调用的值:20
您可以使用b
选项将ascii/文本写入一个二进制打开文件。我认为如果该文件存在,您只需使用a
fopen选项进行追加,如果该文件不存在,ths将创建该文件:
orbit = fopen("orbit.dat", "a");
if(orbit == NULL) {
//IO Error cannot open the file in append mode or create the file
exit(1);
}
如果不执行fclose
调用,请不要重用文件描述符变量:
fprintf(orbit, "xo = %3f, yo = %3f\n", xo, yo);
//Fprintf Return value must be checked too
}
if( 0 != fclose( orbit ) )
{
//IO Error cannot close the opened file
exit(1);
}
orbit = fopen("orbit.dat", "r");
if( orbit == NULL )
{
//IO Error cannot open the file for reading
exit(1);
}
必须检查每个IO调用返回值/errno!
@r-sahu报告的主要问题是for
循环。请查阅文件函数或参考基本文件教程。fopen(文件名,“w”)
将创建一个新文件并覆盖一个同名的旧文件。当无法创建文件时,它将返回NULL
,您应该进行检查。无需先以读/写模式打开。(很少需要读/写模式“r+”
)。写入数据后,调用fclose(orbit)
。不要再调用fopen
。我已改为++I,但仍然遇到同样的问题。那行`orbit=fopen(“orbit.dat”,“r”)那是什么?我对C有点不熟悉。我觉得我应该注意到主帖子中的代码确实可以与另一个脚本一起使用,但效果很好。好的,谢谢,我已经调整了我的fopen代码。我最初使用ab+并将其更改为rb+,以修复它。我尝试了@r-sahu更改,但我仍然遇到了这个问题。
orbit = fopen("orbit.dat", "a");
if(orbit == NULL) {
//IO Error cannot open the file in append mode or create the file
exit(1);
}
fprintf(orbit, "xo = %3f, yo = %3f\n", xo, yo);
//Fprintf Return value must be checked too
}
if( 0 != fclose( orbit ) )
{
//IO Error cannot close the opened file
exit(1);
}
orbit = fopen("orbit.dat", "r");
if( orbit == NULL )
{
//IO Error cannot open the file for reading
exit(1);
}