Thread: File handling

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    40

    File handling

    Hi i want to create a program that examines the content of a file just a text file.
    My problem is i want to search for a pattern and remove then in my file for example the content of a file is:
    the@4
    !3the
    th-7e
    th&^9e

    i want to create a code that will recognize a "pattern" in that example the pattern is symbol followed by a number if the program recognizes that pattern it will remove the symbol and the number so the output will become :
    the
    the
    the
    the

    is it possible?? cause my problem is when it recognizes that pattern it should take 2 stepbackward so that i could delete them in the file then after i delete that how could i concatenate the result string...for example th&7e
    the output after the deleted characters th e how could i concatenate them so that the output will become the? thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, I should say that they use patterns with regular expression functions, which I know next to nothing about, but you should google or search the forum, for, and see what's up with that.

    To "roll your own", you would want to:

    create a char buffer
    read in text from the file, into this buffer
    use strchr(), strstr() or another string searching function to locate the pattern you want, in the buffer
    make your changes to the text in the buffer
    write out the buffer contents to a new file
    when finished, close all files, delete the old file, and rename the new file, to the old filename

    If you try to read, then reposition the file pointer, then write some other data, then shift all the data behind it forward, because you deleted some char's, then repeat it numerous times - you're very likely to damage the file.

    Make sense?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    11

    file operation

    Try this code,

    Code:
    #include        <stdio.h>
    #include        <stdlib.h>
    #include        <string.h>
    
    #include        <ctype.h>
    #define MAXLINE 100
    main()
    {
            FILE *fp;
            FILE *new;
            char buf[100];
            char buf1[100];
            int i;
            int j;
    
            if((new=fopen("new_file","w+"))==NULL)
                    perror("File open error");
    
            if((fp=fopen("file","r+"))==NULL)
                    perror("File open error");
            else
            {
                    while (fgets(buf, MAXLINE, fp) != NULL) {
                            j=0;
                            for(i=0; i<=strlen(buf); i++)
                            {
                                    if(!isalpha(buf[i]))
                                            ;
                                    else
                                            buf1[j++]=buf[i];
                            }
                            buf1[j]='\n';
                            buf1[j+1]='\0';
                            fputs(buf1,new);
    
                    }
    
            }
            fclose(fp);
            fclose(new);
            system("mv new_file file");
            exit;
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    If you know how-to, you can extract substrings from the textfile using the regcomp() and regexec() functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM