Thread: copy data from one file to another

  1. #31
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    @MK27 - well, oops.

    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.

  2. #32
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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 &#37;s %s", 
        the_command_line_parameter[1],
        the_command_line_parameter[2]);
    
      system(buffer);
      return EXIT_FAILURE;
    }

  3. #33
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Except "before" is "malloc(1)". Since when does malloc put a null terminator at the end of memory? Never, is when.
    But if that's all that happened we return 0 to indicate we reached EOF without adding to buffer, so the one byte buffer that we never wrote to is freed.

    My explanation was a bit off but the code remains brilliant.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #34
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by whiteflags View Post
    @MK27 - well, oops.

    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.
    Hey, I didn't mean I thought charlatanism was bad or that calling someone a charlatan is an insult.

    I do it myself whenever possible

    ps. there should be an "edit" button on the lower right...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #35
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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, "&#37;3d. %s", ++line, buffer);
        }
    
        free(buffer);
        fclose(ifile);
        fclose(ofile);
      }
    
      return EXIT_SUCCESS;
    }

  6. #36
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by MK27 View Post
    Sometimes you get what you asked for:

    edit: there's actually three very minor bugs in this, and Salem found them all (see subsequent posts).
    I do not see any minor errors. I only see crippling ones and ones that make the program HIGHLY under-efficient if it even works to begin with.

    You obviously know where the edit button is, so please fix your code.

  7. #37
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by MK27 View Post
    But if that's all that happened we return 0 to indicate we reached EOF without adding to buffer, so the one byte buffer that we never wrote to is freed.

    My explanation was a bit off but the code remains brilliant.
    Well, it's not freed by you. The person using streamline will have to free it.

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

  8. #38
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Well, it's not freed by you. The person using streamline will have to free it.
    I'm going to claim that it's actually me and I did do it. Honest.

    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?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #39
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I notice Master5001 managed to streamline things a bit while sacrificing security. b/t/w, why does
    Code:
    malloc(0x80000)
    equal .5mb?
    Last edited by MK27; 12-05-2008 at 01:48 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #40
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by MK27 View Post
    I'm going to claim that it's actually me and I did do it. Honest.

    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?
    I hate to be the psyche student on you but your code is neither brilliant nor worth using for homework purposes. I was going to simply correct your code but the entire way it works was too flawed to even fix it. You do realize that allocation is one of the most expensive operations you can do, right?

  11. #41
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by MK27 View Post
    I notice Master5001 managed to streamline things a bit. b/t/w, why does
    Code:
    malloc(0x80000)
    equal .5mb?
    Because 0x80000 is half a mebabyte in hexidecimal. Figure if 0x100000 is 1MG, then 0x100000 / 2 = 0x80000. Right? .5MB = 2^19, right?

  12. #42
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by master5001 View Post
    I was going to simply correct your code but the entire way it works was too flawed to even fix it. You do realize that allocation is one of the most expensive operations you can do, right?
    I am super glad you told me this because I was just going to start a new thread asking that very question. So it's much better to go malloc(1000) than for(i=0;i<1000;i++) malloc(1) ????!

    ps. yes, so diagnosed, you know about the meds I guess?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #43
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by MK27 View Post
    I'm going to claim that it's actually me and I did do it. Honest.

    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?

    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.

  14. #44
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  15. #45
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    you've started 25 threads in the last two months, after all
    Wow. Well, at least the_number_of_people_who_found_most_of_those_helpf ul>=1
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 06:44 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

Tags for this Thread