在一个程序中,而不是在外部文件中存储数据 [英] Storing data in a program instead of in an external file

查看:351
本文介绍了在一个程序中,而不是在外部文件中存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的 C $ C $,从文件名WMM.COF使用数据,并使用存储在文件中的数据来计算地球的磁场C的一部分。该项目工程完美,除了我不能有程序访问外部文件;我想有所有已存储在节目中的数据。我试图用一个结构数组复制数据,然后把数组转换成字符串,但这会导致错误的程序,并不会产生正确的结果。下面是我想修改程序的code。

I have the part of the following C code that uses data from a file names WMM.COF and uses the data stored in the file to compute the magnetic field of the earth. The program works perfectly except I can't have the program access the external file; I want to have all of the data already stored in the program. I tried using a structure array to replicate the data and then put the array into a string but this causes an error in the program and doesn't produce the correct results. Here is the code of the program that I'm trying to modify.

static void E0000(int IENTRY, int *maxdeg, double alt, double glat, double glon, double time, double *dec, double *dip, double *ti, double *gv)
{
  static int maxord,i,icomp,n,m,j,D1,D2,D3,D4;
  static double c[13][13],cd[13][13],tc[13][13],dp[13][13],snorm[169],
    sp[13],cp[13],fn[13],fm[13],pp[13],k[13][13],pi,dtr,a,b,re,
    a2,b2,c2,a4,b4,c4,epoch,gnm,hnm,dgnm,dhnm,flnmj,otime,oalt,
    olat,olon,dt,rlon,rlat,srlon,srlat,crlon,crlat,srlat2,
    crlat2,q,q1,q2,ct,st,r2,r,d,ca,sa,aor,ar,br,bt,bp,bpp,
    par,temp1,temp2,parp,bx,by,bz,bh;
  static char model[20], c_str[81], c_new[5];
  static double *p = snorm;
  char answer;

  FILE *wmmdat;


  wmmdat = fopen("WMM.COF","r");

/* INITIALIZE CONSTANTS */
  maxord = *maxdeg;
  sp[0] = 0.0;
  cp[0] = *p = pp[0] = 1.0;
  dp[0][0] = 0.0;
  a = 6378.137;
  b = 6356.7523142;
  re = 6371.2;
  a2 = a*a;
  b2 = b*b;
  c2 = a2-b2;
  a4 = a2*a2;
  b4 = b2*b2;
  c4 = a4 - b4;

/* READ WORLD MAGNETIC MODEL SPHERICAL HARMONIC COEFFICIENTS */
  c[0][0] = 0.0;
  cd[0][0] = 0.0;

  fgets(c_str, 80, wmmdat);


 S3:
  if (fgets(c_str, 80, wmmdat) == NULL) goto S4;

/* CHECK FOR LAST LINE IN FILE */
  for (i=0; i<4 && (c_str[i] != '\0'); i++)
    {
      c_new[i] = c_str[i];
      c_new[i+1] = '\0';
    }
  icomp = strcmp("9999", c_new);
  if (icomp == 0) goto S4;
/* END OF FILE NOT ENCOUNTERED, GET VALUES */
sscanf(c_str,"%d%d%lf%lf%lf%lf",&n,&m,&gnm,&hnm,&dgnm,&dhnm);

  if (n > maxord) goto S4;
  if (m > n || m < 0.0) 
    {
      fprintf(stderr, "Corrupt record in model file WMM.COF\n");
      exit(1);
    }

  if (m <= n)
    {
      c[m][n] = gnm;
      cd[m][n] = dgnm;
      if (m != 0)
        {
          c[n][m-1] = hnm;
          cd[n][m-1] = dhnm;
        }
    }
  goto S3;

/* CONVERT SCHMIDT NORMALIZED GAUSS COEFFICIENTS TO UNNORMALIZED */
 S4:
  *snorm = 1.0;
  fm[0] = 0.0;
  for (n=1; n<=maxord; n++)
    {
      *(snorm+n) = *(snorm+n-1)*(double)(2*n-1)/(double)n;
      j = 2;
      for (m=0,D1=1,D2=(n-m+D1)/D1; D2>0; D2--,m+=D1)
        {
          k[m][n] = (double)(((n-1)*(n-1))-(m*m))/(double)((2*n-1)*(2*n-3));
          if (m > 0)
            {
              flnmj = (double)((n-m+1)*j)/(double)(n+m);
              *(snorm+n+m*13) = *(snorm+n+(m-1)*13)*sqrt(flnmj);
              j = 1;
              c[n][m-1] = *(snorm+n+m*13)*c[n][m-1];
              cd[n][m-1] = *(snorm+n+m*13)*cd[n][m-1];
            }
          c[m][n] = *(snorm+n+m*13)*c[m][n];
          cd[m][n] = *(snorm+n+m*13)*cd[m][n];
        }
      fn[n] = (double)(n+1);
      fm[n] = (double)n;
    }
  k[1][1] = 0.0;

  otime = oalt = olat = olon = -1000.0;
  fclose(wmmdat);
  return;

这是我想出了code,包括数据的程序如下:

The code that I came up with to include the data in the program is as follows:

    struct wmm
       {
        int   alpha;
        int   beta;
        float gamma;
        float delta;
        float epsilon;
        float zeta;
       }book[90]= {{1, 0, -29496.6, 0.0, 11.6,  0.0},
  {1, 1,  -1586.3,    4944.4,       16.5,     -25.9},
  {2, 0,  -2396.6,       0.0,      -12.1,       0.0},
  {2, 1,   3026.1,   -2707.7,       -4.4,     -22.5},
  {2, 2,   1668.6,    -576.1,        1.9,     -11.8},
  {3, 0,   1340.1,       0.0,        0.4,       0.0},
  /* 50+ similar lines of code */
 {12,  8,    -0.4,       0.1,        0.0,        0.0},
 {12,  9,    -0.4,       0.3,        0.0,        0.0},
 {12, 10,     0.2,      -0.9,        0.0,        0.0},
 {12, 11,    -0.8,      -0.2,       -0.1,        0.0},
 {12, 12,     0.0,       0.9,        0.1,        0.0}};





for (i = 0; i < 90 && offset < buf_size; i++) 
    {
     offset += snprintf(c_str + offset,buf_size - offset, "%d %d %7.1lf %7.1lf %7.1lf %7.1lf \n", book[i].alpha, book[i].beta , book[i].gamma , book[i].delta, book[i].epsilon, book[i].zeta);
     }


     sscanf(c_str,"%d%d%lf%lf%lf%lf",&n,&m,&gnm,&hnm,&dgnm,&dhnm);  

问题是的snprintf导致程序每次被放置在节目时间冻结并终止。当code,我写的是在它自己的运行它似乎正确地创建c_str除了当我尝试到n查看变量,男,GNM,HNM,dgnm和dhnm只显示每个单个值。

The problem is the snprintf causes the program to freeze and terminate every time it is placed in the program. When the code that I wrote is run on it's own it seems to create c_str properly except when I try to view the variables n,m,gnm,hnm,dgnm, and dhnm only a single value for each is displayed.

推荐答案

我需要一个答案,继续由于缺乏空间/在注释的格式。

I need to continue in an answer due to a lack of space/formatting in a comment.

首先,你必须90项,但你可以让编译器弄不清数组有多少项的需要:

First of all, you do have 90 entries but you can let the compiler figure out how many entries the book array needs:

struct wmm {
    int   alpha;
    int   beta;
    float gamma;
    float delta;
    float epsilon;
    float zeta;
} book[] = {
    {1,   0, -29496.6,    0.0, 11.6,   0.0},
    {1,   1,  -1586.3, 4944.4, 16.5, -25.9},
    /* ... */
    {12, 12,      0.0,    0.9,  0.1,   0.0}
};

和,更重要的是,你并不需要将它们放在一个字符串,和他们拉背出来时,你已经有他们手头上:

And, more importantly, you don't need to put them in a string and pull them back out when you already have them on hand:

for(i = 0; i < sizeof(book)/sizeof(book[0]); ++i) {
    n    = book[i].alpha;
    m    = book[i].beta;
    gnm  = book[i].gamma;
    hnm  = book[i].delta;
    dgnm = book[i].epsilon;
    dhnm = book[i].zeta;
    /* Do whatever you need to do with the above variables. */
}

这将整齐地并排走什么缓冲区溢出,你还在导致您的的snprintf

This will neatly side step whatever buffer overflow you were causing with your snprintf.

c_str 只是一个的char [81] 键,你通过你的循环将90倍递增每次你的偏移量 c_str ;所以,你会跑掉 c_str 不久的结束,然后你会告诉的snprintf 来遍涂未分配的内存。因此,您的段错误。

Your c_str is only a char[81] and you're going through your loop 90 times and incrementing your offset into c_str each time; so, you'll run off the end of c_str before long and then you'll tell snprintf to scribble all over unallocated memory. Hence your segfault.

这篇关于在一个程序中,而不是在外部文件中存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