Code:
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
/*Declare glob variables*/
int numberOfIterations;
char MyPath[250];
char MyPath2[250];
int imax = INT_MAX;
/* Declare glob functions */
int* m_Rand (int iterations); /*This will return a pointer to the array in memory allocated with malloc*/
int* m_fWrite (char path[250], int buffer_size, int *mem_addr); /*This will read the buffer spiecified in the mem_addr and the relative size (buffer_size) and write the content to a file (path)*/
int* m_fRead (char path[250], int buffer_size); /*This will read a binary file and put the content of it into the buffer then return the pointer address*/
int main() {
printf("Integer MAX is %i and integer size is %d \n", imax, sizeof(int));
printf("Hello, put the source file path\n");
scanf("%s", &MyPath);
printf("Input is %s\n", MyPath);
printf("put the destination file path\n");
scanf("%s", &MyPath2);
printf("Input is %s\n", MyPath2);
printf("Now put the iterations number\n");
scanf("%d", &numberOfIterations);
printf("Input is %s and %i\n", MyPath, numberOfIterations);
// initialise an array in memory with numberOfIterations amount of lines filled with rand()
int* memory_address = m_Rand(numberOfIterations);
printf("Buffer Allocated, mem_addrress is %d\n", *memory_address);
// write it in a binary file (MyPath) from that memory_address
m_fWrite(MyPath, numberOfIterations, memory_address);
free(memory_address);
// read from that binary file (MyPath) and initialise another array
memory_address = m_fRead(MyPath, numberOfIterations);
printf("Buffer Allocated from file, mem_addrress is %d\n", *memory_address);
// write the content of memory_address in another file (MyPath2)
m_fWrite(MyPath2, numberOfIterations, memory_address);
free(memory_address);
return 0
};
int* m_Rand (int iterations){
int *mem_location;
int loopCount;
mem_location = malloc(sizeof(int) * iterations);
for (loopCount = 0; loopCount < iterations; loopCount++) {
/*printf("LoopCount %i, Iteration n %i\n", loopCount, iterations);*/
int randomicValue = rand();
mem_location[loopCount] = randomicValue;
};
return mem_location;
};
int* m_fWrite(char path[250],int buffer_size, int *mem_addr){
printf ("Start path is %s buffer_size is %d and *mem_addr is %i\n", path, buffer_size, *mem_addr);
FILE* the_file;
if ((the_file = fopen(path, "wb")) == NULL) {
printf ("Cannot open %s", path);
return 1;
};
printf ("calling fwrite to write from mem_addr %d with buffer_size %d into file %s\n", *mem_addr, buffer_size, the_file);
fwrite(mem_addr, sizeof(int), buffer_size, the_file);
if ( (fclose(the_file)) != 0) {
printf("Could not close file %s\n", path);
return 1;
};
return 0;
};
int* m_fRead(char path[250],int buffer_size){
printf ("Start path is %s buffer_size is %d\n", path, buffer_size);
int *mem_location = malloc(sizeof(int) * buffer_size);
FILE* the_file;
if ((the_file = fopen(path, "rb")) == NULL) {
printf ("Cannot open %s", path);
return 1;
};
printf ("calling fread to read to mem_location %d with buffer_size %d from file %s\n", *mem_location, buffer_size, the_file);
fread(mem_location, sizeof(int), buffer_size, the_file);
if ( (fclose(the_file)) != 0) {
printf("Could not close file %s\n", path);
return 1;
};
return mem_location;
};
I would like to ask to some experienced person to judge this code (not to hard please