在C代码中循环输入,并使用终端立即将数据写入不同的文件
我编写了一个C代码,它从大小约为1GB的二进制文件中提取数据。有101(0到100)个配置,C代码提取所选配置的数据并将输出写入文件。为了编译C代码,我在终端中给出了如下用户定义的配置号:在C代码中循环输入,并使用终端立即将数据写入不同的文件,c,seq,C,Seq,我编写了一个C代码,它从大小约为1GB的二进制文件中提取数据。有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个配置运行此代码,
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);
}
#包括
#包括
#包括
#包括
#包括
#包括
typedef双复数dcomplex;
//使用io general将数据转换为Bigendian
double-constructfloat(字符字节[sizeof(double)/2],整数顺序)
{
双dRes;
char*pc;
int i;
如果(顺序==0)
对于(i=0,pc=(char*)&dRes;i=0;i--,pc++)
(*pc)=字节[i];
返回dRes;
}
int main(int argc,char*argv[]){
int configcount=101;
int mcount=14;
int t计数=64;
int d1计数=4;
int d2计数=4;
int pcount=45;
int userci;
int usermi;
int-userpi;
//每个配置的复数数
int unitcountperconfig=(mcount*tcount*d1count*d2count*pcount);
//初始化循环索引变量
int ci=0;//配置
int mi=0;//质量
int ti=0;
int d1i=0;
int d2i=0;
int pi=0;//动量
//用于保存读取操作的结果(已读取多少个单元)
int结果;
//用于保存从文件读取的数据
char*cbuff;
//从中读取二进制数据的输入文件句柄
文件*fin=fopen(argv[1],“rb”);
//如果无法读取输入文件进行读取,请关闭打开的文件句柄,向用户显示错误消息,然后退出
如果(fin==NULL)
{
fputs(“打开输入文件时出错”,stderr);
出口(1);
}
文件*fout=fopen(argv[2],“wt”);
//如果无法打开输出文件进行写入,请关闭打开的文件句柄,向用户显示错误消息,然后退出
如果(fout==NULL)
{
财务总监(财务);
fputs(“打开输出文件时出错,\n”,stderr);
出口(1);
}
//从用户那里获取输入
//从用户那里获取输入
printf(“输入配置号:”);
scanf(“%d”、&userci);
//分配内存以包含时间片的数据块:
cbuff=(char*)malloc(sizeof(dcomplex)*unitcountperconfig);
//显示错误消息并在内存分配失败时退出
if(cbuff==NULL)
{
fputs(“缓冲区分配失败”,stderr);
出口(1);
}
//变量来保存从文件中读取的复数
d复杂数a复杂数;
dcomplex SumpertTimeSlice[tcount];
//循环时间片
对于(ci=0;ci0)
{fprintf(fout,“%d、%d、%d、%d、%d、%e+%ei\n”、ci+1、mi+1、ti+1、d1i+1、d2i+1、pi+1、creal(aComplexNumber)、cimag(aComplexNumber));}
其他的
{fprintf(fout,“%d、%d、%d、%d、%d、%e%ei\n”、ci+1、mi+1、ti+1、d1i+1、d2i+1、pi+1、creal(aComplexNumber)、cimag(aComplexNumber));}
}
}
}
}
}
}
}
//释放分配的内存
免费(cbuff);
//关闭打开的文件句柄
财务总监(财务);
fclose(fout);
//fclose(complexNumberFileP);
}
使用该实用程序生成介于0和100之间的数字列表
用于以美元为单位的CNUMBER(seq 0 100);做
./f.out proton-p000-1.bin out${CNUMBER}.txt配置号是否有a序列?是的……配置号是按顺序排列的。从0到100。这太棒了。非常感谢你的帮助!!