![]() |
| | #31 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| I was pretty sure I was wrong when I hit send, but it always happens that way doesn't it? I can find some creative words to describe you too, by the way. I suggest you not dish out unwarranted insults.
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 |
| whiteflags is offline | |
| | #32 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Oh! And who could forget this little gem. Example: Code: #include <stdlib.h>
#include <stdio.h>
#define IS_LESS_THAN <
int main(int the_number_of_command_line_arguments, char **the_command_line_parameter)
{
char buffer[4096];
if(the_number_of_command_line_arguments IS_LESS_THAN 3)
return EXIT_FAILURE;
snprintf(buffer, sizeof(buffer)-1, "copy %s %s",
the_command_line_parameter[1],
the_command_line_parameter[2]);
system(buffer);
return EXIT_FAILURE;
}
|
| master5001 is offline | |
| | #33 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
My explanation was a bit off but the code remains brilliant.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #34 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
I do it myself whenever possible ![]() ps. there should be an "edit" button on the lower right...
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #35 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Oooops, I didn't notice the line number mumbo jumbo. MK27's code hardly seems brilliant. I mean come on now dude... Code: #include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if(argc != 3)
puts("Usage: copyfile [from] [to]");
else
{
FILE *ifile, *ofile;
char *buffer;
size_t line;
ifile = fopen(argv[1], "r");
ofile = fopen(argv[2], "w");
if(!ifile || !ofile)
{
perror("Could not complete this operation.");
if(ifile)
fclose(ifile);
if(ofile)
fclose(ofile);
return EXIT_FAILURE;
}
if(!(buffer = malloc(0x80000))) /* .5MB is sufficient */
{
fputs("Out of memory!\n", stderr);
fclose(ifile);
fclose(ofile);
return EXIT_FAILURE;
}
line = 0;
while(fgets(buffer, 0x80000, ifile))
{
fprintf(ofile, "%3d. %s", ++line, buffer);
}
free(buffer);
fclose(ifile);
fclose(ofile);
}
return EXIT_SUCCESS;
}
|
| master5001 is offline | |
| | #36 | |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Quote:
You obviously know where the edit button is, so please fix your code. | |
| master5001 is offline | |
| | #37 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
You can say (I guess you are saying) that it's an undocumented (as of yet) feature that your function will leave buffer in an invalid state when \n is immediately followed by EOF, and I would agree with you that one fix is to then document that when your function returns 0, buffer is invalid and should not be accessed. I will leave it to you to determine whether that's a bug in the code or a bug in the comments (since your comment about what return 0 means at the bottom is incorrect). | |
| tabstop is offline | |
| | #38 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
But, don't you think it would be going too far, after having actually written some half-brilliant code which I should never have done because it might get used in someone's homework, to then provide our negligent student with some kind of API documentation to complete the charade?
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #39 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| I notice Master5001 managed to streamline things a bit while sacrificing security. b/t/w, why does Code: malloc(0x80000)
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 12-05-2008 at 01:48 PM. |
| MK27 is offline | |
| | #40 | |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Quote:
| |
| master5001 is offline | |
| | #41 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| |
| master5001 is offline | |
| | #42 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
ps. yes, so diagnosed, you know about the meds I guess?
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #43 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
Since the whole exercise was already going too far, that's irrelevant. I realize you were happy to have a solution that (seemed to) work, and you wanted to share; and if you had just done so, you probably would still have had a couple people make noises about doing people's homework, but that's mostly small potatoes (everybody has a line, but in different places), and using code as a "teachable moment" can be useful. The flip side is that you have become a regular here -- you've started 25 threads in the last two months, after all -- and a known quantity. And posting the code that you did, calling it "brilliant" and "bug-free"; well, that's a teachable moment, too. Not just that pride goeth before a fall, but maybe to see what bug-free code actually is. I'm getting way too philosophical for a Friday, so I'll stop. | |
| tabstop is offline | |
| | #44 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Haha you made me laugh :-) In all seriousness, your code isn't "not-brilliant" because of how you did it. It is problematic because of the fact that global buffers are just plain messy. Simply put your code is quite excellent except that it requires the caller to do stuff to clean up after each and every call to the function. And the function is slower because of the fact that it must allocate buffers each and every call (well, probably not, malloc may be over-allocating). Its better to do malloc(1000) than malloc(1) and incrementally increase the buffer size. What you can do is have a factor to increase your malloc by and just allocate chunks at a time. |
| master5001 is offline | |
| | #45 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Wow. Well, at least the_number_of_people_who_found_most_of_those_helpf ul>=1
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
![]() |
| Tags |
| copy, data, file, mode, modify |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| File transfer- the file sometimes not full transferred | shu_fei86 | C# Programming | 13 | 03-13-2009 12:44 PM |
| gcc link external library | spank | C Programming | 6 | 08-08-2007 03:44 PM |
| Possible circular definition with singleton objects | techrolla | C++ Programming | 3 | 12-26-2004 10:46 AM |
| spell check in C using a dictionary file | goron350 | C Programming | 10 | 11-25-2004 06:44 PM |
| gcc problem | bjdea1 | Linux Programming | 13 | 04-29-2002 06:51 PM |