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
.
问题是每当调用fgets时,它都会返回strTemp的相同引用。
So every time it goes through the loop, it changes all value to the recent value in phoneNums
array.
因此,每次循环时,它都会将所有值更改为phoneNums数组中的最新值。
I tried to declare char strTemp[14]
inside the while
loop, but it didn't work.
我试图在while循环中声明char strTemp [14],但它不起作用。
At this point, what could I try to solve this issue?
在这一点上,我可以尝试解决这个问题?
Thanks.
谢谢。
2 个解决方案
#1
1
Do the below changes to get the exact result.
执行以下更改以获得准确的结果。
Change the strTemp variable to pointer variable.
将strTemp变量更改为指针变量。
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.
如果您喜欢这样,它将每次创建一个新的内存,以便将值存储在不同的位置。所以它不能在同一个位置覆盖。
#2
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];
这里为char * phoneNums动态分配内存[14];
pnoneNums=(char **)malloc(14*n); // where n is the numbers of phone numbers
#1
1
Do the below changes to get the exact result.
执行以下更改以获得准确的结果。
Change the strTemp variable to pointer variable.
将strTemp变量更改为指针变量。
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.
如果您喜欢这样,它将每次创建一个新的内存,以便将值存储在不同的位置。所以它不能在同一个位置覆盖。
#2
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];
这里为char * phoneNums动态分配内存[14];
pnoneNums=(char **)malloc(14*n); // where n is the numbers of phone numbers