#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Unhappy Homework problem


    Hi please help me for this:
    this is the C programming:

    Write a program in a file character from the keyboard and The characters are binary and then read the file and the file The last character is transmitted binary inputs *
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    So what have you done so far?
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    sorry i didnt get exactly what do u mean.
    ///
    the program must be in c language.
    i dont know exactly its my homwork,it says write a program which recieve a character by keyboard and put it in binary file and then read the existance character in the file and send it to another binary file,the last character which is recieved must be *
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,635
    Rep Power
    1945
    What do you need help with? What code do you have now?
    Do you know how to read from keyboard?
    Do you know how to read/write from/to files?

    We will not solve you homework for you with a complete code solution.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    I have this but i got confused...!

    # include <stdio. h>
    # include <stdlib. h>
    void main (int argc , char *argv[ ])
    {
    FILE *in , out ;
    char ch ;
    clrscr() ;
    if (argc!=3)
    { printf ("you forget enter file name \n ") ;
    exit(1) ;
    }
    in = fopen (argv[1] , "wb") ;
    if (in = = NULL)
    { printf ("cannot open (first) output file\n ");
    exit (1) ;
    }
    do { ch = getchar() ;
    putc(ch , in) ;
    } while (ch ! = '.') ;
    fclose(in) ;
    in = fopen (argv[1] , "rb") ;
    if (in = = NULL)
    { printf ("cannot open input file \n") ;
    exit(1) ;
    }
    out = fopen (argv[2] , "wb") ;
    if (out = = NULL)
    { printf ("cannot open output file \n ") ;
    exit(1) ;
    }
    ch = getc(in) ;
    while (!feof (in))
    {
    putc(ch , out) ;
    ch = getc(in) ;
    }
    fclose(in) ;
    fclose(out) ;
    }
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    Originally Posted by physics7
    I have this but i got confused...!
    Confused about what? Why do you think that we are able to read your mind? Where did you ever get such a crazy idea? We are not mind-readers! You have to actually tell us what problems you're having!

    Your code is unreadable. Format it! Indent it properly and post it with code tags in order to preserve that indentation. If you cannot be bothered to post readable code, we cannot be bothered to try to read it. You have to make an effort to help us to help you.

    If you get error messages and/or warnings when you try to compile, then you need to tell us that that is what is happening. If your problem is that you don't understand one of those messages, then copy-and-paste it to show us. Remember, we cannot read your mind.

    Your assignment as you have posted it so far does not make much sense. We would need a better description of it. In Standard English this time, please.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,158
    Rep Power
    2222
    Formatted, your code would look like this:
    Code:
    # include <stdio. h>
    # include <stdlib. h>
    
    void main (int argc , char *argv[ ])
    {
        FILE *in , out ;
        char ch ;
    
        clrscr() ;
        if (argc!=3)
        { 
            printf ("you forget enter file name \n ") ;
            exit(1) ;
        }
    
        in = fopen (argv[1] , "wb") ;
        if (in = = NULL)
        { 
            printf ("cannot open (first) output file\n ");
            exit (1) ;
        }
        do 
        { 
            ch = getchar() ;
            putc(ch , in) ;
        } while (ch ! = '.') ;
        fclose(in) ;
    
        in = fopen (argv[1] , "rb") ;
        if (in = = NULL)
        { 
            printf ("cannot open input file \n") ;
            exit(1) ;
        }
        out = fopen (argv[2] , "wb") ;
        if (out = = NULL)
        { 
            printf ("cannot open output file \n ") ;
            exit(1) ;
        }
        ch = getc(in) ;
        while (!feof (in))
        {
            putc(ch , out) ;
            ch = getc(in) ;
        }
        fclose(in) ;
        fclose(out) ;
    }
    It is all frakked up with all kinds of extraneous spaces. Compiling with MinGW gcc, I get:
    C:TEST>gcc -Wall physics7.c
    physics7.c:1: stdio. h: No such file or directory
    physics7.c:2: stdlib. h: No such file or directory

    C:TEST>
    You should have gotten the same errors. There exist no files by those names (ie, with the ". h" extension), but rather the files stdio.h and stdlib.h do exist (ie, with the ".h" extension; notice the removal of that extraneous space). And frankly, I'm surprised that the compiler didn't complain about your "# include" instead of #include.

    You should have been able to correct those errors immediately to get a shipload of other errors and warnings:
    C:\otros\pc14402\dcw\PROJECTS\TEST>gcc -Wall physics7.c
    physics7.c:5: warning: return type of `main' is not `int'
    physics7.c: In function `main':
    physics7.c:9: warning: implicit declaration of function `clrscr'
    physics7.c:17: parse error before `='
    physics7.c:7: warning: unused variable `ch'
    physics7.c:6: warning: unused variable `out'
    physics7.c: At top level:
    physics7.c:22: parse error before `do'
    physics7.c:25: warning: type defaults to `int' in declaration of `putc'
    physics7.c:25: warning: parameter names (without types) in function declaration
    physics7.c:25: warning: data definition has no type or storage class
    physics7.c:26: parse error before `}'
    physics7.c:27: warning: type defaults to `int' in declaration of `fclose'
    physics7.c:27: warning: parameter names (without types) in function declaration
    physics7.c:27: warning: data definition has no type or storage class
    physics7.c:29: warning: type defaults to `int' in declaration of `in'
    physics7.c:29: `argv' undeclared here (not in a function)
    physics7.c:29: warning: initialization makes integer from pointer without a cast

    physics7.c:29: initializer element is not constant
    physics7.c:29: warning: data definition has no type or storage class
    physics7.c:30: parse error before `if'
    physics7.c:33: parse error before `1'
    physics7.c:33: warning: type defaults to `int' in declaration of `exit'
    physics7.c:33: conflicting types for `exit'
    c:/dev-cpp/bin/../lib/gcc-lib/mingw32/2.95.3-6/../../../../include/stdlib.h:325: previous declaration of `exit'
    physics7.c:33: warning: data definition has no type or storage class
    physics7.c:35: warning: type defaults to `int' in declaration of `out'
    physics7.c:35: `argv' undeclared here (not in a function)
    physics7.c:35: warning: initialization makes integer from pointer without a cast

    physics7.c:35: initializer element is not constant
    physics7.c:35: warning: data definition has no type or storage class
    physics7.c:36: parse error before `if'
    physics7.c:39: parse error before `1'
    physics7.c:39: warning: type defaults to `int' in declaration of `exit'
    physics7.c:39: warning: data definition has no type or storage class
    physics7.c:41: warning: type defaults to `int' in declaration of `ch'
    physics7.c:41: warning: passing arg 1 of `getc' makes pointer from integer without a cast
    physics7.c:41: initializer element is not constant
    physics7.c:41: warning: data definition has no type or storage class
    physics7.c:42: parse error before `while'
    physics7.c:45: warning: type defaults to `int' in declaration of `ch'
    physics7.c:45: redefinition of `ch'
    physics7.c:41: `ch' previously defined here
    physics7.c:45: warning: passing arg 1 of `getc' makes pointer from integer without a cast
    physics7.c:45: initializer element is not constant
    physics7.c:45: warning: data definition has no type or storage class
    physics7.c:46: parse error before `}'
    physics7.c:47: warning: type defaults to `int' in declaration of `fclose'
    physics7.c:47: warning: parameter names (without types) in function declaration
    physics7.c:47: warning: data definition has no type or storage class
    physics7.c:48: warning: type defaults to `int' in declaration of `fclose'
    physics7.c:48: warning: parameter names (without types) in function declaration
    physics7.c:48: warning: data definition has no type or storage class
    physics7.c:49: parse error before `}'

    C:TEST>
    A few things for you to do to clean that up:
    1. Before you can use a standard library function, you must #include its header file. Read the documentation for exit() in order to see what its header file is. The same goes for clrscr(), which needs the conio.h header file.

    2. The test for equality is with "==", not with "= =". Why did you stick those frakking stupid spaces in there like that? Correct it! And do the same for "! =" while you're at it.

    Each error message and warning gives you the line number where the error occurs. Use that information to go to that offending line, see what's wrong with it, and correct it. Do that for each and every error and warning.

    You cannot do anything else until your code compiles cleaning and your code cannot be said to compile cleanly until it compiles with no errors or warnings. Never ignore warnings! Warnings are much more important than errors.

    After you have made all the corrections, post your code, properly indented and using code tags. If it is not doing what you expect it to be doing, then tell us what you expect and what it's doing instead. Again, we are not mind-readers.

    PS
    A lot of the errors and warnings were because the compiler was massively confused by your errors described in #2 above.

    Look at this declaration:
    FILE *in , out ;

    The file I/O functions expect to work with a FILE pointer (ie, a FILE* ). Instead, you declare out as a FILE, not as a FILE pointer, and then try to use it as a FILE pointer which is wrong.

    Correct that.
    Last edited by dwise1_aol; May 8th, 2013 at 12:36 PM.

IMN logo majestic logo threadwatch logo seochat tools logo