Błąd przy uruchomieniu, a w debuggerze działa

0

Mam program, który z ciągu "<aaaaaa>...<bbbbbbb><aaaaaa>...<bbbbbbb><aaaaaa>...<bbbbbbb>" wydobywa kolejne "<aaaaaa>...<bbbbbbb>" i zapisuje je w plikach 0.txt, 1.txt, itd.

Oto kod:

    char starts[] = {"<aaaaaa>"};
    char ends[] = {"<bbbbbbb>"};
    char *element;
    int startIndex,endIndex,diff,tabIndex=0;
    char *tmp1 = strstr(input,starts);
    char buf[10];
    do
    {
        startIndex = strpos(tmp1,starts);
        if(startIndex!=NOT_FOUND)
        {
            endIndex = strpos(tmp1,ends)+strlen(ends);
            diff = endIndex-startIndex;
            element = malloc(diff+1);
            strcpy(element,substr(tmp1,0,diff));
            element[diff] = '\0';
            saveToFile(strcat(itoa(tabIndex,buf,10),".txt"),element);
            tmp1 = tmp1+diff;
            tabIndex++;
            free(element);
        }
    }
    while(startIndex!=NOT_FOUND);

Problem w tym, że o ile w debuggerze i trybie pracy krokowej wszystko działa o tyle przy normalnym uruchomieniu zapisuje pierwszy plik i się wysypuje. Dlaczego tak się dzieje? Używam Code::Blocks.

0

Doszedłem do tego, że program wysypuje się w funkcji saveToFile.

void saveToFile(char *filename, char *content)
{
    FILE *file;
    if((file = fopen(filename,"w"))==NULL)
    {
        printf("Nie mozna zapisac do pliku!\n");
        return;
    }
    fprintf(file,"%s",content);
    printf("****\n");
    fclose(file);
    printf("****\n");
}

co ciekawsze w momencie wywołania fclose (pierwsze gwiazdki pokazuje, drugich już nie). Nazwę pliku sprawdzałem - 0.txt.

0

A masz jakiś komunikat ???

0

Process returned -1073741819 (0xC0000005) execution time : 2.085 s

0

Oto cały kod:

#include <stdio.h>

#define NOT_FOUND -1

long getFileSize(char *filename)
{
    FILE *file = fopen(filename,"r");
    long tmp;
    fseek(file,0,SEEK_END);
    tmp = ftell(file);
    fclose(file);
    return tmp;
}

char *getFileContent(char *filename)
{
  FILE *file;
  long filesize = getFileSize(filename);
  char *buffer = malloc(filesize+1);
  if (file = fopen(filename, "rt"))
  {
    fread(buffer, 1, filesize, file);
    buffer[filesize] = 0;
    fclose(file);
  }
  return buffer;
}

char *substr(char *source, int start, int howmany)
{
    char *destination = malloc(howmany+1);
    strncpy(destination, source+start, howmany);
    return destination;
}

int strpos(char *string, char *target)
{
    char *tmp = strstr(string,target);
    if(tmp)
        return tmp-string;
    return NOT_FOUND;
}

void saveToFile(char *filename, char *content)
{
    FILE *file;
    if((file = fopen(filename,"w"))==NULL)
    {
        printf("Nie mozna zapisac do pliku!\n");
        return;
    }
    fprintf(file,"%s",content);
    printf("***\n");
    fclose(file);
    printf("***\n");
}

void getHtmlElements(const char *input)
{
    char starts[] = {"<aaaaaa>"};
    char ends[] = {"<bbbbbbb>"};
    char *element;
    int startIndex,endIndex,diff,tabIndex=0;
    char *tmp1 = strstr(input,starts);
    char buf[10];
    do
    {
        startIndex = strpos(tmp1,starts);
        if(startIndex!=NOT_FOUND)
        {
            endIndex = strpos(tmp1,ends)+strlen(ends);
            diff = endIndex-startIndex;
            element = malloc(diff+1);
            strcpy(element,substr(tmp1,0,diff));
            element[diff] = '\0';
            saveToFile(strcat(itoa(tabIndex,buf,10),".txt"),element);
            tmp1 = tmp1+diff;
            tabIndex++;
            free(element);
        }
    }
    while(startIndex!=NOT_FOUND);
}

int main()
{
    getHtmlElements(getFileContent("0.html"));
    return 0;
}

no i 0.html:

