What I want to do here is to read a text file containing phone numbers. For example:
01011112222
01027413565
01022223333
I want to store these phone numbers into an array for later use. Here below is my code:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fl = NULL;
char* phoneNums[10];
int i = 0;
fl = fopen("phoneNum.txt", "r");
if(fl != NULL){
char strTemp[14];
while( !feof(fl) ){
phoneNums[i] = fgets(strTemp, sizeof(strTemp), fl);
i++;
}
fclose(fl);
}
else{
printf("File does not exist");
}
return 0;
}
The problem is that whenever fgets
is called, it returns the same reference of strTemp
.
So every time it goes through the loop, it changes all value to the recent value in phoneNums
array.
I tried to declare char strTemp[14]
inside the while
loop, but it didn't work.
At this point, what could I try to solve this issue?
Thanks.