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!
Mettre une seule valeur dans un tableau? –
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. –
'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