-3

Hello I have to create a function that takes as input a file and a pointer to an integer and returns an array of the numbers within the file and the length of the pointer . I created this program and have identified the problem in the code portion nuovoarray[i] = s but I do not know how to solve it .

#include <stdio.h>
#include <stdlib.h>
#include "esercizio.h"

int* leggiArray(char* nomefile, int* n){
  FILE* file = fopen(nomefile,"r");
  char* nuovoarray = (char*) malloc(sizeof(char));
  int i=0;
  char s[256];
  while(fscanf(file,"%s",s)!=EOF){
    nuovoarray[i] = s;
    i++;
    nuovoarray = realloc(nuovoarray,i*sizeof(char));
  }
}
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Oct 08 '15 at 15:31
  • 2
    read about `strcpy()` function and `return` statement. No offense, you need to have a basic book on C. – Sourav Ghosh Oct 08 '15 at 15:32
  • 1
    @SouravGhosh: `strcpy`? Surely not for an "... array of the numbers within the file ..." (which leads to several related questions, such as "why is he using `%s`?") – Jongware Oct 08 '15 at 15:33
  • @Jongware that was targeted towards the `=`, mostly. As I already mentioned, a basic C tutorial is required for OP. :) – Sourav Ghosh Oct 08 '15 at 15:34
  • `char* nuovoarray = (char*) malloc(sizeof(char));` allocates **one byte** of memory. – Weather Vane Oct 08 '15 at 15:48
  • In fact that's what I'm doing, and the program is an exercise in reality. Now anyone can fix the code? XD – Giorgio Dramis Oct 08 '15 at 15:48
  • @WeatherVane thank you i do `char* nuovoarray = (char*) malloc(sizeof(char)*256);` but I still have this warning: `c99 -c esercizio.c esercizio.c: In function ‘leggiArray’: esercizio.c:11:19: warning: assignment makes integer from pointer without a cast [-Wint-conversion] nuovoarray[i] = s; ^ c99 -o test esercizio.o test.o libtest.o -lm` – Giorgio Dramis Oct 08 '15 at 15:52
  • The first time `nuovoarray = realloc(nuovoarray,i*sizeof(char));` is executed it doesn't allocate any more than the **one byte** you already have, and so the subsequent `nuovoarray[i] = s;` will write data into the abyss. – Weather Vane Oct 08 '15 at 15:52
  • @GiorgioDramis `nuovoarray[i] = s;` tries to write an array (pointer) into a single character. – Weather Vane Oct 08 '15 at 15:57
  • 1
    You haven't posted your *actual* code have you? You only have one pointer to integer, and you don't do anything with it. Over and out. – Weather Vane Oct 08 '15 at 15:58

1 Answers1

0

There are more than one ways to solve the problem. Here's one.

  1. Create a function that goes through the file and returns the number of integers present in the file.

  2. Allocate memory based on that number.

  3. Create a second function in which the integers are read from the file and stored in the allocated memory.

int getNumberOfIntegers(char const* file)
{
   int n = 0;
   int number;
   FILE* fptr = fopen(file, "r");
   if ( fptr == NULL )
   {
      return n;
   }

   while ( fscanf(fptr, "%d", &number) == 1 )
   {
      ++n;
   }

   fclose(fptr);
   return n;
}

int readIntegers(char const* file, int* numbers, int n)
{
   int i = 0;
   int number;
   FILE* fptr = fopen(file, "r");
   if ( fptr == NULL )
   {
      return i;
   }

   for ( i = 0; i < n; ++i )
   {
      if ( fscanf(fptr, "%d", &numbers[i]) != 1 )
      {
         return i;
      }
   }

   return i;
}

int main()
{
   int n1;
   int n2;
   int* numbers = NULL;
   char const* file = <some file>;

   // Get the number of integers in the file.
   n1 = getNumberOfIntegers(file);

   // Allocate memory for the integers.
   numbers = malloc(n1*sizeof(int));
   if ( numbers == NULL )
   {
      // Deal with malloc problem.
      exit(1);
   }

   // Read the integers.
   n2 = readIntegers(file, numbers, n1);
   if ( n1 != n2 )
   {
      // Deal with the problem.
   }

   // Use the numbers
   // ...
   // ...

   // Deallocate memory.
   free(numbers);

   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270