2013-04-29 11 views
-1

Je suis là pour demander de l'aide. Je veux lire un .txt avec ce format:Lire le fichier .txt à array et split

20 
20 

1 2 
1 2 
3 4 2 2 
5 4 1 1 1 1 
4 2 1 3 1 
3 2 1 7 
4 1 3 3 2 
2 10 1 
5 8 2 1 1 1 
3 8 1 1 
4 9 1 1 1 
4 3 8 1 1 
2 2 12 
4 3 2 4 3 
4 2 2 4 3 
3 3 4 2 
2 3 2 
3 2 1 2 
2 5 4 
2 4 2 

1 6 
1 9 
2 9 3 
4 4 4 1 1 
3 1 2 8 
3 4 9 1 
4 2 7 1 1 
2 7 1 
3 1 5 1 
3 8 5 1 
4 1 3 7 1 
5 1 2 2 3 1 
5 2 1 2 2 1 
3 2 4 1 
4 2 1 4 1 
6 1 1 1 1 2 1 
4 1 2 1 2 
3 8 1 2 
1 4 
1 2 

J'ai un morceau de code écrit. Il copie chaque ligne du fichier .txt (je ne veux pas lire les 20) et l'imprime directement dans le cmd quand on l'exécute. Voici le code:

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#include <string.h> 



int main(int argc, char *argv[]){ 

    FILE*fp; 
    int i=0, dimension[2]; 
    char row[128]; 
    int n_rows, n_columns; 
    char *v_row, *v_col; 


    fp=fopen(argv[1],"r"); 


    if(fp == NULL){ 
     printf("File not found\n"); 
     exit(0); 
       } 
    while(i<3){ 

     fgets(row, 128, fp); 
     sscanf(row, "%d", &dimension[i]); 
     i++; 
     } 
    n_rows=dimension[0]; 
    n_columns=dimension[1]; 

    printf("dimension: %d x %d\n\n", n_rows, n_columns); 
    v_row=malloc((n_rows+1)*sizeof(char)); 
    v_col=malloc((n_columns+1)*sizeof(char)); 

    for(i=0; i<n_rows; i++){ 
     fgets(row, 128, fp); 
     printf("row: %s\n", row); 


     } 
    for(i=0;i<n_columns; i++){ 
     fgets(row, 128, fp); 
     printf("row: %s\n", row); 
     sscanf(row,"%s", &v_col[i]); 
    } 


    fclose(fp); 

    exit(-1); 
} 

Mais ce n'est pas mon objectif final. Ce que je veux vraiment faire est de mettre chaque ligne dans un tableau, en divisant le premier nombre en un tableau et le reste de la ligne en un autre tableau, mais malheureusement, je ne peux pas trouver un moyen de le faire.

+0

Mettre une seule valeur dans un tableau? –

+0

Si vous "mmap" votre fichier, la vérification et la lecture seront plus simples: vous travaillerez avec comme avec une grande chaîne, OS fera tout le travail en arrière-plan. Pour diviser des données en portions, vous pouvez utiliser des fonctions telles que 'strtok' et' strchr'. Et je vous recommande de ne pas utiliser 'scanf' pour obtenir des valeurs entières, mais d'utiliser' strtol' qui est plus flexible et permet de contrôler les données. –

+0

'while (i <3) {... sscanf (ligne,"% d ", & dimension [i]); } 'Il n'y a que deux éléments dans le tableau de dimension; leurs indices sont '0' et' 1'. – wildplasser

Répondre

2

Vous avez effectivement eu la réponse vous-même. Utilisez sscanf() pour faire votre travail

Exemple ::

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    char *tmp = "123 456 789 123 4652 128793"; 
    char arr[5], arr2[30]; 
    printf("tmp::\t%s\n",tmp); 
    sscanf(tmp,"%s %[^\n]s",arr,arr2); 
    printf("arr::\t%s\n",arr); 
    printf("arr2::\t%s\n",arr2); 
    return 0; 
} 

Sortie:

tmp:: 123 456 789 123 4652 128793 
arr:: 123 
arr2:: 456 789 123 4652 128793 

EDIT ::

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define LENGTH 42 // max length of line 

