Thread: cFileName

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    cFileName

    Hello,
    I have a problem with my source. I want to save only the text after <body and before </body> in the php file.
    I can see : "Impossible a ouvrir " only and a Windows Debuggage Error...
    I think the error is in my cFileName. I'm not sure.
    Thanks...

    My code :
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <direct.h>
    
    
    /*Déclariations des fonctions :*/
    void verif(char*);
    
    
    void main(int argc, char *argv[])
    {
        /*Declarations des variables :*/
        HANDLE hdlfff;
        LPCTSTR dir;
        WIN32_FIND_DATA Ffd;
        char * buffer;
        char * buffer1;
        
        printf("Programme par camje_lemon pour la reconstruction du site univers-hp.net v8.\n");
        getcwd ( buffer, 512 ); 
        buffer1=(char*)malloc(sizeof(char)*(sizeof(buffer)+7));
        sprintf(buffer1,"%s/*.php",buffer);
        dir = (LPCTSTR) buffer1;
        printf("Lancement de la verification de tous les fichiers .php du dossier courant (%s).\nTout ce qui sera avant <body> et apres </body> sera supprimé.\n",buffer1);
        hdlfff = FindFirstFile(dir,&Ffd);
        printf("%s\n",Ffd.cFileName);
        verif(Ffd.cFileName);
        while (FindNextFile(hdlfff,&Ffd)) 
        {
             verif(Ffd.cFileName);   
        } 
        printf("Fin du programme.\n");
        
    }
    
    
    
    void verif(char*fname)
    {
    /*Déclaration variables :*/
        FILE *Fichier;
        char *tmp;
        char *final;
        int test;
        int liens;
        int taille;
        
        
        printf("Commencement de la réécriture de %s\n",fname);
        test=2;
        liens=0;
        if ((Fichier = fopen(fname, "a"))==NULL)
        {
            printf("Impossible a ouvrir\n");
        }
    
        fseek(Fichier,0,SEEK_SET);
        taille= ftell(Fichier);
        final=(char*)malloc(sizeof(char)*(taille+1));
        fseek(Fichier,0,SEEK_SET);
        while(fgets(tmp,sizeof(tmp),Fichier)!=NULL)
        {
            if (strstr(tmp,"</body>"))
            {
                    test=2;
            }
            if (test == 1)
            {
                    sprintf(final,"%s%s",final,tmp);     
                    if (strstr(tmp,"<a"))
                    {
                                    liens++;
                    }
            }
            if (strstr(tmp,"<body"))
            {
                    test=1;
            }
        }
        if(fclose(Fichier)!=0)
        {
            printf("Erreur lors de la fermeture du fichier.\n");
        }
        
        if ((Fichier = fopen(fname, "a"))==NULL)
        {
            printf("Impossible a ouvrir\n");
        }
        if(fprintf(Fichier,final)<0)
        {
            printf("Erreur lors de la réécriture.\n");
        }
        if(fclose(Fichier)!=0)
        {
            printf("Erreur lors de la fermeture du fichier.\n");
        }
        
        printf("Le fichier %s a bien été réécrit (Tout ce qu'il avait avant <body ou apres </body> a été supprimé). Il contient %d lien(s).\n",fname,liens);            
    }

  2. #2
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    The error lies in one of these lines:
    Code:
      buffer1=(char*)malloc(sizeof(char)*(sizeof(buffer)
    +7));
        sprintf(buffer1,"%s/*.php",buffer);
        dir = (LPCTSTR) buffer1;
    or one before it. the sprintf command may bverflowing the buffer of the string you are writing to.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Close. This is your problem:
    Code:
    while(fgets(tmp,sizeof(tmp),Fichier)!=NULL)
    tmp has no memory allocated for it, and as such, points to some arbitrary point in memory, and crashes your program.

    See:
    Code:
        char *tmp;
        char *final;
        int test;
        int liens;
        int taille;
        
        
        printf("Commencement de la réécriture de %s\n",fname);
        test=2;
        liens=0;
        if ((Fichier = fopen(fname, "a"))==NULL)
        {
            printf("Impossible a ouvrir\n");
        }
    
        fseek(Fichier,0,SEEK_SET);
        taille= ftell(Fichier);
        final=(char*)malloc(sizeof(char)*(taille+1));
        fseek(Fichier,0,SEEK_SET);
        while(fgets(tmp,sizeof(tmp),Fichier)!=NULL)
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > void main(int argc, char *argv[])
    main returns an int

    > getcwd ( buffer, 512 );
    Quzah has already said it, you're not allocating any memory here.
    Save yourself a bunch of grief and remove all your malloc calls. and just go with replacing

    char * buffer;
    with
    char buffer[512];

    Do that for all your char pointers, make them char arrays.

    Not only that, you don't call free() for the memory you malloc, and you don't include stdlib.h to prototype it properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    I repaired the buffer with [512], it's that for the /Muad'Dib\'s message, i think.
    I called free to free my pointers.
    I called stdlib...
    But to tmp i don't know what i can do.
    And i'm not sure dir = (LPCTSTR) buffer1; is OK.
    Thanks for your replys.

    My new code :

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <direct.h>
    #include <stdlib.h>
    
    
    /*Déclariations des fonctions :*/
    void verif(char*);
    
    
    void main(int argc, char *argv[])
    {
        /*Declarations des variables :*/
        HANDLE hdlfff;
        LPCTSTR dir;
        WIN32_FIND_DATA Ffd;
        char buffer[512];
        char * buffer1;
        
        printf("Programme par camje_lemon pour la reconstruction du site univers-hp.net v8.\n");
        getcwd ( buffer, 512 ); 
        buffer1=(char*)malloc(sizeof(char)*(512+7));
        sprintf(buffer1,"%s/*.php",buffer);
        dir = (LPCTSTR) buffer1;
        printf("Lancement de la verification de tous les fichiers .php du dossier courant (%s).\nTout ce qui sera avant <body> et apres </body> sera supprimé.\n",buffer1);
        hdlfff = FindFirstFile(dir,&Ffd);
        printf("%s\n",Ffd.cFileName);
        verif(Ffd.cFileName);
        while (FindNextFile(hdlfff,&Ffd)) 
        {
             verif(Ffd.cFileName);   
        } 
        printf("Fin du programme.\n");
        
        free(buffer1);
    }
    
    
    
    void verif(char*fname)
    {
    /*Déclaration variables :*/
        FILE *Fichier;
        char tmp[512];
        char *final;
        int test;
        int liens;
        int taille;
        
        
        printf("Commencement de la réécriture de %s\n",fname);
        test=2;
        liens=0;
        if ((Fichier = fopen(fname, "r"))==NULL)
        {
            printf("Impossible a ouvrir\n");
        }
    
        fseek(Fichier,0,SEEK_END);
        taille= ftell(Fichier);
        final=(char*)malloc(sizeof(char)*(taille+1));
        fseek(Fichier,0,SEEK_SET);
        while(fgets(tmp,sizeof(tmp),Fichier)!=NULL)
        {
            if (strstr(tmp,"</body>"))
            {
                    test=2;
            }
            if (test == 1)
            {
                    strcat(final, tmp);     
                    if (strstr(tmp,"<a"))
                    {
                                    liens++;
                    }
            }
            if (strstr(tmp,"<body"))
            {
                    test=1;
            }
        }
        if(fclose(Fichier)!=0)
        {
            printf("Erreur lors de la fermeture du fichier.\n");
        }
        
        if ((Fichier = fopen(fname, "w+"))==NULL)
        {
            printf("Impossible a ouvrir\n");
        }
        if(fprintf(Fichier,final)<0)
        {
            printf("Erreur lors de la réécriture.\n");
        }
        if(fclose(Fichier)!=0)
        {
            printf("Erreur lors de la fermeture du fichier.\n");
        }
        
        printf("Le fichier %s a bien été réécrit (Tout ce qu'il avait avant <body ou apres </body> a été supprimé). Il contient %d lien(s).\n",fname,liens);            
        
        free(final);
    }
    Last edited by camje_lemon; 03-06-2004 at 06:42 AM.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void verif(char*fname)
    {
    /*Déclaration variables :*/
        FILE *Fichier;
        char *tmp; // Note 1
        char *final;
        int test;
        int liens;
        int taille;
        
        
        printf("Commencement de la réécriture de %s\n",fname);
        test=2;
        liens=0;
        if ((Fichier = fopen(fname, "a"))==NULL) // Note 2
        {
            printf("Impossible a ouvrir\n");
        }
    
        fseek(Fichier,0,SEEK_SET); // Note 3
        taille= ftell(Fichier);
        final=(char*)malloc(sizeof(char)*(taille+1));
        fseek(Fichier,0,SEEK_SET);
        while(fgets(tmp,sizeof(tmp),Fichier)!=NULL)
        {
            if (strstr(tmp,"</body>"))
            {
                    test=2;
            }
            if (test == 1)
            {
                    sprintf(final,"%s%s",final,tmp); // Note 4
                    if (strstr(tmp,"<a"))
                    {
                                    liens++;
                    }
            }
            if (strstr(tmp,"<body"))
            {
                    test=1;
            }
        }
        if(fclose(Fichier)!=0)
        {
            printf("Erreur lors de la fermeture du fichier.\n");
        }
        
        if ((Fichier = fopen(fname, "a"))==NULL) // Note 5
        {
            printf("Impossible a ouvrir\n");
        }
        if(fprintf(Fichier,final)<0)
        {
            printf("Erreur lors de la réécriture.\n");
        }
        if(fclose(Fichier)!=0)
        {
            printf("Erreur lors de la fermeture du fichier.\n");
        }
        
        printf("Le fichier %s a bien été réécrit (Tout ce qu'il avait avant <body ou apres </body> a été supprimé). Il contient %d lien(s).\n",fname,liens);            
        
        free(tmp); // Note 6
        free(final);
    }
    1. As mentioned you need to make tmp a buffer, not just a pointer:
    Code:
        char tmp[512];
    2.
    mode "a": append; open or create text file for writing at end-of-file
    As you are only reading the file(you reopen it when you want to write to it) I think you want:
    mode "r": open text file for reading
    3. You set the file pointer to the beginning of the file instead of the end so this will return 0. I think you want:
    Code:
        fseek(Fichier,0,SEEK_END);
        taille= ftell(Fichier);
    4. sprintf can not write overlapped content. Use this instead:
    Code:
    strcat(final, tmp);
    5. Unless you want to append the contents of final to the end of the file I think you want:
    mode w: truncate to zero length or create text file for writing
    6. You should only free() memory that you have allocated.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    I always have the bug...
    I modified my code ^^...

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    I think it's OK
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Directories using I/O
    By legit in forum C++ Programming
    Replies: 12
    Last Post: 06-16-2009, 02:06 PM
  2. subclass
    By dayknight in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2004, 03:39 PM