dilluns, 16 de juliol del 2007

Reading vectors from a file & representing a code

Here is an example of how to use NTL library. This is a function I have used to read codes. I use the following format to save and load the code, I have tried to save the minium parameters necesaries to respresent code as standard way.

- int base of the code
- int word length
- int cardinal of the code (number of words)
- 1 line for each vector. form --> ( int int int int int )

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Reading Code from file
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
vec_ZZ_p io_read(list &code)
{
FILE *f;//File to read From
int i,j;
char c;
if (( f = fopen(filepath, "r+")) == NULL )
{
perror("This file can not be opened\n");
}

fscanf(f, "%d\n",&base);

/////////////// Definition used for the list of vectors ///////////////////
ZZ p; //Modulus of the vector
p = base;
ZZ_p::init(p); //Ring/Field definition
vec_ZZ_p v; //vector used to compute Intersection Array over ZZ_p
///////////////////////////////////////////////////////////////////////////////////////////

fscanf(f, "%d\n",&wordlength);
fscanf(f, "%d\n",&codecardinal);

v.SetLength(wordlength);

for (i = 0;i < codecardinal;i++)
{
for (j = 0;j < wordlength;j++)
{
fseek(f,1,SEEK_CUR);// jump the blckspace and the "(" character
c = fgetc(f); // get the character we want
v[j] = c-ASCII_CONVERSION;
}
fseek(f,2,SEEK_CUR);// jump the last chars of the string
code.push_back(v);
}
fclose(f);
return v;
}