从文件分配char*数组
从代码中的文件分配数组时遇到问题。代码的目的是向函数传递一个文件名、一个整数(该整数将被设置为文件中的行数)和一个字符数组*每行一个,在函数中,文件将被打开,每行将被传递到数组中 我要打开的文件是Storelist.txt,包含:从文件分配char*数组,c,arrays,file,function,assign,C,Arrays,File,Function,Assign,从代码中的文件分配数组时遇到问题。代码的目的是向函数传递一个文件名、一个整数(该整数将被设置为文件中的行数)和一个字符数组*每行一个,在函数中,文件将被打开,每行将被传递到数组中 我要打开的文件是Storelist.txt,包含: 842B 832B 812B 848B 代码中的主要功能是: #include <stdio.h> #include <string.h> #include <stdbool.h> #include <stdlib.h>
842B
832B
812B
848B
代码中的主要功能是:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h> /* strtol */
void pass_list(int *final_legnth_list, char* filename, char* final_list[]);
main(int argc, char* argv[])
{
int store_n=0;
char* store_param= "storelist.csv";
char* store_list[100]={0};
pass_list(&store_n,store_param, store_list);
printf("STATUS: size of array [%i]\n",store_n);
int jj=0;
for(jj=0;jj<store_n;jj++){
printf("Number: %i is store: [%s]\n",jj, store_list[jj]);
}
return 0;
}
最终输出显示:
STATUS:output = [842B]
STATUS:output = [832B]
STATUS:output = [812B]
STATUS:output = [848B]
STATUS: size of array [4]
Number: 0 is store: [848B]
Number: 1 is store: [848B]
Number: 2 is store: [848B]
Number: 3 is store: [848B]
因此,它从文件中读取正确的值,但它总是以某种方式完成对文件中最终值的赋值
我认为这可能是因为数组存储的是temp_项的位置,而不是值。是否有人知道我做错了什么,以及如何获得所需的功能
final_list[ii] = temp_item; //setting the array
您正在分配局部变量的值
改为复制值:
strcpy(final_list[ii], temp_item); //setting the array
还请注意,您必须为要存储在数组中的每个字符串保留空间(使用malloc
),并在最后保留free
,这是一个简化的示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *store_list[100];
char *list[] = {
"842B",
"832B",
"812B",
"848B"
};
int i;
for (i = 0; i < 4; i++) {
store_list[i] = malloc(strlen(list[i]) + 1); /* +1 for trailing 0 */
if (store_list[i] == NULL) { /* always check the return of malloc */
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy(store_list[i], list[i]);
}
for (i = 0; i < 4; i++) {
printf("%s\n", store_list[i]);
free(store_list[i]);
}
return 0;
}
#包括
#包括
#包括
内部主(空)
{
字符*存储列表[100];
字符*列表[]={
“842B”,
“832B”,
“812B”,
“848B”
};
int i;
对于(i=0;i<4;i++){
store_list[i]=malloc(strlen(list[i])+1);/*+1表示尾部0*/
如果(store_list[i]==NULL){/*始终检查malloc的返回*/
佩罗尔(“马洛克”);
退出(退出失败);
}
strcpy(存储列表[i],列表[i]);
}
对于(i=0;i<4;i++){
printf(“%s\n”,存储列表[i]);
免费(门店列表[i]);
}
返回0;
}
我在这段代码中看到两个问题
1) store_list
变量声明为char*store_list[100]={0}代码>。此变量可以包含一个指针,指向一个包含100个char
值的数组。然后可以通过调用store\u list[jj]
来访问此变量,但除非您将数据设计为小于sizeof(char*)
,否则访问此变量是不正确的。以这种方式设计代码是可能的,但也存在许多缺陷,包括不能指望所有系统上的指针大小相同
2) 必须使用字符串复制函数将字符数据从一个内存位置复制到另一个内存位置。您仅通过赋值final_list[ii]=temp_item将temp_item
的指针位置赋值给final_list[ii]
代码>。我建议你查一下strcpy或者一个更安全的版本,限制要复制的字符数。David,谢谢你的快速回复。你有没有办法在不使用函数globel中的所有变量的情况下解决这个问题?@David Rf这是一个很好的解释。我不知道这会发生在C中。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char *store_list[100];
char *list[] = {
"842B",
"832B",
"812B",
"848B"
};
int i;
for (i = 0; i < 4; i++) {
store_list[i] = malloc(strlen(list[i]) + 1); /* +1 for trailing 0 */
if (store_list[i] == NULL) { /* always check the return of malloc */
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy(store_list[i], list[i]);
}
for (i = 0; i < 4; i++) {
printf("%s\n", store_list[i]);
free(store_list[i]);
}
return 0;
}