Thread: Array of struct to csv file

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    1

    Array of struct to csv file

    I have a code where the user can change/add information to a array struct and parse it to a csv file. Since I'm really new to programming I truly have basic knowledge and no matter how much I research.

    So this code is meant to do the following: It'll ask the user for a city and if it finds the line with said city the user could add information to a struct regarding that line and then save it to the csv file. The code shows no error however it stops running after I input a city name.

    Here is the code:
    Code:
    struct wea_array{    //int id_cidade[100];
        char cidade[100];
        //char concelho[100];
        //char distrito[100];
        int temperatura[100];
        int temp_max[100];
        int temp_min[100];
        int press[100];
        int humi[100];
        
    };
    
    
    const char* getfield(char* line, int num)
    {
        const char* tok;
        for (tok = strtok(line, ",");
                tok && *tok;
                tok = strtok(NULL, ","))
        {
            if (!--num)
                return tok;
        }
        return NULL;
    }
    
    
    
    
    void structwea(){
        struct wea_array* wea = malloc(sizeof(struct wea_array));
        int i;
        for (i=0;i<152;i++){
                    printf("Introduza o valor da temperatura:");
                    scanf("%s",wea[i].temperatura);
                    printf("Introduza o valor da temperatura maxima:");
                    scanf("%s",wea[i].temp_max);
                    printf("Introduza o valor da temperatura minima:");
                    scanf("%s",wea[i].temp_min) ;
                    printf("Introduza o valor da pressap:");
                    scanf("%s",wea[i].press);
                    printf("Introduza o valor da humidade:");
                    scanf("%s",wea[i].humi);
        }
    }
        
    
    
    void edit(){
        char input[100];
        char id_input[100];
        char id_city[100];
        char cidade[100];
        char concelho[100];
        char distrito[100];
        int i=0;
        
        FILE* stream;
        if (stream != NULL) {
                    printf("Introduce city to edit information:");
                    scanf ("%[^\n]%*c", input);
                    char line[1024];
                while (fgets(line, 1024, stream))
                {
                    char *tmp = strdup(line);
                    if (i > 0) { 
                        strcpy(id_cidade, strtok(tmp, ",\n"));  
                        strcpy(cidade, strtok(NULL, ",\n"));  
                        strcpy(concelho, strtok(NULL, ",\n"));  
                        strcpy(distrito, strtok(NULL, ",\n"));  
                        //printf("%s - %s - %s - %s \n", id_cidade, cidade, concelho, distrito);
    
    
                        if (strcmp(cidade,input)==0 || strcmp(concelho,input)==0 || strcmp(distrito,input)==0){
                            structwea();
                            strcpy(id_input, id_cidade);
                        }
                    }
                    i++;      
                    free(tmp);
                }
        }
        else {
            printf("INVALIDO");
        }
    }    
        
    int main(){
        edit();
    }
    Any help is appreciated, thanks in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > struct wea_array* wea = malloc(sizeof(struct wea_array));
    This needs to be
    struct wea_array* wea = malloc(sizeof(struct wea_array) * 152);
    to cope with the amount of memory that the for loop following tries to access.

    > void structwea()
    You should make this return (at least) wea, otherwise you just lose all the work you just did.

    > FILE* stream;
    > if (stream != NULL)
    Stream is uninitialised here, so comparing it with NULL is anyone's guess.
    Is this where you tell us you've snipped out 'obvious' code for our convenience?

    > strcpy(id_cidade, strtok(tmp, ",\n"));
    It might be, because this variable isn't even declared.
    Perhaps it's the untranslated version of id_city.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-21-2017, 10:48 AM
  2. Problem with reading a file into array of struct
    By Lozy in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2014, 08:29 AM
  3. reading the content of a file into an array of struct
    By m_programmer in forum C Programming
    Replies: 6
    Last Post: 09-20-2012, 07:29 AM
  4. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  5. Writing an array of struct to file
    By stellastarr in forum C Programming
    Replies: 10
    Last Post: 03-25-2006, 06:59 PM

Tags for this Thread