Napisalem program, ktory powinien usuwac slowo podane przez uzytkownika. Program dziala prawie dobrze tzn. usuwa slowo "go" ale slowa "To" juz nie. Dlaczego? Poza tym valgrind caly czas wywala mi bledy. Czesc juz poprawilem a reszty nie moge znalezc.
Valgrind:
==5328== Memcheck, a memory error detector
==5328== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==5328== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==5328== Command: ./remove3 infile
==5328==
Type word that you want to remove from the file:
go
==5328== Conditional jump or move depends on uninitialised value(s)
==5328== at 0x400B61: DELTEword (remove3.c:123)
==5328== by 0x400A9E: main (remove3.c:96)
==5328==
That Katharina and Petruchio should be married,
And yet we hear not of our son-in-law.
What will be said? what mockery will it be,
To what bridegroom when the priest attends
To speak the ceremonial rites of marriage!
What says Lucentio to this shame of ours?
==5328==
==5328== HEAP SUMMARY:
==5328== in use at exit: 0 bytes in 0 blocks
==5328== total heap usage: 50 allocs, 50 frees, 1,200 bytes allocated
==5328==
==5328== All heap blocks were freed -- no leaks are possible
==5328==
==5328== For counts of detected and suppressed errors, rerun with: -v
==5328== Use --track-origins=yes to see where uninitialised values come from
==5328== ERROR SUMMARY: 45 errors from 1 contexts (suppressed: 2 from 2)
oraz moj kod programu:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define CHUNK 12
char *getWord(FILE *infile);
int DELTEword(char *word, char *KEYword);
char *getWord(FILE *infile)
{
int length, cursor = 0, c;
char *word, *word2;
word = (char*)malloc(sizeof(char)*CHUNK);
if(word == NULL) return NULL;
length = CHUNK;
while((c = getc(infile)) != ' ' && !feof(infile))
{
word[cursor] = c;
cursor++;
if(cursor >= length)
{
length += CHUNK;
word2 = (char*)realloc(word, length*sizeof(char));
if(word2 == NULL)
{
free(word);
return NULL;
}
else word = word2;
}
}
word[cursor] = '\0';
return word;
}
int main(int argc, char *argv[])
{
FILE *infile;
int length;
int size = 20;
char *word, c, *keyWord, *keyWord2;
if(argc != 2)
{
printf("\nMissing arguments.\n");
abort();
}
keyWord = malloc(size*sizeof(*keyWord));
if(keyWord == NULL) exit(EXIT_FAILURE);
printf("Type word that you want to remove from the file:\n");
length = 0;
while(1)
{
if(length >= size)
{
size += CHUNK;
keyWord2 = realloc(keyWord, size*sizeof(char));
if(keyWord2 == NULL)
{
free(keyWord);
exit(EXIT_FAILURE);
}
else keyWord = keyWord2;
}
c = getchar();
if(c == '\n') break;
keyWord[length] = c;
length++;
}
infile = fopen(argv[1], "r");
if(infile != NULL)
{
while(!feof(infile))
{
word = getWord(infile);
if(word == NULL)
{
free(word);
break;
}
if(DELTEword(word, keyWord) == 1)
{
printf(word, infile);
printf(" ");
free(word);
}
}
}
else
{
printf("It is impossible to open the infile\n");
free(keyWord);
abort();
}
free(keyWord);
fclose(infile);
return 0;
}
int DELTEword(char *word, char *KEYword)
{
int i, k = 0, l = 0, length;
char *ptr;
if(word != NULL)
{
length = strlen(KEYword);
for(i = 0; word[i] != '\0'; i++)
{
if(word[i] == KEYword[k])
{
l++;
k++;
}
else break;
if(l == length)
{
ptr = &word[i];
memmove((ptr - length) + 1, ptr + 1, strlen((ptr - length) + 1));
l = 0;
k = 0;
}
}
return 1;
}
else return 0;
}
Dziekuje za wszelka pomoc.
dodanie znacznika <code> - fp