When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
72 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
segfaults (segmentation faults) can be caused by many things
its basically the operating system telling you you did a bad and it has no idea what to do
e.g. trying to read invalid memory
now what do you mean by this
I know for a fact the fopen does not end in NULL
Because fopen can not end in NULL since it is a function. Do you mean it does not return NULL?
Because then how can you be sure. You can only be sure if you assert(test != NULL); and that passes (or do another related check)
ye
i mean i know the fopen does work
cus i read somewhere to just chgeck if it does properly open
ye i checked if text == NULL
and it wasnt
fseek(text, 0, SEEK_END); is not valid on text files
oh waot
no thats binary files
my bad
lol
nah cus if i put it to the top
it works
i think its caused by using fseek on differnet files
after
or something
if i fseek text first then fseek redacts then it doesnt work i tthink
can I see the rest of the code before this?
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
void redact_words(const char *text_filename,
const char *redact_words_filename) {
FILE *redacts = fopen(redact_words_filename, "r");
/* INITIALIZIGIN REDACT_WORDS */
fseek(redacts, 0, SEEK_END);
int length = ftell(redacts);
fseek(redacts, 0, SEEK_SET);
char *str = malloc(length);
fread(str, 1, length, redacts);
fclose(redacts);
int count = 0;
char *token = strtok(str, ",");
while (token != NULL) {
count++;
token = strtok(NULL, ",");
}
char *redact_words[count];
// printf("%d", count);
int iindex = 0;
int i = 0;
for (int index = 0; index < length; index++) {
if (!isalpha(str[index])) {
if (iindex != -1) {
char *sub = malloc(index - iindex + 2);
// SUBSTRINGING
int c = 0;
while (c < index - iindex) {
sub[c] = str[iindex + c];
// printf("%c \n", sub[c]);
c++;
}
sub[c] = '\0';
// SUBSTRING FINISHED
redact_words[i] = sub;
i++;
iindex = -1;
}
} else {
if (iindex == -1) {
iindex = index;
}
}
// printf("index: %d, char: %c\n", index, str[index]);
}
free(str);
for (int i = 0; i < sizeof(redact_words); i++) {
printf("%s \n", redact_words[i]);
}
/* AFTER REDACT_WORDS */
FILE *text = fopen(text_filename, "r");
if(text == NULL){
printf("wwss");
return;
}
fseek(text, 0, SEEK_END);
return;
length = ftell(text);
fseek(text, 0, SEEK_SET);
*str = malloc(length);
fread(str, 1, length, text);
fclose(text);
iindex = 0;
for (int index = 0; index < length; index++) { // LOOP EVERY LETTER
char letter = str[index];
if (!isalpha(letter) ){
if(letter == '-' || letter == '\''){
iindex = -2;
}
if (iindex != -1) {
char sub[index - iindex + 1];
// SUBSTRINGING
int c = 0;
while (c < index - iindex) {
sub[c] = str[iindex + c];
// printf("%c \n", sub[c]);
c++;
}
sub[c] = '\0';
bool same = 0;
for (int i = 0; i < sizeof(redact_words); i++) {
if (strcmp(redact_words[i], sub) != 0) {
same = 1;
}
}
if (same) {
char *str2 = malloc(length);
for (int i = 0; i < length; i++) {
if (i >= iindex && i <= index) {
str2[i] = '*';
} else {
str2[i] = str[i];
}
}
str = str2;
free(str2);
}
iindex = -1;
}
else if(iindex == -2){
if(letter == ' '){
iindex = -1;
}
}
}
else{
if (iindex == -1) {
iindex = index;
}
}
}
printf("%s", str);
}
bool isRedactable(char word[], char *list[]) {
for (int i = 0; i < sizeof(list); i++) {
if (strcmp(list[i], word) != 0) {
return 1;
}
}
return 0;
}
void main33() { redact_words("q3text.txt", "q3redact.txt"); }
yes
yes
i was returning to see if thats where error is caused
and it is
wait
i get it somewhere else now
for (int i = 0; i < sizeof(redact_words); i++) {
printf("%s \n", redact_words[i]);
}
its on this
oh its tryna access a third word
for some reason
that doesnt exist
what I was about to say is if I expect an error anywhere itll be in your substring allocations. I can't see through those without executing but thats where I expect any errors to pop up
oh hang on
char *redact_words[count];
im not a massive fan of this
yeah its the dynamic array im not a fan of
but thats personal preference. I think its okay because you don't change count after that so it doesnt really matter
what you are doing is correct
sizeof(redact_words) gives something else?
perhaps count is incorrect
oh wait
haha
sizeof will give the number of bytes not the count
ye
lmfao
ill try to find this
and come back to u if i cant understand
why does
fclose(text)
do signal aborted
nvm it was something else
ty
@fathom hound Has your question been resolved? If so, run !solved :)
!solved
Thank you and let us know if you have any more questions!