Thread: replace character while writing into a file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
       FILE *fin, *fout;
       char *s;
       fin = (argc > 1) ?fopen(*++argv, "r"):stdin;
       fout = (argc > 2)?fopen(*++argv, "w"):stdout;
       if (fout && fin) 
          do { 
              char buffer[1000]; 
              char *p;
              while(s = fgets(buffer, sizeof(buffer), fin))
              {
                  p = s;
                  while(p = strchr(s, 'w'))
                  {
                      s = p;
                      if (p - buffer > 3 && p[-1] == 'o' && p[-2] == 'l' && p[-3] == 'f' && 
                          p[1] == 'o' && p[2] == 'r' && (p[1] = 'e'));
                   }
                   fputs(buffer, fout);
              }
            } while(s);
        else
           return 1;
       return 0;
    }
    --
    Mats
    Last edited by matsp; 12-08-2008 at 10:11 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Writing input from a file into shared memory
    By RazielX in forum C Programming
    Replies: 2
    Last Post: 09-23-2004, 12:34 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM