1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char *argv[]){
FILE * finput, * foutput;
char * line;
char ** tabLine, **tabVal;
char * fInName, * fOutName;
long int position;
int cntL, cntV;
int i;
if (argc == 3) {
fInName = argv[1];
fOutName = argv[2];
printf("input file : %s\t, output file : %s\n",fInName, fOutName);
finput = fopen(fInName, "r");
if (finput != NULL) {
fseek(finput,0,SEEK_END);
position = ftell(finput);
fseek(finput,0,SEEK_SET);
line = calloc(position,sizeof(char));
fread(line,sizeof(char),position, finput);
fclose(finput);
foutput = fopen(fOutName, "w");
if (foutput != NULL) {
fprintf(foutput,"test\n");
cntL = 0;
//decoupage par lignes
tabLine = splitString(line, '\n');
while (tabLine[cntL][0]!= '\0') {
//decoupage par ";"
tabVal = splitString(tabLine[cntL], ';');
cntV = 0;
while (tabVal[cntV][0]!= '\0') {
cntV++;
}
/*
----
fonctions sur les données recuperées.....
*/
//--------------------------
// desallocation memoire
for (i = 0;i<=cntV;i++) {
free(tabVal[i]);
tabVal[i] = NULL;
}
//--------------------------
free(tabVal);
tabVal = NULL;
cntL++;
}
for (i = cntL-1;i>=0;i--) {
free(tabLine[i]);
}
free(tabLine);
fclose(foutput);
printf("the sequence has terminated\n");
}else printf("error while opening the output file %s\n", fOutName);
free(line);
}else printf ("error while opening file :%s\n", fInName);
}else printf("the numbre of parameters is incorrect : there must be 3\n");
return(0);
}
char ** splitString(char * strLine, char strMod){
char ** tabVal;
unsigned int i, j, cnt;
i = 0; j = 0; cnt= 0;
tabVal = malloc(sizeof(char*));
while (i<strlen(strLine)) {
while (strLine[i] != strMod && strLine[i] !='\0' && strLine[i]!= '\r')
i++;
if (strLine[i] != '\r') {
tabVal = realloc(tabVal, (cnt+1) * sizeof(char*));
tabVal[cnt] = malloc((i-j)*sizeof(char));
strncpy (tabVal[cnt], strLine + j , i-j);
tabVal[cnt][i-j] = '\0';
cnt++;
i++;
j = i;
}else i = strlen(strLine);
}
tabVal = realloc(tabVal, (cnt+1) * sizeof(char*));
tabVal[cnt] = malloc(sizeof(char));
tabVal[cnt][0] = '\0';
return(tabVal);
} |
Partager