简体   繁体   English

从文件分配一个char *数组

[英]Assigning a char* array from a file

I am having problems assigning an array from a file in my code. 我在从代码中的文件分配数组时遇到问题。 The aim of the code is to pass to a function a filename, an integer which will be set as the number of lines in the file and an array of char* one for each line, and within the function the file will be opened and each line passed into the array. 该代码的目的是将一个文件名,一个整数(该整数将设置为文件中的行数)和一个char *数组(每行一个)设置给函数,并且在函数内将打开该文件,行传递到数组中。

The file that I want to open is Storelist.txt and contains: 我要打开的文件是Storelist.txt,其中包含:

842B
832B
812B
848B

The main function in the code is: 代码中的主要功能是:

#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;
}

and finally the function is: 最后的功能是:

void pass_list(int *final_legnth_list, char* filename, char* final_list[]){
    FILE *temp_file;  //opening the file
    temp_file = fopen (filename, "rt");
    int ii=0;
    if (temp_file!=NULL){
        char temp_line[30]; 
        char temp_item[30];
        while(fgets(temp_line, 30, temp_file) != NULL){ //looping over the lines
            sscanf(temp_line,"%s",temp_item);   //getting the value without the end line
            printf("STATUS:output =  [%s]\n",temp_item);
            final_list[ii] = temp_item;  //setting the array
            ii++;
        }
        (*final_legnth_list) = ii;
    }
}

The final output shows: 最终输出显示:

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]

So it is reading from the file the correct values, but somehow it always finishes assigning to the final value from the file. 因此它正在从文件中读取正确的值,但是总会以某种方式完成从文件中分配给最终值的过程。

I think this maybe due to the array is storing the position of temp_item, opposed to the value. 我认为这可能是由于数组存储了temp_item的位置,而不是值。 Does anyone have an idea of what I have done wrong and how to get the desired functionality? 有谁知道我做错了什么以及如何获得所需的功能?

final_list[ii] = temp_item;  //setting the array

You are assigning the value of a local variable 您正在分配局部变量的值

Copy the value instead: 复制值:

strcpy(final_list[ii], temp_item);  //setting the array

Also note that you have to reserve space (using malloc ) for each string you want to store in the array and free at the very end, a simplified example: 还要注意,您必须为要存储在数组中的每个字符串保留空间(使用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;
}

There are two problems I see in this code. 我在这段代码中看到两个问题。

1) The store_list variable is declared as char* store_list[100]={0}; 1)将store_list变量声明为char* store_list[100]={0}; . This variable can contain a single pointer to one array of 100 char values. 此变量可以包含一个指向100个char值的数组的指针。 Accessing this variable by calls such as store_list[jj] are then possible, but incorrect unless you have designed your data to be smaller than sizeof(char*) . 然后可以通过诸如store_list[jj]类的调用来访问此变量,但是除非您将数据设计为小于sizeof(char*) ,否则它是不正确的。 Designing code this way is possible, but there are many pitfalls including the fact that you may not be able to count on pointers being the same size on all systems. 以这种方式设计代码是可能的,但是存在许多陷阱,包括您可能无法在所有系统上依靠大小相同的指针这一事实。

2) You must use a string-copy function to copy character data from one memory location to another. 2)必须使用字符串复制功能将字符数据从一个存储位置复制到另一个存储位置。 You have merely assigned the pointer location of temp_item to final_list[ii] by the assignment final_list[ii] = temp_item; 您已分配仅仅的指针位置temp_itemfinal_list[ii]由分配final_list[ii] = temp_item; . I would suggest looking up strcpy or a safer version that limits the number of characters to be copied. 我建议查找strcpy或更安全的版本,以限制要复制的字符数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM