using the cp command in a program in os x

This is a discussion on using the cp command in a program in os x within the C Programming forums, part of the General Programming Boards category; Originally Posted by Apocalypse No, he was correct before. str1 should be a size of 13 to allow Hello World!

  1. #16
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Quote Originally Posted by Apocalypse
    No, he was correct before. str1 should be a size of 13 to allow Hello World!\0. Read the manual for strcat, char *s MUST be large enough to hold what it is being appended.
    He was quoting my original code, which I then changed after he pointed out the little typo.
    Sent from my iPad®

  2. #17
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    i guess my main question is can you have
    a) multiple if statements in a program (im guessin yes)
    b) can you have an "if" inside another "if" (not so sure)

    any suggestions/answers?

    thanks

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > can you have more than one getchar in a program?
    Yes, yes and yes to your questions.

    However, your problem is basically one of mixing getchar() and scanf().
    scanf() processes the minimum number of characters necessary to complete your conversion string, and anything else is left for the next input call. This normally isn't a problem if the next call is another scanf call, but for any other input function, the results are surprising.

    So, what you would do is something like
    Code:
    int main()
    {
      char buff[BUFSIZ];
      char q;
      char a;
      char original[50];
      char orig[50];
      char file[10];
      char dest[50];
      char cp[100];
        
      printf("where is the file you would like to copy? \n"
             "if the file is on the desktop, type \"y\" \n"
             "if not, type \"n\" \n");
      fgets( buff, sizeof buff, stdin );
      q=buff[0]; /* q=getchar(); */
      if(q=='y')
      {
        printf("now type the file name.\n");
        fgets( buff, sizeof buff, stdin );
        sscanf( buff, "%s", file); /* scanf("%s", &file); */
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    ok thanks for the help, but that looks way beyond me (me being about half way through c for dummies) so ill wait. i have a few working versions, just without all the bells and whistes i would like. i made one to try to let the user choose between rm, cp, and mv. needless to say it didnt work...ill just have to wait. thanks a lot

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    There's an FAQ entry on clearing the input buffer: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    i dont realy get that, or what it does or how it helps...why is my program skipping over the if's and getchar's after the first one?

  7. #22
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    q=getchar();
    You type in 'y' and enter.
    The 'y' is read by getchar() and the '\n' is left for the next input function to deal with.

    scanf("%s", &file);
    You type in "file.txt" and press enter.
    But there was a '\n' from the previous input there already. This is OK because scanf(%s) skips leading white space such as newlines, so all you get stored in your variable is "file.txt". The '\n' at the end is left for the next input function to deal with.

    a=getchar();
    OOPS - you lose.
    getchar() just sees the '\n' left behind by the previous scanf() call and throws off your entire program logic.

    /me *points to answer using fgets*
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #23
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    ok thanks a lot, i think this was getting a little beyond my skill level haha. but with the help of some guys at ipodlinux forums, it works! thanks a lot you guys,you helped a lot

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. How to program a OS
    By iwod in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2001, 07:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21