int main() 
{ 
    FILE *fp; // for the file 
    int Nrow, Ncol; 
    int loop; 
    char line[LENGTH]; 
    char **arrRows_1stDim, **arrRows_2ndDim; 
    char **arrCols_1stDim, **arrCols_2ndDim; 
    char arr[20], arr2[20]; 
    fp = fopen("testDATA.txt","r"); // I saved your file with this name 
    if(fp == NULL) 
    { 
     perror("testDATA.txt"); 
     return -1; 
    } 
    fscanf(fp,"%d", &Nrow); 
    fscanf(fp,"%d", &Ncol); 
    printf("Nrow::%d,\tNcol::%d\n",Nrow,Ncol); 
    arrRows_1stDim = malloc(sizeof(char*) * Nrow); 
    arrRows_2ndDim = malloc(sizeof(char*) * Nrow); 
    arrCols_1stDim = malloc(sizeof(char*) * Ncol); 
    arrCols_2ndDim = malloc(sizeof(char*) * Ncol); 
    // check if our malloc() was allocated 
    if(arrRows_1stDim == NULL || arrRows_2ndDim == NULL || arrCols_1stDim == NULL || arrCols_2ndDim == NULL) 
    { 
     fprintf(stderr,"Couldn't malloc()"); 
     return -1; 
    } 
    printf("Reading Rows...\n\n"); 
    for(loop=0;loop<Nrow;loop++) 
    { 
     fgets(line,LENGTH,fp); 
     if(strlen(line)==1) 
      loop--; 
     else 
     { 
      sscanf(line,"%s %[^\n]s",arr,arr2); 
      //printf("Arr::%s,\tArr2::%s\n",arr,arr2); 
      arrRows_1stDim[loop] = malloc(sizeof(char) * (strlen(arr) + 1)); 
      arrRows_2ndDim[loop] = malloc(sizeof(char) * (strlen(arr2) + 1)); 
      // check if our malloc() was allocated 
      if(arrRows_1stDim[loop] == NULL || arrRows_2ndDim[loop] == NULL) 
      { 
       fprintf(stderr,"Couldn't malloc()"); 
       return -1; 
      } 
      strcpy(arrRows_1stDim[loop],arr); 
      strcpy(arrRows_2ndDim[loop],arr2); 
      //printf("arrRows_1stDim[loop]::%s,\tarrRows_2ndDim[loop]::%s\n",arrRows_1stDim[loop],arrRows_2ndDim[loop]); 
     } 
    } 
    printf("\nReading Cols...\n\n"); 
    for(loop=0;loop<Ncol;loop++) 
    { 
     fgets(line,LENGTH,fp); 
     if(strlen(line)==1) 
      loop--; 
     else 
     { 
      sscanf(line,"%s %[^\n]s",arr,arr2); 
      //printf("Arr::%s,\tArr2::%s\n",arr,arr2); 
      arrCols_1stDim[loop] = malloc(sizeof(char) * (strlen(arr) + 1)); 
      arrCols_2ndDim[loop] = malloc(sizeof(char) * (strlen(arr2) + 1)); 
      // check if our malloc() was allocated 
      if(arrCols_1stDim[loop] == NULL || arrCols_2ndDim[loop] == NULL) 
      { 
       fprintf(stderr,"Couldn't malloc()"); 
       return -1; 
      } 
      strcpy(arrCols_1stDim[loop],arr); 
      strcpy(arrCols_2ndDim[loop],arr2); 
     } 
    } 
    printf("Let's see whether we read correctly or not...\n"); 
    printf("Printing Rows...\n"); 
    for(loop=0;loop<Nrow;loop++) 
    { 
     printf("Rows_1stArray:: %s,\t Rows_2ndArray:: %s\n",arrRows_1stDim[loop],arrRows_2ndDim[loop]); 
    } 
    printf("\nPrinting Cols...\n"); 
    for(loop=0;loop<Ncol;loop++) 
    { 
     printf("Rows_1stArray:: %s,\t Rows_2ndArray:: %s\n",arrCols_1stDim[loop],arrCols_2ndDim[loop]); 
    } 
    printf("\nFree Allocated Memory...\n"); 
    // free inner level 
    for(loop=0;loop<Nrow;loop++) 
    { 
     free(arrRows_1stDim[loop]); 
     free(arrRows_2ndDim[loop]); 
    } 
    // free outer level 
    free(arrRows_1stDim); 
    free(arrRows_2ndDim);   
    for(loop=0;loop<Ncol;loop++) 
    { 
     free(arrCols_1stDim[loop]); 
     free(arrCols_2ndDim[loop]); 
    } 
    free(arrCols_1stDim); 
    free(arrCols_2ndDim); 
    fclose(fp); 
    printf("Done!\n"); 
    return 0; 
} 

