简体   繁体   English

为结果添加fopen时程序无法正常工作

[英]Program won't work when adding fopen for results

I'm trying to generate a tabulation file for 1000 data points using this code below, the program works when I don't add the File Creation and Opening section, but when this is added the program doesn't end once I've entered the inputs. 我正在尝试使用下面的代码为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, 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);
}

Main problem: 主要问题:

The for loop is not well formed. for循环的格式不正确。

for(i=0; i<nspin; i+1){
                  ^^^ This does not change the value of i
                  ^^^ That explains why the loop never ends.

Use: 采用:

for(i=0; i<nspin; ++i){

Another issue: 另一个问题:

I don't understand why you have the line: 我不明白您为什么要这么做:

   orbit = fopen("orbit.dat", "r");

That seems to be an unnecessary line. 这似乎是不必要的一行。

Additional Info 附加信息

The following program illustrates how you can accidentally modify a value in a function called from main . 以下程序说明了如何意外修改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);
}

I built the program using gcc 4.8.2. 我使用gcc 4.8.2构建了程序。 When I run the program, the output is: 当我运行程序时,输出为:

Value of a before call to foo: 10
Value of a after call to foo: 20

You write ascii/text into a binary open file with the b option. 您可以使用b选项将ascii / text写入二进制打开文件。 I think if the file exists you just have to append by using a fopen option and ths will create the file if it doesn't exists: 我认为,如果该文件存在,你只需要通过追加a的fopen选项,如果部份不存在,它将创建该文件:

orbit = fopen("orbit.dat", "a");
if(orbit == NULL) {
    //IO Error cannot open the file in append mode or create the file
    exit(1);
}

And don't reuse a file descriptor variable without doing fclose call: 并且不要在不执行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);
}

Each IO call return value/errno must be checked! 必须检查每个IO调用的返回值/错误号!

And the main problem reported by @r-sahu is the for loop. @ r-sahu报告的主要问题是for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM