/* * Program: Global Communicator II - scanner signals converter * Programmer: Robert John Morton UK-YE572246C * Date: Tue 22 Sep 2020 From the directory in which stns.c resides: to compile: gcc stns.c -o stns 'stns.idx' contains 177 4-byte integers. Each integer is the number of the record at which each broadcaster's frequencies start within 'stns.dat'. The first 4-byte integer is the number of broadcasters, thus the index num- bers of the broadcasters run fron 1 to 176. 'stns.dat' contains all the fre- quencies of all the broadcasters in one continuous stream of integers. */ #include // input output to/from the console and keyboard #include // standard functions for the 'C' language char FN[28] = "../hfbrx_freqs/freqs000.txt"; char S[8]; /* CONVERT AN 'int x' TO AN l-DIGIT NUMERIC STRING WITH LEADING SPACES OR ZEROS IN ARRAY S2[]). */ void showInt(int x, int l, int y) { char s = ' '; // for leading spaces if(y) s = '0'; // for leading zeros if(l > 11) l = 11; // safety precaution for(int i = 0; i < l; i++) S[i] = s; // fill the whole field with spaces or zeros S[l - 1] = '0'; S[l] = (char)0; // terminating NULL character if(x > 0) { // if the number is non-zero: int i = l; // total of 'l' possible digits in the number /* While there're still more digits to process, work backwards from [8] to [0], storing the remainder after dividing the number by 10 as a numeric character in array S[]. Then divide the number by 10 [unrounded] and loop back. */ while(x > 0 && i > 0) { S[--i] = (char)(x % 10 + 48); x /= 10; } } } int getNum(FILE *F) { int x = 0, c; char S[8]; while(!feof(F) && (c = fgetc(F)) != '\n') S[x++] = c; S[x] = '\0'; return atoi(S); // number of frequencies in this file } void putInt(int x, FILE *F) { // SAVE AN INT TO FILE AS 4-BYTE TRAIN fputc((x >> 24) & 0xFF, F); // Store the new number of records fputc((x >> 16) & 0xFF, F); // as a 4 byte int in the first fputc((x >> 8) & 0xFF, F); // 4 bytes of the records file. fputc(x & 0xFF, F); } void main() { FILE *FI = fopen("stns.idx","wb"); // open for writing FILE *FF = fopen("stns.dat","wb"); // open for writing int Q = 0; int N = 176; // number of broadcasters putInt(N,FI); // store the total number of broadcasters putInt(N,FI); // twice to make an 8-byte record int R = 0; // 'stns.dat' record number for(int K = 0; K < N; K++) { // for each broadcaster: showInt(K,3,1); // Insert sequence number [000 to 175] of for(int k = 0; k < 3; k++) // current freqs file as a 3-digit string FN[k + 20] = S[k]; // with leading zeros into file name string FILE *FT = fopen(FN,"rb"); // and open the input file for reading. int I = getNum(FT); // number of freqs for this broadcaster if(I > Q) Q = I; if(I > 200) { printf("Input File error: K=%i I=%i\n",K,I); break; } putInt(R,FI); // start record in stns.dat' for this broadcaster's freqs putInt(I,FI); // number of frequencies for this broadcaster R += I; // start record for next broadcaster for(int i = 0; i < I; i++) { // for each freq for this broadcaster int F = getNum(FT); putInt(F,FF); // copy it into 'stns.dat' } fclose(FT); // close the input file } fclose(FF); // close 'stns.dat' fclose(FI); // close 'stns.idx' printf("Max=%i\n",Q); }