I am having a problem with storing numbers from a file to a 1D array. I want to store the numbers without giving size to the array int numberArray[11]. These are numbers I am trying to read from an input.txt file
200 53 65 98 183 37 122 14 124 65 67
int main()
{
FILE *myFile;
myFile = fopen("input.txt", "r");
//read file into array
int numberArray[11];
int i;
if (myFile == NULL)
{
printf("Error Reading File\n");
exit (0);
}
//Store number to array
for (i = 0; i < 11; i )
{
fscanf(myFile, "%d,", &numberArray[i] );
}
/*
//Print array
for (i = 0; i < 11; i ){
printf("%d\n", numberArray[i]);
}
*/
//Close the file
fclose(myFile);
//FCFS the array
FCFS(numberArray);
return 0;
}
As you can see in my code, I have created an array of int size of 11 because there are 11 numbers that I am trying to read into the array. Let's just say I don't know the numbers inside the input file. How do I allocate the array?
P.s my code works but I just want to know whats the efficient approach when it comes to these kind of situations.
CodePudding user response:
There are 2 classic solutions 2 this
First is to have a number in the file that indicates how many numbers there are and to size the array using VLA. ie
int num;
fscanf(myFile, "%d", &num);
int numbers[num];
second is to dynamically allocate space using malloc - say 5 to start - and keep doubling the size of the array using realloc until you reach end of file
int size = 5;
int count = 0;
int *numbers = malloc(size * sizeof(int));
int numin;
while (fscanf(myFile, "%d", &numin));
if(count== size){
size *= 2;
numbers = realloc(numbers, size * sizeof(int));
}
count ;
numbers[count] = numin;
}
CodePudding user response:
The question may be a bit cloudy but I think you want to know how do you manage to read variable numbers of data from a file. There are a number of ways to manage that actually...some of them are:
- Read the file and only count the numbers before allocating (without reading data). then allocate the array and read the data.
- Dynamically allocate and reallocate twice the size of array every time the array comes to be short.
- Use a List :)
Approach 1:
int count = 0;
int var;
while(fscanf(myFile, "%d", &var) == 1){ //included only a snippet instead of full code
count ;
}
int numberArray[count];
//iterate again and read the data
Approach 2:
int curr_size = 2; //you can take any positive integer, but be wary of heap overflow(large allocation)
int i = 0;
int* varr = malloc (sizeof(int) * curr_size);
while(fscanf(myFile, "%d", &varr[i ]) == 1){
if(i == curr_size){
curr_size *= 2; //you can do instead of *, depends on what you're dealing with... '*' can sometimes lead to unnecessary allocation of big chunks of memory
int* temp = realloc(varr, sizeof(int)*curr_size);
if(temp == NULL) { printf("Error allocating"); return -1;}
free(varr);
varr = temp;
}
}
// 'i' will give you total numbers read you can realloc if you like
//if you use it for reading string be sure to account for '\0' //if you use it for reading string be sure to account for '\0'
Approach 3: Use List Data structure to store variable number of data. that will be pretty easier
Approach 2 is one of most used ways between programmers to read variable sized strings(char sequences) from a source in c.
Approach 1 will be easier for beginners.
Approach 3 will be easier for reading integers.
Trying all will help you grow.