Question

In: Computer Science

FILE *fin = NULL; fin = fopen(in.txt, "r"); int *arr = (int *) malloc (sizeof (int)...

FILE *fin = NULL;
fin = fopen(in.txt, "r");

int *arr = (int *) malloc (sizeof (int) * fin);

*****************************************************

If I want to open a in.txt, only the integer in the file, is the malloc code correct?

What if the in.txt includes the string and integer, how can I use the malloc?  

Thanks

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

The code above is indeed not correct. There is 1 major flaw that the fin can't be used to pass it as size of array. We don't know the file pointer size and we are converting it to the size of array.

For example file with only 2 integers as

1 2

It will return size a much larger and we will be wasting the memory.

We can do 1 thing which is we have to first count the number of integers in the file and then we will use it as

int *arr = (int *) malloc (sizeof (int) * ct); where ct is the count of integers

Below is the full code in C

FILE *fin = NULL;
fin = fopen(in.txt, "r");

int ct = 0;

  fscanf (file, "%d", &i);    
  while (!feof (file))
    {  
ct++;
      //printf ("%d ", i);
      fscanf (file, "%d", &i);      
    }
  fclose (file); 

int *arr = (int *) malloc (sizeof (int) * ct);

Kindly revert for any queries

Thanks.


Related Solutions

   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
for C++ pointers: a. Given int arr [10], *ap = &arr[2]; what is the result of...
for C++ pointers: a. Given int arr [10], *ap = &arr[2]; what is the result of (ap - arr)? b. Given int val = 10, *x = &val, *y; y = x; *y=100; what is the output of cout << *x << *y;? c. Given int arr [10], *ap = arr; what element does ap point to after ap +=2; ?
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that...
Write a MIPS function using a MARS 4.5 complier named void makeRandomArray(int arr[], int size) that generates and stores a specified number of random numbers each of whose values are between 0 and 1000 in an array
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
#include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");...
#include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to the program above what...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT