如何使用C

时间:2016-09-14 19:21:52

标签: c

我在C语言编程方面经验有限,但由于软件限制,必须使用这种语言。我正在尝试为CFD软件编写用户自定义。

软件在计算过程中写入输出文件,我想监视变量“PERCENT FILLED”并在值稳定时终止模拟。我可以终止代码,我遇到的问题是使用C读取PERCENT FILLED的值,我可以很容易地在python或shell脚本中执行此操作,但我真的很难用C语言。

可能有一种更优雅的方式,我很乐意学习,但我不是一个经验丰富的程序员,所以我可能会迷失复杂的解决方案。

我试图通过将任务分解为更小的子任务来做到这一点。代码的效率并不重要,因为不必频繁运行此函数,并且要读取的文件不是很大。我相信有更快更优雅的方式来做到这一点! 使用网络上的其他代码并修改它们我已经提出了以下内容。

我尝试执行以下子任务: A)去除包含“PERCENT FILLED”的所有行并将它们写入文件(PERCENT FILLED.txt) B)读取该文件中的行数,然后将最后3行读入单独的变量中。 C)然后我需要重新格式化这些变量。例如,将3个变量从“PERCENT FILLED = 6.72902E + 00”形式的字符串更改为值为6.72902的浮点值。

我已经完成了A),在B)中有错误,并且无法确定从哪里开始用C)。

B中的错误) - 当我运行代码时,它在while循环中正确地为line1,line2和line3分配值,但是一旦在while循环之外,所有值都会变为line1的值。这个我不明白。谁能帮助我理解和解决这个问题?

示例代码输出:

line3: PERCENT FILLED =   6.31275E+00
line2: PERCENT FILLED =   6.50146E+00
line1: PERCENT FILLED =   6.72902E+00
****************** 
out line1: PERCENT FILLED =   6.72902E+00
out line2: PERCENT FILLED =   6.72902E+00
out line3: PERCENT FILLED =   6.72902E+00

C部分) - 我不知道从哪里开始。看起来有足够的内联信息来使用atof()将字符串转换为float。但是,我首先需要从字符串的开头删除PERCENT FILLED =然后转换工程数字格式(某些值为1.00E + 02,因此只需剥离最后4个字符 - E + 00 - 将无效)。这个我无法解决怎么做。任何帮助,将不胜感激。

示例输入文件(通过删除PERCENT FILLED值之间的行来简化):

  Step = 171 Iteration = 0 Time step = 0.014849 Time = 4.834002
  Iter Variable Solver Loops          Delta      Solve CPU       Form CPU
     0        F  EXPLC     4   1.0000000E+00       0.007999       0.000000
 FRACTION SOLID = 0.000000e+00 %
 PERCENT FILLED =   5.46882E+00
  Step = 172 Iteration = 0 Time step = 0.029698 Time = 4.863700
  Iter Variable Solver Loops          Delta      Solve CPU       Form CPU
     0        F  EXPLC    11   1.0000000E+00       0.018997       0.000000
 FRACTION SOLID = 0.000000e+00 %
 PERCENT FILLED =   5.70902E+00
  Step = 173 Iteration = 0 Time step = 0.029698 Time = 4.893398
  Time step reduced by COURANT limit in free_surface, c_lim = 2.032750e-02
  Iter Variable Solver Loops          Delta      Solve CPU       Form CPU
     0        F  EXPLC     6   1.0000000E+00       0.010998       0.000000
 FRACTION SOLID = 0.000000e+00 %
 PERCENT FILLED =   5.89665E+00
  Step = 174 Iteration = 0 Time step = 0.020328 Time = 4.904356
  Iter Variable Solver Loops          Delta      Solve CPU       Form CPU
     0        F  EXPLC     7   1.0000000E+00       0.011997       0.000000
 FRACTION SOLID = 0.000000e+00 %
 PERCENT FILLED =   6.08026E+00
  Step = 175 Iteration = 0 Time step = 0.040655 Time = 4.945011
  Time step reduced by COURANT limit in free_surface, c_lim = 2.547617e-02
  Iter Variable Solver Loops          Delta      Solve CPU       Form CPU
     0        F  EXPLC     9   1.0000000E+00       0.016998       0.000000
 FRACTION SOLID = 0.000000e+00 %
 PERCENT FILLED =   6.31275E+00
  Step = 176 Iteration = 0 Time step = 0.025476 Time = 4.955307
  Time step reduced by COURANT limit in free_surface, c_lim = 1.997734e-02
  Iter Variable Solver Loops          Delta      Solve CPU       Form CPU
     0        F  EXPLC     9   1.0000000E+00       0.016994       0.000000
 FRACTION SOLID = 0.000000e+00 %
 PERCENT FILLED =   6.50146E+00
  Step = 177 Iteration = 0 Time step = 0.039955 Time = 4.989764
  Time step reduced by COURANT limit in free_surface, c_lim = 2.547537e-02
  Iter Variable Solver Loops          Delta      Solve CPU       Form CPU
     0        F  EXPLC    13   1.0000000E+00       0.024994       0.000000
 FRACTION SOLID = 0.000000e+00 %
 PERCENT FILLED =   6.72902E+00

