简体   繁体   中英

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.

The file that I want to open is Storelist.txt and contains:

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. 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:

#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}; . This variable can contain a single pointer to one array of 100 char values. 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*) . 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. You have merely assigned the pointer location of temp_item to final_list[ii] by the assignment final_list[ii] = temp_item; . I would suggest looking up strcpy or a safer version that limits the number of characters to be copied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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