replace character while writing into a file

This is a discussion on replace character while writing into a file within the C++ Programming forums, part of the General Programming Boards category; read a string "flowor" from a file and then write it to another file as "flower"....

  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 09: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
    Katy, Texas
    Posts
    2,309
    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?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Katy, Texas
    Posts
    2,309
    Quite clever. I really liked the optional parms and the negative indexing.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    I just copied your code, big boy!!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    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)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21