Problème C ouverture de fichier
wolveryne
-
fiddy Messages postés 11653 Statut Contributeur -
fiddy Messages postés 11653 Statut Contributeur -
Bonjour,
voici mon code
je souhaite ouvrir un fichier CSV et le copier dans un tableau dynamique
cependant j'arrive à compiler mon code mais celuici plante à l"execution
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <string.h>
void main (){
char nomFichier[255];
int i=0;
int j=0;
int l=1;
int c=1;
int s=0;
char *chaine[255];
float***data;
FILE *F;
affichagedemarrage();
printf("Chemin d'accès du fichier à analyser ?,\n )");
fflush(stdin);
scanf("%s",nomFichier);
F=fopen(nomFichier,"r");
if (F != NULL){
*data=(float**)malloc(sizeof(float));
**data=(float*)malloc(sizeof(float));
for(i=0;((fgetc(F))!=NULL);i++){
switch(fgetc(F)){
case'-':s=1;break;
case';':c+=1;realloc(**data,(c*(sizeof(float))));*data[(l-1)][(c-1)]=(atof(chaine));break;
case'\0':l+=1;c=1;realloc(*data,(l*(sizeof(float))));realloc(**data,(c*(sizeof(float))));*data[(l-1)][(c-1)]=(atof(chaine));break;
case'EOF':*data[(l-1)][(c-1)]=(atof(chaine));break;
default:strcat(chaine,(fgetc(F)));
}
}
for(i=0;i<l;i++){
for(j=0;j<c;j++){
printf("data %d %d %f\n",i,j,data[i][j]);}}}
fclose(F);}
quelqu'un peut il m'aider ?
voici mon code
je souhaite ouvrir un fichier CSV et le copier dans un tableau dynamique
cependant j'arrive à compiler mon code mais celuici plante à l"execution
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <string.h>
void main (){
char nomFichier[255];
int i=0;
int j=0;
int l=1;
int c=1;
int s=0;
char *chaine[255];
float***data;
FILE *F;
affichagedemarrage();
printf("Chemin d'accès du fichier à analyser ?,\n )");
fflush(stdin);
scanf("%s",nomFichier);
F=fopen(nomFichier,"r");
if (F != NULL){
*data=(float**)malloc(sizeof(float));
**data=(float*)malloc(sizeof(float));
for(i=0;((fgetc(F))!=NULL);i++){
switch(fgetc(F)){
case'-':s=1;break;
case';':c+=1;realloc(**data,(c*(sizeof(float))));*data[(l-1)][(c-1)]=(atof(chaine));break;
case'\0':l+=1;c=1;realloc(*data,(l*(sizeof(float))));realloc(**data,(c*(sizeof(float))));*data[(l-1)][(c-1)]=(atof(chaine));break;
case'EOF':*data[(l-1)][(c-1)]=(atof(chaine));break;
default:strcat(chaine,(fgetc(F)));
}
}
for(i=0;i<l;i++){
for(j=0;j<c;j++){
printf("data %d %d %f\n",i,j,data[i][j]);}}}
fclose(F);}
quelqu'un peut il m'aider ?
A voir également:
- Problème C ouverture de fichier
- Fichier bin - Guide
- Fichier epub - Guide
- Fichier rar - Guide
- Comment réduire la taille d'un fichier - Guide
- Fichier .dat - Guide
1 réponse
Salut,
Lorsque tu déclares ton tableau dynamique, faudrait que tu précises la longueur de ton allocation. Exemple malloc(15*sizeof(float)).
Et lorsque tu compiles ton code, il y a pleins de warning. Je te laisse corriger ;)
Lorsque tu déclares ton tableau dynamique, faudrait que tu précises la longueur de ton allocation. Exemple malloc(15*sizeof(float)).
Et lorsque tu compiles ton code, il y a pleins de warning. Je te laisse corriger ;)
test.c: In function «main": test.c:24: attention : comparaison entre un pointeur et un entier test.c:27: attention : passing argument 1 of «atof" from incompatible pointer type test.c:28: attention : passing argument 1 of «atof" from incompatible pointer type test.c:29:11: attention : constante caractère multi-caractères test.c:29: attention : passing argument 1 of «atof" from incompatible pointer type test.c:30: attention : passing argument 1 of «strcat" from incompatible pointer type test.c:30: attention : passing argument 2 of «strcat" makes pointer from integer without a cast test.c:6: attention : return type of «main" is not «int" test.c:27: attention : ignoring return value of «realloc", declared with attribute warn_unused_result test.c:28: attention : ignoring return value of «realloc", declared with attribute warn_unused_result test.c:28: attention : ignoring return value of «realloc", declared with attribute warn_unused_result