I wrote a C program that converts a text file of numbers to a binary file of floats, or vice-versa. It's attached. It's all in one file, so you just have to compile that file and make an executable called "convert" or "convert.exe"

Then, say you have a chromosome file named "savedchromes". You just type the commmand:
convert savedchromes
and you get a file called savedchromes.txt which consists of the same values in text form. You can then email that file to someone whos floats are not binary compatible. He will then give the command:
convert savedchromes.txt
He will get a binary file named savedchromes.txt.bin which is compatible with his machine. He can rename the file to something more convenient if he wishes.

m

--
I'm proud of http://ANNEvolve.sourceforge.net. If you want to write software, or articles, or do testing or research for ANNEvolve, let me know.
/* convert.c - converts float array to text or vice-versa
   by M. Timin, August 2006
   Just give a filename; if it's a text file of numbers it will convert it
   to binary.  If it's a binary file it will convert it to text numbers.
*/

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

#define NUMS_PER_LINE 10        /* only used for outputting text */

void text2bin(char* name);
void bin2text(char* name);

int main(int argc, char *argv[])  {
    FILE* file;
    unsigned char c;
    int i;
    int isBinary = 0;

    if(argc != 2 || *argv[1] == '-' || *argv[1] == '/') {
       printf("Need one argument, a file name to convert.\n");
       exit(1);
    }
    file = fopen(argv[1], "rb");
    if(file == NULL) {
        printf("Can't open %s file\n", argv[1]);
        exit(0);
    }
    /* Read a few bytes to determine if this is a text file or a binary file: */

    for(i=0; i<10; i++){
        c = fgetc(file);
        if(c < ' ' || c > '~')  {
            isBinary = 1;
            break;
        }        
        /* printf("%4d %c\n", c, c); */
    }
    fclose(file);
    if(!isBinary)  {
        printf("Converting from text to binary.\n");
                text2bin(argv[1]);
        }
    else        {
        printf("Converting from binary to text.\n");
        bin2text(argv[1]);
    }    
    printf("Done.\n");    
}

void bin2text(char* name){
        int i, count;
        float val;
    FILE *infile, *outfile;
    
        infile = fopen(name, "rb");
    outfile = fopen(strcat(name, ".txt"), "w");
    if(infile == NULL) {
        printf("Can't open %s source file\n", name);
        exit(0);
    }
    if(outfile == NULL) {
        printf("Can't open %s destination file\n", strcat(name, ".txt"));
        exit(0);
    }
    
    for(i=1; ;i++) {
        count = fread(&val, 1, sizeof(float), infile); 
        if(count != sizeof(float))
                break;        
        fprintf(outfile, "%11.6f ", val);
        if(i%NUMS_PER_LINE == 0)
                fprintf(outfile, "\n");

    }         
    printf("read & wrote %d values\n", i-1);
        
        
    if((i-1)%NUMS_PER_LINE != 0)
        fprintf(outfile, "\n");
    fclose(infile);
    fclose(outfile);
}

void text2bin(char* name) {
        int i, count;
        float val;
    FILE *infile, *outfile;
    
        infile = fopen(name, "r");
    outfile = fopen(strcat(name, ".bin"), "wb");
    if(infile == NULL) {
        printf("Can't open %s source file\n", name);
        exit(0);
    }
    if(outfile == NULL) {
        printf("Can't open %s destination file\n", strcat(name, ".bin"));
        exit(0);
    }
    
    for(i=1; ;i++) {
        count = fscanf(infile, "%f", &val);
        if(count != 1)
                break;        
        fwrite(&val, 1, sizeof(float), outfile);
    }         
    printf("read & wrote %d values\n", i-1);
        
        
    fclose(infile);
    fclose(outfile);
}
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

Reply via email to