reading file and saving the data in arrays without malloc with language c

Viewed 30

i need to read from a file from each line the city name ,the coutry and the number of population but the city name and the coutry are arrays i used strtok but it save in a pointer

struct Ville 
   {
       char[] nom; // Le nom ASCII
       long population;          // La population
       char[] pays;         // Le pays de la ville
   };
  FILE* fp = fopen("fich.txt", "r");
    struct Ville ville;
    if (!fp)
    {
        printf("Can't open file\n");
    }
    else
    {
        // Here we have taken size of
        // array 1024 you can modify it
        char buffer[1024];
 
        int row = 0;
        int column = 0;
 
        while (fgets(buffer, 1024, fp)) 
        {
            column = 0;
            row++;
 
            // To avoid printing of column
            // names in file can be changed
            // according to need
            if (row == 1)
                continue;
 
            // Splitting the data
            char* value = strtok(buffer, "\t");
            int iterateur = 0;
            while (value) 
            {
                
                // Column 3
                if (column == 2) 
                {
                    ville.nom[] = value;
                }

                // Column 4
                if (column == 3) 
                {
                    if ((int)*value >= 0 ||(int)*value <= 9 )
                    {
                        column ++;
                    }
                    
                }

                // Column 9
                if (column == 8) 
                {
                    ville.pays[] = value;
                }

                // Column 11
                /*if (column == 10) 
                {              
                    if ((int)value > 15000)
                    {
                        ville[iterateur].population = value;
                    }
                    
                }

                if (column == 11) 
                {
                    if (value > 15000 && ville.population < 15000 )
                    {
                        ville.population = value;
                    }  
                    
                }

                value = strtok(NULL, "\t");
                column++;
            }
 
            printf("\n");
        }
 
        // Close the file
        fclose(fp);
            printf("\nNom : %s" , ville.nom);
            printf("          code du pays : %s" , ville.pays);
            printf("          populatiom : %d" , ville.population);
        
        
    }


it doesn't work cause value is a pointer and even with

    ville.nom[] = *value

i get only the first char but i need all of them i tried also this

   int i = 0; 
   while(value != NULL) {
    ville.nom[i] = *value;
    i++;
   }

in the end nom is full with unknown caracters so how can i save value in array and integer in (population)

0 Answers
Related