Cala masa innego zbednego tekstu<aaaaaa>Anna Maria zajmuje sie nawracaniem Austriakow. Pewnego dnia, po latach nieobecnosci, wraca do niej sparalizowany maz - muzulmanin egipskiego pochodzenia.<bbbbbbb><aaaaaa>Dwie pary zyja w doskonalej harmonii, a jedyne czego im brakuje, to udane zycie seksualne. Wspolny wyjazd na Karaiby obudzi w nich drzemiace instynkty.<bbbbbbb><aaaaaa>Oparta na faktach historia opowiada o trzech przedsiebiorcach, ktorzy w skutek zmowy zostali oskarzeni o dzialalność w grupie przestepczej.<bbbbbbb><aaaaaa>Opowiesc o legendarnym poecie oraz bezsensie wojny i sztuce, ktora jest ponadczasowa i niesmiertelna.<bbbbbbb>
0

masz wyciek pamięci. W funkcji getFileContent rezerwujesz pamięć dla buffer i nigdzie jej nie zwalniasz(free)

0

Nie do końca opanowałem te łańcuchy w char*. Wydaje mi się, że napis utworzony w getFileContent po zakończeniu funkcji zostaje przekazany na zewnątrz (wskaźnik do niego).

Funkcję main zmieniłem na taką:

int main()
{
    char *content = getFileContent("0.html");
    getHtmlElements(content);
    free(content);
    return 0;
}

Czy nadal jest to traktowane jako wyciek? Program nadal się wysypuje.

0

... to co napisał @robcio plus to samo w substr(): char *destination = malloc(howmany+1);

0

Usunąłem te wycieki zwalniając pamięć po każdym wykorzystaniu funkcji tworzących i zwracających napisy:

#include <stdio.h>

#define NOT_FOUND -1

long getFileSize(char *filename)
{
    FILE *file = fopen(filename,"r");
    long tmp;
    fseek(file,0,SEEK_END);
    tmp = ftell(file);
    fclose(file);
    return tmp;
}

char *getFileContent(char *filename)
{
    FILE *file;
    long filesize = getFileSize(filename);
    char *buffer = malloc(filesize+1);
    if (file = fopen(filename, "rt"))
    {
        fread(buffer, 1, filesize, file);
        buffer[filesize] = 0;
        fclose(file);
    }
    return buffer;
}

char *substr(char *source, int start, int howmany)
{
    char *destination = malloc(howmany+1);
    strncpy(destination, source+start, howmany);
    return destination;
}

int strpos(char *string, char *target)
{
    char *tmp = strstr(string,target);
    if(tmp)
        return tmp-string;
    return NOT_FOUND;
}

void saveToFile(char *filename, char *content)
{
    FILE *file;
    if((file = fopen(filename,"w"))==NULL)
    {
        printf("Nie mozna zapisac do pliku!\n");
        return;
    }
    fprintf(file,"%s",content);
    printf("%s\n",content);
    printf("!!!\n");
    fclose(file);
    printf("!!!\n");
}

void getHtmlElements(const char *input)
{
    char starts[] = {"<aaaaaa>"};
    char ends[] = {"<bbbbbbb>"};
    char *element;
    int startIndex,endIndex,diff,tabIndex=0;
    char *tmp1 = strstr(input,starts);
    char *tmp2, *filename;
    char buf[10];
    do
    {
        startIndex = strpos(tmp1,starts);
        if(startIndex!=NOT_FOUND)
        {
            endIndex = strpos(tmp1,ends)+strlen(ends);
            diff = endIndex-startIndex;
            element = malloc(diff+1);
            tmp2 = substr(tmp1,0,diff);
            strcpy(element,tmp2);
            element[diff] = '\0';
            filename = strcat(itoa(tabIndex,buf,10),".txt");
            saveToFile(filename,element);
            tmp1 = tmp1+diff;
            tabIndex++;
            free(element);
            free(tmp2);     // tutaj zwalniam napis utworzony w substr
            free(filename);      // tutaj zwalniam napis utworzony na potrzeby nazwy pliku
        }
    }
    while(startIndex!=NOT_FOUND);
}

int main()
{
    char *content = getFileContent("0.html");
    getHtmlElements(content);
    free(content);      // tutaj zwalniam napis utworzony i zwrócony przez getFileContent
    return 0;
}

Nadal nie działa...

Czy jest możliwym, aby system przez "politykę bezpieczeństwa" blokował zapis na dysku?

0
char starts[] = {"<aaaaaa>"};

dziwny styl.

const char *starts = "<aaaaaa>";

1 użytkowników online, w tym zalogowanych: 0, gości: 1