Thread: Why this code keeps erasing the files?

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    2

    Question Why this code keeps erasing the files?

    Hi, my code keep erasing the .html files it was supposed to edit, so I need help. First things first: I'm a beginner and don't know much of the details of programming, I have done my research entirely to do this code work. Why this code is important to me? Re: Because it's one step further to get one of my projects going. Why C? Re: because I had learned the basics and some more of programming in this language, so I didn't wanted to learn a new one. One more reason: C works faster than most languages. So lets get to the problem:


    I had tried lots of variations of this code other loop statements, ifs and elses, had tried different ways to represent the blanc spaces, and so on… The important part is that before this one every other had crashed repeatedly and a I couldn't find why. This one doesn't crash, however erases the content of the files it shoud edit and I don't know why, again.


    The code was supposed to edit a .html file from a .epub e-book, no need to extract the .html files with the program, I can do it manually. It shoud search the file for a specified word in it ignoring the previous html tags of it and when the code do find it it shoud add a tag at the front of the word and close it at the word's end, formatting it accordingly to the css configuration. Thats all: a code to edit a .html file from a epub to format it's text blocks with tags preseted in a css file.


    This code below, was the better a could do until now. I hope to get it working when possible and add some other features.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void){
    
    
    //declaring variables
    
    
    char arquivo[]="teste.html";
        FILE *arq;
        fpos_t pos[1];
         char capitular[]="class=Capitular>", body[]="class=Body>", bold_style[]="</style>", bold[]="bold";
         int x=strlen(capitular), y=strlen(body), i=0, cleaner;
         char word[60], ch;
    
    
        arq = fopen(arquivo, "w+"); //open file in "w+" mode
        if(arq == NULL){ //error test
                perror("Erro, nao foi possivel abrir o arquivo html\n");
                return -1;
                }
        else
    
    
                while ((ch=fgetc(arq))!=EOF){ //keep looping while it doesn't find EOF, making sure all the file gets through the code
                            if (ch==' '){ //finds blank spaces on the html files to close the string word, making sure to run this block only when a entire word in the file had been read
    
    
                                if (memcmp(word,capitular,x)==0){ //compares word and capitular having the size of capitular as number of bytes
                            fgetpos(arq,&pos[0]); //case the previous if returns true it stores the current position on the file in pos[0]
                        }
                        else{ //this else was placed here to allow the program to avoid having to test both if statements
                        if (memcmp(word,body, y)==0){ //case the previous if returns true it stores the current position on the file in pos[0]
                            fgetpos(arq,&pos[0]);
                        }}
                        if ((memcmp(word,bold, 4)==0)){ //compares word and bold strings with 4 bytes in length
                                    fsetpos(arq,&pos[0]); //set the current position to the beginning of the text block
                                    strcat(word,bold_style); //case previous if statement returns true concatenate word and bold_style
                                    fprintf(arq,"<style class=bold>%s", &word); //writes word with the concatenated bold_style and a html style tag at it's rear, replacing the previous word in file
                                    fgetpos(arq,&pos[0]);//stores the current position on the file in pos[0]
                                    i=0; // sets i to its initial state so that a new string can be stored from the beginning of the array
                                    for (cleaner=0;cleaner<=60;cleaner++){ //loops a block responsable for clean the word string
    
    
                                            word[cleaner]=NULL; //equals the current position of the word char array to NULL
                                    }
                        }   //This block(that ended here) of code is supposed to replace the word with bold written in front of it in a .html file for the same word with style html tags attached to it,
                            // that will recover a preset style written in it's corresponding .css file
                            else{ //case the word string is not complete runs this block
                                word[i]=ch; //address another character to the array word
                                i++; //add 1 to i, moving the components of the word string
                        }
    
    
    }
    }
    
    
            fclose(arq); //close the file
            return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted and answered here.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    2
    Quote Originally Posted by jimblumberg View Post
    Also posted and answered here.
    I've seen. so thats the answer:

    I did actually, but I haven't used exclusively the "w+" flag, I've used also "a", "a+", "r", "r+" and "w", of course when using the flags that can't both read and write in the file I changed the code so it would work… At least was what I tought. None of the previous codes have worked, but these one did something, even if haven't been what I wanted. Case you were wondering if I haven't used any of these options in these code: I did have, but when the flag "w+" in this code is replaced for "r+" nothing happens… It doesn't crash, it doesnt do anything as well. When replaced for "a+" flag it concatenates only a very small part of the text, not enough to know if it even did something. So, any other thoughts?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Erasing in openGL
    By Zishaan in forum Game Programming
    Replies: 6
    Last Post: 04-02-2007, 11:04 PM
  2. Erasing console
    By -Prime- in forum C Programming
    Replies: 22
    Last Post: 10-27-2006, 05:23 AM
  3. Erasing FILE
    By f0rg0tten in forum C Programming
    Replies: 8
    Last Post: 12-01-2005, 01:03 AM
  4. erasing everything except windows
    By lucy in forum Tech Board
    Replies: 3
    Last Post: 12-10-2004, 11:11 PM

Tags for this Thread