2016-09-14 5 views
2

私はC言語でのプログラミング経験は限られていますが、ソフトウェアの制約からこの言語を使用する必要があります。私はCFDソフトウェアのユーザカスタマイズを書こうとしています。C言語を使用して.txtファイルから最後の3変数値を読み取る方法

ソフトウェアは計算中に出力ファイルを書き込みますが、変数「PERCENT FILLED」を監視し、値が安定したらシミュレーションを終了します。私はコードの終了を行うことができます、私が持っている問題は、Cを使ってPERCENT FILLEDの値を読むことです。私は簡単にこれをPythonやシェルスクリプトで行うことができますが、Cと本当に苦労しています。

これを行うもっとエレガントな方法、私は喜んで学ぶことができますが、私は経験豊富なプログラマーではないので、私は複雑なソリューションで迷うかもしれません。

私はこれを、より小さなサブタスクに分割することでこれを試みました。この関数を頻繁に実行する必要がなく、読み込むファイルがそれほど大きくないので、コードの効率は重要ではありません。私はこれを行うのより速く、よりエレガントな方法があると確信しています! ウェブから他のコードを使用してそれらを修正すると、私は以下を思いついた。

私は以下のサブタスクを実行しようとしました: A)ストリップ「をPERCENT FILLED」を含むすべての行を、ファイル(PERCENT FILLED.txt) Bに書き込む)このファイルの行数を読み、読み最後の3行を別々の変数に変換します。 C)これらの変数を再フォーマットする必要があります。たとえば、3つの変数を "PERCENT FILLED = 6.72902E + 00"という形式の文字列から6.72902などの浮動小数点数に変更します。

私はA)を完了しました.B)でエラーがあり、Cで始める場所を見つけることができません。

ERROR in 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()を使用するのに十分なインライン情報があるようです。しかし、まず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; 

} 
+2

文字列から読み込み、書式指定子と変数を使用してデータを取得し、変数から出力をフォーマットします。 – KevinDTimm

答えて

1

おかげで、私は今働いてコードを持っています下に示された。私はこれを行うもっとエレガントな方法があると確信していますが、完全性のために、私はそれが誰かを助けることができる場合に備えて私の最終的なコードを載せます。

#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[256]; 
    char line2[256]; 
    char line3[256]; 

    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)) 
      { 
       strcpy (line3, readline); 
       count++; 
      } 
      else if (count == (number_of_lines-3)) 
      { 
       strcpy (line2, readline); 
       count++; 
      } 
      else if (count == (number_of_lines-2)) 
      { 
       strcpy (line1, readline); 
       count++; 
      } 
      else 
      { 
       count++; 
      } 
     } 
     fclose(infile); 
    }  

/* 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 varaible of value 6.72902 */ 

    char per1[7], fil1[6], eq1[1]; 
    char per2[7], fil2[6], eq2[1]; 
    char per3[7], fil3[6], eq3[1]; 
    float value1, value2, value3; 

    sscanf (line1, "%s %s %s %E", per1, fil1, eq1, &value1); 
    sscanf (line2, "%s %s %s %E", per2, fil2, eq2, &value2); 
    sscanf (line3, "%s %s %s %E", per3, fil3, eq3, &value3); 

    printf("Value1: %f\n", value1); 
    printf("Value2: %f\n", value2);  
    printf("Value3: %f\n", value3); 


    return 0; 

    } 
関連する問題