Question

In: Computer Science

Array/File Functions Write a function to accept three arguments:  The name of a file, a...

Array/File Functions
Write a function to accept three arguments:
 The name of a file, a pointer to an int array, and the size of the array. The
function should open the specified file in binary mode, write the contents of the
array to the file, and then close the file.
Write another function to accept three arguments:
 The name of a file, a pointer to an int array, and the size of the array. The
function should open the specified file in binary mode, read its contents into the
array, and then close the file.
Write a complete program that demonstrates these functions:
 Function to write an array to a file, and then read the data from the same file.
An array values shall be created, write to a file, and after that data are read from
the file, and display the array’s contents on the screen.

Notes:
 The filename parameter is the name of the file,
 The array parameter is a pointer to the array, and
 The size parameter is the number of elements in the array (size of the array)

Solutions

Expert Solution

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include<stdlib.h>

//function to write the array to a file in binary mode

void binaryWrite(char *fname, int *ptr, int size);

//function to read from file into the array

void binaryRead(char *fname, int *ptr, int size);

int main(void) {

  //declare an array and initialize with some elements

  int arr[] = { 1,2,3,4,5,6 };

//using different array retArray while reading from bianry file , jsut to show that we are getting actutal result from the binary file

  int *retArray=(int*)malloc(sizeof(sizeof(arr) / sizeof(int)));

  int i;

printf("Content of array to write in binary mode to a file: \n");

for (i = 0; i < sizeof(arr) / sizeof(int); i++)

  {

    printf("%d ", arr[i]);

  }

printf("\n");

  binaryWrite("binaryWrite.dat", arr, sizeof(arr) / sizeof(int));

  binaryRead("binaryWrite.dat", retArray, sizeof(arr) / sizeof(int));

  //print read array

  printf("Array after reading binary file\n");

  binaryWrite("binaryWrite.dat", arr, sizeof(arr) / sizeof(int));

  for (i = 0; i < sizeof(arr) / sizeof(int); i++)

  {

    printf("%d ", retArray[i]);

  }

printf("\n");

  return 0;

}

//function to write the array to a file in binary mode

void binaryWrite(char *fname, int *ptr, int size)

{

  //open file in binary mode

  FILE *fp;

  fp = fopen(fname, "wb");

  if (fp == NULL)

  {

    printf("Not able to open file %s for writing in binary mode\n", fname);

    return;

  }

  fwrite(ptr, sizeof(int), size, fp);

  fclose(fp);

}

//function to read from file into the array

void binaryRead(char *fname, int *ptr, int size)

{

  //open file in binary mode

  FILE *fp;

  fp = fopen(fname, "rb");

  if (fp == NULL)

  {

    printf("Not able to open file %s for reading in binary mode\n", fname);

    return;

  }

  fread(ptr, sizeof(int), size, fp);

  fclose(fp);

}

/*Output

 ./main

Content of array to write in binary mode to a file:

1 2 3 4 5 6

Array after reading binary file

1 2 3 4 5 6

*/

//Since programming language is not given , I am taking it as C , you can also execute this code in C++


Related Solutions

Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...
Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The name of the file, a pointer to an array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to file, and then close the file. Write another function named fileToArray. This function should accept 3 arguments: the name of the file, a pointer, to an int array, and the size of...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
write a function that accept an array and its size than return an array call modeAry...
write a function that accept an array and its size than return an array call modeAry that store the following information:    modeAry[0] = Number of modes; modeAry[1] = Frequency of the modes ; modeAry[>=2] = All the modes you found   ;        EXP:if the array input is [1,1,2,2,4,3,3], the function modAry should be [3,2,1,2,3]. In c++
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The first ofile should be opened for reading and the second file should be opened for writing. The class should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, excpet all the characters will be uppercase. Use notepad or another text editor to...
C++ Write a function that accepts an array of doubles and the array's size as arguments....
C++ Write a function that accepts an array of doubles and the array's size as arguments. The function should display the contents of the array to the screen.
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The...
Write a function in JAVASCRIPT that accepts two arguments (a string array and a character). The function will check each character of the strings in the array and removes the character, regardless of case. If the first letter of the string is removed the next letter must be capitalized. Your function would return the result as one string, where each element of array is separated by comma. E.G. function([“John”,”Hannah”,”Saham”], “h”); // returns ‘Jon,Anna,Saam Use of any built in string function...
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
In this third part, write a function that accepts an array of integers and its size as arguments.
in C++In this third part, write a function that accepts an array of integers and its size as arguments. The function should create a new array that is of half size the argument array (round up the fraction if the size of the argument array is odd). The value of the first element (Element 0) of the new array should be the sum of the first two elements of the argument array (Element 0 and 1). Element 1 of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT