c - 在 C 代码中循环输入并使用终端一次将数据写入不同的文件

标签 c

我写了一个 C 代码,它从一个大小约为 1 GB 的二进制文件中提取数据。有 101(0 到 100)种配置,C 代码提取所选配置的数据并将输出写入文件。为了编译 C 代码,我在终端中给出了用户定义的配置编号,如下所示:

gcc binary2textperconfig.c -o f.out
./f.out proton-p000-1.bin out1.txt

然后它询问配置编号:

Enter the configuration number:

之后数据被写入文件“out0.txt”。现在我想为所有 101 个配置运行这段代码,并将这些数据写入文件“out0.txt”、“out1.txt”、....、“out100.txt”等。我不知道该怎么做无需在终端中输入配置编号 101 次。任何人都可以帮助我吗?这是我的 C 代码:

#include <stdio.h>
#include<complex.h>
#include<math.h>
#include <stdlib.h>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_matrix.h>


typedef double complex  dcomplex;


//Data is converted to Bigendian using io-general

double constructfloat(char bytes[sizeof(double)/2],  int order)
{
    double dRes;
    char *pc;
    int i;

    if (order == 0)
        for(i=0, pc = (char*) &dRes; i<=sizeof(double)-1 ; i++, pc++)
            (*pc) = bytes[i];
    else
        for(i=sizeof(double)-1, pc = (char*) &dRes; i>=0; i--, pc++)
            (*pc) = bytes[i];

    return dRes;
}


int main(int argc, char *argv[]){



    int configcount = 101;
    int mcount = 14;
    int tcount = 64;
    int d1count = 4;
    int d2count = 4;
    int pcount = 45;


    int userci;
    int usermi;
    int userpi;


    // number of complex numbers per configuration
    int unitcountperconfig =(mcount*tcount*d1count*d2count*pcount);


    // initialize loop index variables
    int ci = 0; //config
    int mi = 0; //mass
    int ti = 0;
    int d1i = 0;
    int d2i = 0;
    int pi = 0; //momentum

    // for holding the result of read operation ( how many units have been read)
    int result;

    // for holding the data read from file
    char * cbuff;


    // input file handle from where binary data is read
    FILE * fin = fopen(argv[1],"rb");

    // if the input file cannot be read for reading, close opened file handles, show an error message to the user, and exit
    if (fin==NULL)
    {
        fputs ("Error opening input file\n",stderr);
        exit (1);
    }

    FILE * fout = fopen(argv[2],"wt");

    // if the output file cannot be opened for writing, close opened file handles, show an error message to the user, and exit
    if (fout==NULL)
    {
        fclose(fin);
        fputs ("Error opening output file\n",stderr);
        exit (1);
    }


    // take input from the user
    // take input from the user
    printf("Enter the configuration number: ");
    scanf("%d",&userci);

    // allocate memory to contain the chunk of data for a time slice:
    cbuff = (char*)malloc(sizeof(dcomplex)*unitcountperconfig );

    // show error message and exit if memory allocation failed
    if(cbuff == NULL)
    {
        fputs("Buffer allocation failed.", stderr);
        exit(1);
    }

    // variable to hold a complex number read from the file
    dcomplex aComplexNumber;

    dcomplex sumpertimeslice[tcount];


    // loop on time slices
    for( ci = 0;  ci< configcount ; ci++){

        // index of the complex number being read
        unsigned int cNumberIdx = 0;

        // debugging message
        printf("reading data for configuration: %d\n",ci);

        // perform read operation to read the desired chunk of data
        result = fread(cbuff,  sizeof(char),  sizeof(dcomplex)*unitcountperconfig, fin );


        // if size of data successfully read is not equal to what we wanted to read, notify the user and exit
        if (result != sizeof(dcomplex)*unitcountperconfig) {
            fputs ("data reading error\n",stderr);
            exit (3);
        }

        double realP;
        double imagP;// variable to hold real and imaginary part of the complex number


        double realPSum;
        double imagPSum;// variable to hold sum of real and sum of imaginary part of the current sum per time slice


            for (mi =0; mi< mcount ; mi++){

                for (ti =0; ti< tcount ; ti++){



                // array to hold trace for each time slice
                sumpertimeslice[ti] = 0.0 + 0.0*_Complex_I;

                    for (d1i =0; d1i < d1count ; d1i++){
                        for (d2i =0; d2i < d2count ; d2i++){
                            for (pi =0; pi < pcount ; pi++){
                            aComplexNumber = constructfloat( &cbuff[cNumberIdx], 0 ) + constructfloat( &cbuff[cNumberIdx+ ((int)sizeof(dcomplex))/2 ], 0 )*_Complex_I;
                if (ci== userci)
                {
            cNumberIdx += (int)sizeof(dcomplex);
                            if (cimag(aComplexNumber)>0)
                            {fprintf( fout, "%d,%d,%d,%d,%d,%d,%e+%ei\n" ,ci+1, mi+1,ti+1, d1i+1,d2i+1,pi+1,creal( aComplexNumber ),cimag( aComplexNumber ) );}
                            else
                            {fprintf( fout, "%d,%d,%d,%d,%d,%d,%e%ei\n" ,ci+1, mi+1,ti+1, d1i+1,d2i+1,pi+1,creal(  aComplexNumber ),cimag( aComplexNumber ) );}
                }


                            }               
                        }           
                    }


                }           
            }

            }



    // free the allocated memory    
    free(cbuff);

    // close the opened file handles
    fclose(fin);
    fclose(fout);
    //fclose(complexNumberFileP);

}

最佳答案

使用 实用程序生成 0 到 100 之间的数字列表,以及 send it as a string to stdin .

for CNUMBER in $(seq 0 100); do
    ./f.out proton-p000-1.bin out${CNUMBER}.txt <<< "${CNUMBER}"
done

for CNUMBER in $(seq 0 100); do
    echo $CNUMBER | ./f.out proton-p000-1.bin out${CNUMBER}.txt
done

关于c - 在 C 代码中循环输入并使用终端一次将数据写入不同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23999793/

相关文章:

c - 在 C 中为矩阵分配内存,为什么我之后不能访问矩阵?

c - 使用 strftime 获取时间戳

c - 使用 RPCGen 了解 RPC

c - 我们在头文件中有 C 结构,我们希望从头文件中生成一个 XML 模式

c++ - 在二进制 {0, 255} 图像中找到中值时消除分支

ios - float 的不精确性

c - 释放内存时如何在 gdb 中得到通知?

command - "/usr/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";?

c - 如何使用scanf读取文件中的每个单词。

c - 使用以下命令查找两个数字之间的素数