Thread: Why this program doesnt work properly

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43

    Unhappy Why this program doesnt work properly

    Code:
    #include<stdio.h>
    
    const int MAXLINELEN = 1024;
    
    void f(char *ifilename, char *ofilename)
    {
            FILE *ifp=fopen(ifilename, "rb"), *ofp=fopen(ofilename, "wb");
            char buf[MAXLINELEN];
            if(ifp & ofp)
                while(!feof(ifp))
                {
                        fgets(buf, MAXLINELEN,ifp);
                        fputs(buf,ofp);
                 }
    }
    The above program is intended to copy Binary and Text files; However, it does not work correctly. What is the problem?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Your AND operator is should be &&, not &, I think...

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How does it not work correctly ?

    Just off the top of my head, don't use foef to control a loop.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The above program is intended to copy Binary and Text files
    Use fread() and fwrite() then
    And don't use feof() as already noted.
    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. #5
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    I'm sorry there was a typo in the code.....its actually "...if(ifp && ofp).." and this was one of the question asked in the class....so i need answer to it, i tried find it but could not succeed

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Hey, what do you intend to do with this line??
    Code:
     if(ifp & ofp)
    Shouldn't it be an expression?? Not only the variable's name....

    How about this?
    Code:
    if(ifp != NULL && ofp != NULL)

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    Code:
    if(ifp != NULL && ofp !=NULL)
    is same as
    Code:
    if( ifp && ofp)
    Actually in the code there is typo...i typed only "&" instead of "&&"

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Actually in the code there is typo...i typed only "&" instead of "&&"
    Here's a tip - use COPY and PASTE of your actual code which you last tried to compile and run.
    Not some half-assed "it looked something like this".

    Otherwise we all just waste time finding your typos and then arguing about them rather than focussing on the real question.
    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.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    Code:
    #include<stdio.h>
    
    const int MAXLINELEN = 1024;
    
    void f(char *ifilename, char *ofilename)
    {
            FILE *ifp=fopen(ifilename, "rb"), *ofp=fopen(ofilename, "wb");
            char buf[MAXLINELEN];
            if(ifp && ofp)
                while(!feof(ifp))
                {
                        fgets(buf, MAXLINELEN,ifp);
                        fputs(buf,ofp);
                 }
    }
    Here i have pasted the complete code once again with all corrections

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just use fread and fwrite? Oh read the FAQ on why it's bad to use feof to control loops.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    The above question had multiple choice answers which are given below.

    choice 1: If the length of the input line is equal to "MAXLINELEN", then result of "fputs" is not defined

    choice 2: Long lines cannot be copied by this code

    choice 3: If the length of the input line is greater than "MAXLINELEN", then the new line character will not be copied

    choice 4: It does not produce an exact copy of some input files.

    choice 5: memset(buf,0,MAXLINELEN) is not called before the fgets call.

  12. #12
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    then i believe the proper question should be "what's bad if i run that code"

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh, you mean it's an exam question.
    "Here is some broken code, which of the following best describes what's wrong with it."

    Why don't people post their real question in the first place?

    Have you tried running the code with a few test files to see what happens?
    Like a line containing only one line - go on, it's fun!

    Here's another one
    > const int MAXLINELEN = 1024;
    This isn't constant enough for C when it comes to declaring your array, so it won't even compile on an ANSI-C compiler.

    > What is the problem?
    Learning C from an idiot who doesn't know C seems to be one problem
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program doesnt work properly.
    By +Azazel+ in forum C Programming
    Replies: 4
    Last Post: 10-12-2007, 06:57 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Why does this program not work on my laptop?!
    By Eddie K in forum C Programming
    Replies: 1
    Last Post: 03-11-2006, 04:34 PM
  4. program from book wont work
    By cemock in forum C Programming
    Replies: 2
    Last Post: 03-06-2003, 09:58 AM
  5. how do i get this program to work, anyone, anyone?
    By correlcj in forum C Programming
    Replies: 5
    Last Post: 07-04-2002, 10:28 PM