Thread: deleting ifstream line

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    17

    deleting ifstream line

    hello,
    I have a scanner that I need to tweak. This one section scans a line and I need it to delete what ever it takes in if its between the comment sign. { delete this}. here is the block of code that I messing with.

    Code:
     
    else if( ch == '{' ) 
        {
           ch = rfile.get();
           tokenIndex = 0;
           while(ch != '}' && ! rfile.eof () )
           {
             tokString[ tokenIndex ++ ] = ch;
             ch = rfile.get();
           }
           tokString [ tokenIndex ] = 0;
           ch = rfile.get(); 
           currentToken = OUTPUT;
        }
    Is there a get and delete in ifstream? I couldn't find anything online. can I set "ch" to null?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you mean you want to delete it from the file, or delete it from your string representation. You can't exactly delete it from the file without reading in the entire file and then writing it back out except for the parts you don't want. I don't think that's what you want to do here, though.

    It looks like all you need to do is ignore the text you want to "delete". Just don't add it to tokString or whatever you are doing with it. You could just keep calling rfile.get() until you reach the closing brace without the rest of the code.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This function removes bracketed things from buffers
    Code:
    char* delquotes(char* source,int szsource){
        char temp[szsource];
        int tempc=0;
        bool del=false;
        for(int i=0;source[i]!='\0';i++){
            if(source[i]=='{'&& !del){
                del=true;
            }
            else if(del==false){
                temp[tempc]=source[i];
                tempc++;
            }
            if(source[i]=='}' && del){
                del=false;
            }
        }
        temp[tempc]='\0';
        strcpy(source,temp);
        return source;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Unfortunately it isn't legal C++.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Why?
    You don't like strcpy?
    Fine, I'll replace it:
    Code:
    char* delquotes(char* source,int szsource){
        char temp[szsource];
        int tempc=0,n;
        bool del=false;
        for(int i=0;source[i]!='\0';i++){
            if(source[i]=='{'&& !del){
                del=true;
            }
            else if(del==false){
                temp[tempc]=source[i];
                tempc++;
            }
            if(source[i]=='}' && del){
                del=false;
            }
        }
        temp[tempc]='\0';
        for(n=0;temp[n]!='\0';n++){
            source[n]=temp[n];
        }
        source[n]='\0';
        return source;
    }
    Last edited by maxorator; 10-25-2006 at 10:06 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    strcpy is fine and completely legal in C++. I was referring to the non-constant array sizes.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    char* delquotes(char* source,int szsource){
        char *temp=new char[szsource];
        int tempc=0,n;
        bool del=false;
        for(int i=0;source[i]!='\0';i++){
            if(source[i]=='{'&& !del){
                del=true;
            }
            else if(del==false){
                temp[tempc]=source[i];
                tempc++;
            }
            if(source[i]=='}' && del){
                del=false;
            }
        }
        temp[tempc]='\0';
        for(n=0;temp[n]!='\0';n++){
            source[n]=temp[n];
        }
        source[n]='\0';
        delete[] temp;
        return source;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Usually functions that take a C style string and a size assume that the size is the size of the string, not the size of the array. If you are going to do something different you should make it clear in a comment at the very least.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And better to test that there is no buffer overrun.

    Mpreover - you don't need the temp string everithing can be done inplace

    Code:
    char* remove(char* stringSrc, char char2remove)
    {
       char* src = stringSrc;
       char* dst = stringSrc;
       while(*src)
       {
          if(*src == char2remove)
             src++;
          else
          {
             if(src != dst)
             {
                *dst = *src; 
             }
             dst++;
             src++;
          }
       }
       *dst = 0;
       return stringSrc;
    }
    Last edited by vart; 10-26-2006 at 03:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked to MSCVRT, deleting first line of a file.
    By Francisco in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2009, 10:16 AM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. ASM beginner gets 24 compiler errors on hello world...
    By Desolation in forum Tech Board
    Replies: 12
    Last Post: 06-16-2007, 10:21 PM
  4. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  5. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM