在C中读取和操作数据列

时间:2014-09-24 19:24:27

标签: c

我有一个二进制数据文件,其外部到内部的尺寸如下:

ccount
t2count
mcount
tcount
chancount
opcount
pcount
complex number

我有另一个文本数据文件,其中包含这样的数据,例如

1.1
1.2
1.3
1.4

等等。文本文件中的行数与二进制文件中的“tcount”相同,即64.我不知道如何在C代码中读取该列的数据文件。一旦我可以读取并加载文本文件,我想我可以编写其余的代码。我希望在读取/加载文本数据文件时有一些帮助,然后提示如何将文本文件中数据的第一列与二进制数据文件中相应的复数相乘。到目前为止,这是我未完成的代码:

#include <stdio.h>
#include <complex.h>  /* Standard Library of Complex Numbers */
#include <math.h>
#include <stdlib.h>


typedef double complex  dcomplex;

//data converted to bigendian using io-general ---check !!!

//define a function "constructfloat"

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 agrc, char*argv[])
{

    int ccount = 202;
    int t2count = 32;
    int mcount = 6;
    int tcount = 64;
    int chancount = 4;
    int opcount = 4;
    int pcount = 179;

    int userci;
    int usert2i;
    int usermi;
    int userchani;
    int useropi;
    int userpi;

    //number of complex numbers per config

    int unitcountperconfig = (t2count*mcount*tcount*chancount*opcount*pcount);

    //initilize loop index variables

    int ci= 0;
    int t2i = 0;
    int mi = 0;
    int ti = 0;
    int chani = 0;
    int opi = 0;
    int pi = 0;


    // 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);
    }

    //USER INPUT
    printf("enter t2: ");
    scanf("%d",&usert2i);

    printf("enter mass: ");
    scanf("%d",&usermi);

    printf("enter channel: ");
    scanf("%d",&userchani);

    printf("enter operator :");
    scanf("%d",&useropi);

    printf("enter momentum: ");
    scanf("%d",&userpi);

    //Allocate memory to contain chunk of data for a time slice

    cbuff = (char*)malloc(sizeof(dcomplex)*unitcountperconfig);

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

    //Variable to hold a complex numer read from the file
    dcomplex aComplexNumber;

    dcomplex sumpertimeslice[tcount];

    //Loop on time slices

    for (ci = 0; ci < ccount; ci++) {
        //index of the complex number being read
        unsigned int cNumberIdx = 0;

        //Debugging message
        printf("Reading data for config : %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 succesfully is not equal to what we want to read, then exit
        if (result !=sizeof(dcomplex)*unitcountperconfig){
            fputs ("Data Reading Error\n",stderr);
            exit (3);
        }

        //Variable to hold real and imaginary part of the complex number in the inner most loop
        double realPart;
        double imagPart;
        for (t2i = 0; t2i < t2count; t2i++) {
            for (mi = 0; mi < mcount; mi++) {
                for (ti = 0; ti < tcount; ti++) {
                    double multiplier = 1.0;
                    sumpertimeslice[ti] = 0.0 + 0.0*_Complex_I;
                }
                    for (chani =0; chani < chancount; chani++) {
                        for (opi = 0; opi < opcount; opi++) {
                            for (pi = 0; pi < pcount; pi++) {
                                aComplexNumber = constructfloat (&cbuff[cNumberIdx],0 ) +constructfloat( &cbuff[cNumberIdx+ ((int)sizeof(dcomplex))/2 ], 0 )*_Complex_I;
                                if (t2i = usert2i) {
                                    if (mi = usermi) {
                                        if (chani = userchani) {
                                            if (opi = useropi) {
                                                if (pi = userpi) {
                                                    // HERE I WANT TO DO SOME MULTIPLICATION OF THE COMPLEX NUMBER AND THE COLUMN OF DATA IN THE TEXT FILE.
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

            }
        }


    }



}

0 个答案:

没有答案