Thread: copy data from one file to another

  1. #16
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    use splint

  2. #17
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by slingerland3g View Post
    Well MK27 you will be spoon feeding for a very long time then.

    "Give a man a fish he is fed for a day, teach a man to fish he is fed for life"
    google charalatan

    What are we teaching today?
    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

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by MK27
    google charalatan
    That word is in Spanish, apparently, and means "rattler", according to the online source I checked. You probably mean charlatan, though the connection in both cases seems rather tenous.

    The point of both Adak and slingerland3g is that we should avoid giving a complete answer to a homework problem until it has been shown that a genuine attempt has been made for a complete solution.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    > if (argc<2) fatal(-1,"Two filenames required.");
    If argc is 2, what is the value of argv[2] here?
    > if ((fstOUT=fopen(argv[2],"w"))==NULL)

    > if ((fstRO=fopen(argv[1],"ro"))==NULL)
    Which non-standard compiler extension requires "o" in the mode?

    > case -1: fprintf(fstOUT, "%d. %s\n",ln,buffer); // the "special case"
    Yes, very special when you have this line in the called function.
    > if (buffer==NULL) return -1; // last check

    > buffer=realloc(buffer,++i+1); // more memory for next iteration
    And if realloc fails, what happened to the memory you used to have?


    Whilst it doesn't appear to be a problem in this instance, the fact that i starts at 0 and the amount of memory starts at 1 is a recipe for off-by-one errors at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    That word is in Spanish, apparently, and means "rattler", according to the online source I checked. You probably mean charlatan, though the connection in both cases seems rather tenous.
    Yes, I checked the spelling on line and accidently used the spanish one. The meaning is similiar, altho in english it would be more akin to someone that chatters/rattles on about things which s/he may not actually know. However, it's not prejudicial (I'm not a mean, angry person) but rather implies one is pretending or acting that way on purpose. It might even be considered a skill.

    Quote Originally Posted by laserlight View Post
    The point of both Adak and slingerland3g is that we should avoid giving a complete answer to a homework problem until it has been shown that a genuine attempt has been made for a complete solution.
    Well, I know the policy of the cboard moderators is not to do people's homework. If you want to make it a rule that nobody should and that you will remove posts that do, then go ahead. Philosophically, I don't think it will make the forum better for anyone, tho. To be honest, if someone wants to copy something they don't understand and hand it in somewhere, then I hope they aren't paying to much for their own non-education (I'm not trying to help people "cheat").

    Anyway, the OP does not claim to be a student with homework, which means s/he may not have the benefit of a class of peers and all the examples and advice one can find at an institution of higher learning, so thought I was being helpful, and, in fact, inviting questions with my unorthodox programming style

    Which I am beginning to believe is pretty much "bug free" for sure now.
    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

  6. #21
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Not to ruin a perfectly good round of public humiliation on another member, but I should point out I posted code yesterday that does basically exactly this task. Though mine works a little differently than MK27's since it is designed to remove or find and replace something from a different file.

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    Why was this reopened for discussion.

    >> Which I am beginning to believe is pretty much "bug free" for sure now.

    buffer=realloc(buffer, ++i+1);

    The value of i is undefined. An expression must not reference a stored value more than once, in order to be defined. But that may not be the exact wording... it's in the Statements section of the Standard. Anyway, aside from what Salem pointed out earlier about the realloc statement. He's right about that - if realloc fails, you leak your own memory.

    Lengthening the buffer one character at a time seems rather braindead. You might as well read character by character.

    Not to mention that your streamline function will return leaving the string without a terminating zero, sometimes. And also, if streamline returns zero (which should happen if the line does not end in a newline) then you only succeed partially. Consider the last line in a file for example, which may not be followed with a newline and not copied by the program.

  8. #23
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Lol, Hey I'm "Fluent" in spanish agreed with MK27 , enjoy guys, heading to class atm
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  9. #24
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    ++i + 1 does not produce an undefined value for i. If he put ++i += i + 1, that would be a problem.

  10. #25
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    > if (argc<2) fatal(-1,"Two filenames required.");
    If argc is 2, what is the value of argv[2] here?
    Yep, there's a bug. It should be <3.

    > if ((fstRO=fopen(argv[1],"ro"))==NULL)
    Which non-standard compiler extension requires "o" in the mode?
    Hmm...right again. Should just be "r"

    > case -1: fprintf(fstOUT, "&#37;d. %s\n",ln,buffer); // the "special case"
    Yes, very special when you have this line in the called function.
    > if (buffer==NULL) return -1; // last check
    Wow, three in a row! I'm out. This should be return -2 of course.

    > buffer=realloc(buffer,++i+1); // more memory for next iteration
    And if realloc fails, what happened to the memory you used to have?
    If realloc fails, the program is over because the function returns -2. All the allocation is checked. If the program is over, it doesn't matter what happened to what used to be.

    Whilst it doesn't appear to be a problem in this instance, the fact that i starts at 0 and the amount of memory starts at 1 is a recipe for off-by-one errors at some point.
    You have it backwards (the first character is 0, but that still requires 1 byte). Also, this ensures that we won't get a double free if the EOF is after a \n, since nothing is written into the (one byte) buffer.
    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

  11. #26
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by whiteflags View Post
    buffer=realloc(buffer, ++i+1);

    The value of i is undefined. An expression must not reference a stored value more than once, in order to be defined. But that may not be the exact wording... it's in the Statements section of the Standard. Anyway,
    i is defined, and only referenced once here.

    Not to mention that your streamline function will return leaving the string without a terminating zero, sometimes.
    Definitely incorrect, which is why I like words like charlatan. If it did that (at EOF after \n), then it did nothing to the string, which means the string still has a terminator from before.

    And also, if streamline returns zero (which should happen if the line does not end in a newline) then you only succeed partially. Consider the last line in a file for example, which may not be followed with a newline and not copied by the program.
    This is very very clearly addressed in the code and notes. If streamline reaches EOF after writing to buffer,

    buffer[i]='\0';

    Lengthening the buffer one character at a time seems rather braindead. You might as well read character by character.
    I am reading character by character.
    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

  12. #27
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    > If realloc fails, the program is over because the function returns -2. All the allocation is checked. If the program is over
    Yeah today maybe.
    But sooner or later, you'll use realloc in the same way and you won't be exiting the program.
    THEN you'll have a problem.

    > You have it backwards (the first character is 0, but that still requires 1 byte).
    Like I said, I didn't think there was a problem, just that the code was messy and error prone.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #28
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    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 size, total;
    
        ifile = fopen(argv[1], "rb");
        ofile = fopen(argv[2], "wb");
    
        if(!ifile || !ofile)
        {
          perror("Could not complete this operation.");
    
          if(ifile)
            fclose(ifile);
    
          if(ofile)
            fclose(ofile);
    
          return EXIT_FAILURE;
        }
    
        if(!(buffer = malloc(0x1000))) /* 4k is sufficient */
        {
          fputs("Out of memory!\n", stderr);
          fclose(ifile);
          fclose(ofile);
          return EXIT_FAILURE;
        }
    
        total = 0;
        while((size = fread(in, 1, 0x1000, ifile)))
        {
          fwrite(out, 1, size, ofile);
          total += size;
          printf("\r&#37;d bytes copied", total);
        }
    
        free(buffer);
        fclose(ifile);
        fclose(ofile);
      }
    
      return EXIT_SUCCESS;
    }
    But that is just my two cents.

  14. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    > If realloc fails, the program is over because the function returns -2. All the allocation is checked. If the program is over
    Yeah today maybe.
    But sooner or later, you'll use realloc in the same way and you won't be exiting the program.
    THEN you'll have a problem.
    What if I promise not to?

    > You have it backwards (the first character is 0, but that still requires 1 byte).
    Like I said, I didn't think there was a problem, just that the code was messy and error prone.
    Messy! Is not!
    Anyway, if by error prone you mean "there's some mistakes you could have made by doing it this way, but didn't" then I will take that as a compliment, Salem.
    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

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by MK27 View Post
    i is defined, and only referenced once here.


    Definitely incorrect, which is why I like words like charlatan. If it did that (at EOF after \n), then it did nothing to the string, which means the string still has a terminator from before.
    Except "before" is "malloc(1)". Since when does malloc put a null terminator at the end of memory? Never, is when.

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