read a string "flowor" from a file and then write it to another file as "flower".
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"....
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 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.
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!
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.
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
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!
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.![]()
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!
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);
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!
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!!
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!
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)
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!