#include #include #define NAME_SIZE 20 #define SURNAME_SIZE 30 #define GENDER_SIZE 15 #define OCCUPANCY_SIZE 45 #define LOE_SIZE 35 #define EMAIL_SIZE 50 #define BAN_SIZE 20 #define IBAN_SIZE 30 #define TYPE_SIZE 20 #define UNIT_SIZE 10 #define AFL_SIZE 5 #define MAX_CUSTOMERS 100 typedef struct{ char name[NAME_SIZE]; char surname[SURNAME_SIZE]; char gender[GENDER_SIZE]; char occupancy[OCCUPANCY_SIZE]; char level_of_education[LOE_SIZE]; char email[EMAIL_SIZE]; char bank_account_number[BAN_SIZE]; char IBAN[IBAN_SIZE]; char account_type[TYPE_SIZE]; char currency_unit[UNIT_SIZE]; int total_balance_available; char available_for_loan[AFL_SIZE]; } Person; int convertCSVtoBinary() { FILE* csvFile; FILE* binFile; csvFile=fopen("records.csv","r"); binFile=fopen("records.dat","wb"); if(csvFile == NULL){ printf("Couldn't open CSV file\n"); return 1; } char row[1024]; while(fgets(row, 1024, csvFile)){ Person p; int attributeCount = sscanf(row, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%d,%[^\n]", p.name, p.surname, p.gender, p.occupancy, p.level_of_education, p.email, p.bank_account_number, p.IBAN, p.account_type, p.currency_unit, p.total_balance_available, p.available_for_loan); if (attributeCount != 12){ printf("problemli satir.\n"); } else{ fwrite(&p, sizeof(p), 1, binFile); } } fclose(binFile); fclose(csvFile); return 0; } int convertBinarytoXML() { printf("Starting BINARY to XML conversion...\n"); FILE* binFile; FILE* xmlFile; binFile = fopen("records.dat", "rb"); xmlFile = fopen("records.xml" ,"w"); // Return if an error occurs while opening the file if(binFile == NULL){ printf("Couldn't open bin file\n"); return 1; } // XML Header fprintf(xmlFile, "\n"); fprintf(xmlFile, "\n"); int id = 1; while(!feof(binFile)){ // Initialize fresh variables for each customer (each row element) char name[20], surname[30], gender[2], occupancy[30], level_of_education[20], email[50], bank_account_number[30], IBAN[28], account_type[14], currency_unit[6], available_for_loan[10]; int total_balance_available; // Read corresponding data from binary file to initialized variables fread(name, sizeof(char), NAME_SIZE, binFile); fread(surname, sizeof(char), SURNAME_SIZE, binFile); fread(gender, sizeof(char), GENDER_SIZE, binFile); fread(occupancy, sizeof(char), OCCUPANCY_SIZE, binFile); fread(level_of_education, sizeof(char), LOE_SIZE, binFile); fread(email, sizeof(char), EMAIL_SIZE, binFile); fread(bank_account_number, sizeof(char), BAN_SIZE, binFile); fread(IBAN, sizeof(char), IBAN_SIZE, binFile); fread(account_type, sizeof(char), TYPE_SIZE, binFile); fread(currency_unit, sizeof(char), UNIT_SIZE, binFile); fread(&total_balance_available, sizeof(int), 1, binFile); fread(available_for_loan, sizeof(char), AFL_SIZE, binFile); // Write and format XML file fprintf(xmlFile, " \n", id); fprintf(xmlFile, " \n"); fprintf(xmlFile, " %s}\n", name); fprintf(xmlFile, " %s}\n", surname); fprintf(xmlFile, " %s}\n", gender); fprintf(xmlFile, " %s}\n", occupancy); fprintf(xmlFile, " %s}\n", level_of_education); fprintf(xmlFile, " %s}\n", email); fprintf(xmlFile, " \n"); fprintf(xmlFile, " \n"); fprintf(xmlFile, " %s}\n", bank_account_number); fprintf(xmlFile, " %s}\n", IBAN); fprintf(xmlFile, " %s}\n", account_type); fprintf(xmlFile, " %s}\n", currency_unit); fprintf(xmlFile, " %d}\n", total_balance_available); fprintf(xmlFile, " \n"); fprintf(xmlFile, " \n"); // Increment id id++; } // XML Footer fprintf(xmlFile, "\n"); // Close both files fclose(binFile); fclose(xmlFile); printf("BINARY to XML conversion DONE.\n"); return 0; } int swap_Endians(int value) { // This var holds the leftmost 8 // bits of the output. int leftmost_byte; // This holds the left middle // 8 bits of the output int left_middle_byte; // This holds the right middle // 8 bits of the output int right_middle_byte; // This holds the rightmost // 8 bits of the output int rightmost_byte; // To store the result // after conversion int result; // Get the rightmost 8 bits of the number // by anding it 0x000000FF. since the last // 8 bits are all ones, the result will be the // rightmost 8 bits of the number. this will // be converted into the leftmost 8 bits for the // output (swapping) leftmost_byte = (value & 0x000000FF) >> 0; // Similarly, get the right middle and left // middle 8 bits which will become // the left_middle bits in the output left_middle_byte = (value & 0x0000FF00) >> 8; right_middle_byte = (value & 0x00FF0000) >> 16; // Get the leftmost 8 bits which will be the // rightmost 8 bits of the output rightmost_byte = (value & 0xFF000000) >> 24; // Left shift the 8 bits by 24 // so that it is shifted to the // leftmost end leftmost_byte <<= 24; // Similarly, left shift by 16 // so that it is in the left_middle // position. i.e, it starts at the // 9th bit from the left and ends at the // 16th bit from the left left_middle_byte <<= 16; right_middle_byte <<= 8; // The rightmost bit stays as it is // as it is in the correct position rightmost_byte <<= 0; // Result is the concatenation of all these values. result = (leftmost_byte | left_middle_byte | right_middle_byte | rightmost_byte); return result; } int main() { convertCSVtoBinary(); convertBinarytoXML(); printf("Done!"); return(0); }