Sortie ::

Nrow::20,  Ncol::20 
Reading Rows... 


Reading Cols... 

Let's see whether we read correctly or not... 
Printing Rows... 
Rows_1stArray:: 1,  Rows_2ndArray:: 2 
Rows_1stArray:: 1,  Rows_2ndArray:: 2 
Rows_1stArray:: 3,  Rows_2ndArray:: 4 2 2 
Rows_1stArray:: 5,  Rows_2ndArray:: 4 1 1 1 1 
Rows_1stArray:: 4,  Rows_2ndArray:: 2 1 3 1 
Rows_1stArray:: 3,  Rows_2ndArray:: 2 1 7 
Rows_1stArray:: 4,  Rows_2ndArray:: 1 3 3 2 
Rows_1stArray:: 2,  Rows_2ndArray:: 10 1 
Rows_1stArray:: 5,  Rows_2ndArray:: 8 2 1 1 1 
Rows_1stArray:: 3,  Rows_2ndArray:: 8 1 1 
Rows_1stArray:: 4,  Rows_2ndArray:: 9 1 1 1 
Rows_1stArray:: 4,  Rows_2ndArray:: 3 8 1 1 
Rows_1stArray:: 2,  Rows_2ndArray:: 2 12 
Rows_1stArray:: 4,  Rows_2ndArray:: 3 2 4 3 
Rows_1stArray:: 4,  Rows_2ndArray:: 2 2 4 3 
Rows_1stArray:: 3,  Rows_2ndArray:: 3 4 2 
Rows_1stArray:: 2,  Rows_2ndArray:: 3 2 
Rows_1stArray:: 3,  Rows_2ndArray:: 2 1 2 
Rows_1stArray:: 2,  Rows_2ndArray:: 5 4 
Rows_1stArray:: 2,  Rows_2ndArray:: 4 2 

Printing Cols... 
Rows_1stArray:: 1,  Rows_2ndArray:: 6 
Rows_1stArray:: 1,  Rows_2ndArray:: 9 
Rows_1stArray:: 2,  Rows_2ndArray:: 9 3 
Rows_1stArray:: 4,  Rows_2ndArray:: 4 4 1 1 
Rows_1stArray:: 3,  Rows_2ndArray:: 1 2 8 
Rows_1stArray:: 3,  Rows_2ndArray:: 4 9 1 
Rows_1stArray:: 4,  Rows_2ndArray:: 2 7 1 1 
Rows_1stArray:: 2,  Rows_2ndArray:: 7 1 
Rows_1stArray:: 3,  Rows_2ndArray:: 1 5 1 
Rows_1stArray:: 3,  Rows_2ndArray:: 8 5 1 
Rows_1stArray:: 4,  Rows_2ndArray:: 1 3 7 1 
Rows_1stArray:: 5,  Rows_2ndArray:: 1 2 2 3 1 
Rows_1stArray:: 5,  Rows_2ndArray:: 2 1 2 2 1 
Rows_1stArray:: 3,  Rows_2ndArray:: 2 4 1 
Rows_1stArray:: 4,  Rows_2ndArray:: 2 1 4 1 
Rows_1stArray:: 6,  Rows_2ndArray:: 1 1 1 1 2 1 
Rows_1stArray:: 4,  Rows_2ndArray:: 1 2 1 2 
Rows_1stArray:: 3,  Rows_2ndArray:: 8 1 2 
Rows_1stArray:: 1,  Rows_2ndArray:: 4 
Rows_1stArray:: 1,  Rows_2ndArray:: 2 

Free Allocated Memory... 
Done! 
+0

Binayaka Chakraborty merci, mais avec ce que je peux sauver que deux parties divisées en deux tableaux différents, pour les travailler plus tard? –

+0

Oui, vous pouvez, à condition que les deux tableaux sont accessibles dans votre programme plus tard. Mais, vous savez que chaque boucle que vous faites écrasera les tableaux correctement? Si vous voulez une solution alternative, je vous conseille d'opter pour un tableau 2D, comme ARR [N] [DATA], où N est le nombre de lignes pour ligne ou col, et DATA est évidemment le segment de données montré ci-dessus :) –

+0

ok , et avec le premier code ci-dessus comment le codez-vous? –