Thread: replace character while writing into a file

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    replace character while writing into a file

    read a string "flowor" from a file and then write it to another file as "flower".

  2. #2
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    Why?

  3. #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.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Isn't a file descriptor of stdin going to be 0, and therefore, your AND will fail if the program is run with stdin redirection?
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    Isn't a file descriptor of stdin going to be 0, and therefore, your AND will fail if the program is run with stdin redirection?
    But it's not a file-descriptor, it's a file-pointer - which is guaranteed to not be 0.

    How did you like it otherwise?

    --
    Mats
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I kind of agree with shoutatchickens: if you know that you are going to write "flower" to the second file, why bother reading from the first file?

    btw, matsp, this is the C++ programming forum, and it turns out that due to typos your code is neither valid C nor C++, heheh.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quite clever. I really liked the optional parms and the negative indexing.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    I kind of agree with shoutatchickens: if you know that you are going to write "flower" to the second file, why bother reading from the first file?

    btw, matsp, this is the C++ programming forum, and it turns out that due to typos your code is neither valid C nor C++, heheh.
    Ok, I have updated it to valid C - it won't pass as C++, and I can't be bothered to rewrite it as C++, to be honest.

    --
    Mats
    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.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Actually, there is a logic bug. If an instance of flower is split across buffers, due to really long lines, it won't be found.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    Actually, there is a logic bug. If an instance of flower is split across buffers, due to really long lines, it won't be found.
    Ah, well, can't get everything for free - I suppose we could add:
    Code:
    {
       int len = strlen(s);
       if (s[len-1] != '\n') fprintf(stderr, "Lines longer than %s characters... Output may be incorrect\n", sizeof(buffer)-1);
    }

    --
    Mats
    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.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yep., or this too:

    Code:
    if (s[len-1] != '\n') fprintf(stderr, "Lines longer than &#37;s characters... Output may be incorrect (or this program might seg fault ;) )\n", sizeof(buffer)-1);
    Good job otherwise. I just notice the assignment of 'e' as part the &&'ed logic. Slick.
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I take it you intended to say [strlen(s)-1] instead of [len-1]?

    --
    Mats
    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.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I just copied your code, big boy!!
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    I just copied your code, big boy!!
    Oh, I see what you did. It probably won't seg-fault tho' - unlikely that the offset on the stack segfaults.

    --
    Mats
    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.

  15. #15
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Yes, you are correct - modern stacks are big.

    When I program for my day job, on mainframes, memory allocations are designed (obviously) around page boundaries (4K). If you request 8 bytes, and your request is the first request, or the first request that requires a new page frame, then you get the last 8 bytes in a page. If you touch one byte later, and you don't own that page, you get a fault.

    And, I work in assembler, so the concept of a system provided stack is also moot - you get nothing, so it's all up to the programmer (as I assume it is for asm programmers on any platform)
    Mainframe assembler programmer by trade. C coder when I can.

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