0

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.

barak manos
  • 29,648
  • 10
  • 62
  • 114
Jun1987
  • 15
  • 3
  • You need to dynamically allocate an array of `char` arrays (unless you know the exact number of lines in the file). – barak manos Jul 24 '14 at 07:48
  • what's gonna be the simple way for that? should I pre-process to count the number of phone numbers in a file?? – Jun1987 Jul 24 '14 at 08:20
  • I don't know Jun1987. You down-voted my answer (which works, by the way; I've tested it), so I removed it!!! – barak manos Jul 24 '14 at 08:21
  • you see, I can't even do up-vote or down-vote since I have only 5 reputation. – Jun1987 Jul 24 '14 at 08:23
  • No problem Jun1987. I'll add my answer (which checks the file size first and then preallocates the `phoneNums` array according to the number of lines). You can try it, and I'll remove it later. – barak manos Jul 24 '14 at 08:24
  • `while( !feof(fl) ) ...` is incorrect. Read more about the "EOF anti-pattern" here: http://stackoverflow.com/questions/5431941 and http://drpaulcarter.com/cs/common-c-errors.php#4.2 – Michael Burr Jul 24 '14 at 08:30

2 Answers2

1

Do the below changes to get the exact result.

Change the strTemp variable to pointer variable.

 char *strTemp;

Inside while allocate the dynamic memory for the variable.

 strTemp=malloc(14);
 phoneNums[i]=fgets(strTemp,14,fl);

If you do like this it will create a new memory for each time so the value is stored in the different location. So it can't overwrite in the same location.

Chandru
  • 1,306
  • 13
  • 21
0

Hope this will help you

#include <stdio.h>
#include<string.h>
#include <stdlib.h>
int main(){
    FILE *fl = NULL;
    char    phoneNums[3][14]; // you didn't allocate memory here. i am using static memory(for 3 phone numbers)
    int i = 0,j;
    fl = fopen("phoneNum.txt", "r");

    if(fl != NULL){
            char strTemp[14];

            while( fgets(strTemp, sizeof(strTemp), fl) ){
                    strcpy(phoneNums[i],strTemp); // you need to string copy function to copy one string to another string
                    i++;
            }
            fclose(fl);
    }
    else{
            printf("File does not exist");
    }
    for(j=0;j<i;j++) // i am printing the array content
            printf("%s\n",phoneNums[j]);

    return 0;
}

Here the dynamic allocation of memory for char *phoneNums[14];

pnoneNums=(char **)malloc(14*n); // where n is the numbers of phone numbers
Sathish
  • 3,740
  • 1
  • 17
  • 28