我目前的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char line[1000];
    char *pch;
    char c[] = "PERCENT FILLED =";
    char buff[1000];


/* Create a list of PERCENT FILLED values and write to file - copy every line containing PERCENT FILLED from p.out file*/
    FILE *fp = fopen("inputFILE.txt", "r");
    FILE *op = fopen("PERCENT_FILLED.txt", "w");

    if(fp == NULL || op == NULL)
       {
          fprintf(stderr, "Error opening file.");
          exit(1);
       }
    else 
       {
         while (fgets(line, sizeof(line), fp) != 0)
           {
              if((pch = strstr (line, c))!= 0)
              fprintf(op, "%s", line);
           }
       }

   fclose(fp);
   fclose(op);


/* Get the total number of lines in the file PERCENT_FILLED.txt   */

   FILE* myfile = fopen("PERCENT_FILLED.txt", "r");
   int ch, number_of_lines = 0;
   int line_num = 0;
   int count = 0;
   char readline[256];                                /* or other suitable maximum line size */
   char* line1;
   char* line2;
   char* line3;

    do 
    {
        ch = fgetc(myfile);
        if(ch == '\n')
            number_of_lines++;
    } while (ch != EOF);

    // last line doesn't end with a new line!
    // but there has to be a line at least before the last line
    if(ch != '\n' && number_of_lines != 0) 
        number_of_lines++;

    fclose(myfile);




/* Get the last 3 PERCENT_FILLED values from the PERCENT_FILLED.txt   */


    FILE* infile = fopen("PERCENT_FILLED.txt", "r");

    if ( infile != NULL )
    {   
        while (fgets(readline, sizeof line, infile) != NULL) /* read a line */
        {   
            if (count == (number_of_lines-4))
            {   
               line3 = readline;
               count++;
               printf("\nline3:%s", line3);
            }   
            else if (count == (number_of_lines-3))
            {  
               line2 = readline;
               count++;
               printf("line2:%s", line2);
            }
            else if (count == (number_of_lines-2))
            {
               line1 = readline;
               count++;
               printf("line1:%s", line1);
            }
            else
            {   
                count++;
                printf("readline:%s count:%d\n", readline, count);
            }   
        }   
        fclose(infile);
    }   
    printf("******************\n");
    printf("out line1:%s", line1);
    printf("out line2:%s", line2);
    printf("out line3:%s\n\n", line3);


/* strip "PERCENT FILLED = " from the string and turn the string into a float  */
/* Do this for line1, line2, line3 */
/* Example string is "  PERCENT FILLED =   6.72902E+00" need to turn this in to a float variable of value 6.72902 */

    return 0;

}

1 个答案:

答案 0 :(得分:1)

感谢您的帮助,我现在正在使用代码,如下所示。我确信有一种更优雅的方式可以做到这一点,但为了完整起见,我会提出我的最终代码,以防它可以帮助某人。

config.transpiler = 'typescript',
    config.map = {
      'app': 'app', // this is where your transpiled files live
      '@angular': 'node_modules/@angular',
      'rxjs': 'node_modules/rxjs',
      'typescript': 'node_modules/typescript/lib/typescript.js'
    };
    config.packages = {
      'app': { main: 'main.js', format: 'cjs', defaultExtension: 'js' },

      '@angular/core': { main: 'index.js' },
      '@angular/common': { main: 'index.js' },
      '@angular/compiler': { main: 'index.js' },
      '@angular/forms': { main: 'index.js' },
      '@angular/http': { main: 'index.js' },
      '@angular/platform-browser': { main: 'index.js' },
      '@angular/platform-browser-dynamic': { main: 'index.js' },
      '@angular/router': { main: 'index.js' },
      'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
      'rxjs': { defaultExtension: 'js' },
    };
    config.bundles = {
      'dist/index.js': ['app/*'],

      'dist/dependencies.js': [
        '@angular/core/index.js',
        '@angular/common/index.js',
        '@angular/compiler/index.js',
        '@angular/platform-browser/index.js',
        '@angular/platform-browser-dynamic/index.js',
        '@angular/http/index.js',
        '@angular/router/index.js',
        '@angular/forms/index.js',
        'angular2-in-memory-web-api/index.js',
        'rxjs/*', 'rxjs/scheduler/*', 'rxjs/add/*', 'rxjs/add/operator/*', 'rxjs/observale/*', 'rxjs/add/observable/*',
      ]
    }