read a string "flowor" from a file and then write it to another file as "flower".
read a string "flowor" from a file and then write it to another file as "flower".
--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.
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.
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.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Quite clever. I really liked the optional parms and the negative indexing.
Mainframe assembler programmer by trade. C coder when I can.
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.
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.
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.
Yep., or this too:
Good job otherwise. I just notice the assignment of 'e' as part the &&'ed logic. Slick.Code:if (s[len-1] != '\n') fprintf(stderr, "Lines longer than %s characters... Output may be incorrect (or this program might seg fault ;) )\n", sizeof(buffer)-1);
Mainframe assembler programmer by trade. C coder when I can.
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.
I just copied your code, big boy!!
Mainframe assembler programmer by trade. C coder when I can.
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.
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.