簡體   English   中英

從文件分配一個char *數組

[英]Assigning a char* array from a file

我在從代碼中的文件分配數組時遇到問題。 該代碼的目的是將一個文件名,一個整數(該整數將設置為文件中的行數)和一個char *數組(每行一個)設置給函數,並且在函數內將打開該文件,行傳遞到數組中。

我要打開的文件是Storelist.txt,其中包含:

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

最后的功能是:

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

最終輸出顯示:

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_item的位置,而不是值。 有誰知道我做錯了什么以及如何獲得所需的功能?

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

我在這段代碼中看到兩個問題。

1)將store_list變量聲明為char* store_list[100]={0}; 此變量可以包含一個指向100個char值的數組的指針。 然后可以通過諸如store_list[jj]類的調用來訪問此變量,但是除非您將數據設計為小於sizeof(char*) ,否則它是不正確的。 以這種方式設計代碼是可能的,但是存在許多陷阱,包括您可能無法在所有系統上依靠大小相同的指針這一事實。

2)必須使用字符串復制功能將字符數據從一個存儲位置復制到另一個存儲位置。 您已分配僅僅的指針位置temp_itemfinal_list[ii]由分配final_list[ii] = temp_item; 我建議查找strcpy或更安全的版本,以限制要復制的字符數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM