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
|
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
#ifndef WIN32
#define WIN32
#endif
#ifndef RENFOTO
#define RENFOTO
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h> // perror
#include <strings.h> // strlen, strcpy,...
#include <string.h> // bzero...
#include <unistd.h> // R_OK
#include <fcntl.h> // O_RDONLY O_WRONLY
#endif
#ifdef WIN32
#include <windows.h>
#endif
#define filePath "C:\\blabla.txt"
int existFile(char *fichier);
int myRealloc(void ** ppData, size_t newSize);
void loadTextFile(char *name, char **stockage);
// Fonction Principale :p
int main(int argc, char *argv[], char **envp){
char *fichier=NULL;//(char *) malloc(1);
// fichier[0]=0x0;
loadTextFile(filePath, &fichier);
printf("%s",fichier);
free(fichier);
exit(0);
}
/** Test d'existance d'un fichier */
int existFile(char *fichier){
if ( access(fichier, F_OK)==-1 ){
printf("Error Access File : %s :\\\n",fichier);
return -1;
}
else
return 0;
}
int myRealloc(void ** ppData, size_t newSize){
void *pTmp = realloc(*ppData, newSize);
if(pTmp == NULL)
return -1;
else{
*ppData = pTmp;
return 0;
}
}
void loadTextFile(char *name, char **stockage){
#define tailleBuffer 10
char buffer[tailleBuffer];
FILE *file;
int NbLu;
int taille=strlen(*stockage);
// printf("TAille initiale : %i\n",taille);
if (existFile(name)==-1){
printf("Accès impossible au fichier %s...\n",name);
printf("Existe t'il vraiment... :s");
exit(1);
}
if((file=fopen(name, "r")) == NULL){
printf("Ouverture impossible du fichier %s :s\n",name);
exit(1);
}
while( (NbLu=fread(buffer, 1, tailleBuffer, file)) != 0){
taille+=NbLu;
if (myRealloc((void **)stockage,taille+1)==-1){
free(*stockage);
printf("Error Realloc :s\n");
exit(1);
}
// *stockage=(char *) realloc(*stockage,taille+1);
strncat(*stockage,buffer,NbLu);
(*stockage)[taille-1]=0x0;
}
fclose(file);
} |