C Help.

Hello,
I am trying to read integer from a .txt file and then store the integers in the allocated array using malloc(). My thought is as following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
 
 
int *read_text_deltas(char *fname, int *len){
 
FILE *fp = fopen(fname, "r");
int i, count = 0;
count = *len;
int* array = malloc(count *sizeof(int)); //Implicitly declaring library function 'malloc' with type 'void *(unsigned long)'
if (fp == NULL){
    len = &(-1); //Cannot take the address of an rvalue of type 'int'
    return NULL;
}
else{
    while(!feof(fp)){
    fscanf(fp, "%d", &array[count]);
    count++;
    }
}
for(i=0; i<count; i++){
    printf(" \n a[%d] = %d\n",i,array[i]);
}
rewind(fp);
fclose(fp);
return 0;
}



Here is my reason:

I open the file using fopen() and I go through the file by using fscanf() to count the number of integers in my file. Then I used malloc() to allocate an array of proper size.
I want to use rewind() function to go back to the beginning of the file then reads integers into my array. Close the file when I am done reading all ints. I have to return a pointer to the allocated array.
The argument "size" in "int *read_text(char *fp, int *size)" indicates a pointer to an integer which is set to the length of the allocated array.I want to set up a condition that if my file cannot be opened using fopen(), then will set my size to -1 and return NULL.
This is a part of the whole program. There are three parts: text_main text_main.c read_text.c.
I have some errors such as:

Redefinition of 'fp' with a different type: 'int' vs 'FILE *'
Use of undeclared identifier 'size'
A parameter list without types is only allowed in a function definition

How can I navigate these errors?
Any tip will be greatly appreciated. Thank you!
Last edited on
Topic archived. No new replies allowed.