function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    function

    I am trying to write a utility that takes as argument 1 a file name to
    write to and as argument 2 takes num as a number of zeros to write a
    argument 1. A file of zeros. This is how far I have got. Why would I want a
    file of zeros? To mount to a loopback device.

    #include <stdio.h>
    #include <stdlib.h>

    int
    main (int argc, char *argv[])
    {
    if (argc != 3)
    {
    puts ("usage error");
    exit (EXIT_FAILURE);
    }
    char a = '0';
    int b, num;
    FILE *fp;
    num = atoi (argv[2]);
    fp = fopen (argv[1], "wb");

    I am thinking that I would need a for loop involved here somewhere. I am
    not sure what to put in the body.

    Bill


  • Joachim Schmitz

    #2
    Re: function

    Bill Cunningham wrote:
    I am trying to write a utility that takes as argument 1 a file
    name to write to and as argument 2 takes num as a number of zeros to
    write a argument 1. A file of zeros. This is how far I have got. Why
    would I want a file of zeros? To mount to a loopback device.
    [OT]
    dd if=/dev/zero of=<your filecount=<your size>
    [/OT]
    #include <stdio.h>
    #include <stdlib.h>
    >
    int
    main (int argc, char *argv[])
    {
    if (argc != 3)
    {
    puts ("usage error");
    exit (EXIT_FAILURE);
    }
    char a = '0';
    char a = '\0';
    int b, num;
    FILE *fp;
    num = atoi (argv[2]);
    fp = fopen (argv[1], "wb");
    >
    I am thinking that I would need a for loop involved here
    somewhere. I am not sure what to put in the body.
    while (num--)
    fputc(a, fp);
    fclose(fp);
    return 0;
    }

    Still needs some error checking (num>=0, fp != NULL, fclose and fput worked)
    Bill
    Bye, Jojo


    Comment

    • Bill Cunningham

      #3
      Re: function


      "Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dewrote in message
      news:g1rpes$mp7 $1@online.de...
      [OT]
      dd if=/dev/zero of=<your filecount=<your size>
      [/OT]
      Ah. The easy way out :)
      >#include <stdio.h>
      >#include <stdlib.h>
      >>
      >int
      >main (int argc, char *argv[])
      >{
      > if (argc != 3)
      > {
      > puts ("usage error");
      > exit (EXIT_FAILURE);
      > }
      > char a = '0';
      char a = '\0';
      Why the termination string character? Do you mean to replace my line or
      are you adding one?
      > int b, num;
      > FILE *fp;
      > num = atoi (argv[2]);
      > fp = fopen (argv[1], "wb");
      >>
      > I am thinking that I would need a for loop involved here
      >somewhere. I am not sure what to put in the body.
      >
      while (num--)
      fputc(a, fp);
      fclose(fp);
      return 0;
      }
      >
      Still needs some error checking (num>=0, fp != NULL, fclose and fput
      worked)
      I can get that part. I think.
      >Bill
      Bye, Jojo
      >

      Comment

      • Bill Cunningham

        #4
        Re: function


        "Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dewrote in message
        news:g1rpes$mp7 $1@online.de...

        [snip]
        while (num--) fputc(a, fp);
        fclose(fp);
        return 0;
        }
        >
        Still needs some error checking (num>=0, fp != NULL, fclose and fput
        worked)
        >
        >Bill
        Bye, Jojo
        While(num--) What's that? Is that the same as what I was thinking here.

        int c;
        for(c=1;c<num;c ++)

        Bill


        Comment

        • Joachim Schmitz

          #5
          Re: function

          Bill Cunningham wrote:
          "Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dewrote in message
          news:g1rpes$mp7 $1@online.de...
          >[OT]
          >dd if=/dev/zero of=<your filecount=<your size>
          >[/OT]
          >
          Ah. The easy way out :)
          Jep 8-) Why reinventing the wheel...
          >>#include <stdio.h>
          >>#include <stdlib.h>
          >>>
          >>int
          >>main (int argc, char *argv[])
          >>{
          >> if (argc != 3)
          >> {
          >> puts ("usage error");
          >> exit (EXIT_FAILURE);
          >> }
          >> char a = '0';
          >char a = '\0';
          >
          Why the termination string character?
          It's not just that, it also is the nul character, a byte with all bits
          unset.
          Do you mean to replace my
          line or are you adding one?
          Replacing. Do you want zeros in the file or the ascii representation of the
          charcter 0? I believe the earlier.
          >> int b, num;
          >> FILE *fp;
          >> num = atoi (argv[2]);
          >> fp = fopen (argv[1], "wb");
          >>>
          >> I am thinking that I would need a for loop involved here
          >>somewhere. I am not sure what to put in the body.
          >>
          >while (num--)
          > fputc(a, fp);
          >fclose(fp);
          >return 0;
          >}
          >>
          >Still needs some error checking (num>=0, fp != NULL, fclose and fput
          >worked)
          >
          I can get that part. I think.
          Yep, left as an excercise to the reader 8-)
          >>Bill
          >Bye, Jojo

          Comment

          • Joachim Schmitz

            #6
            Re: function

            Bill Cunningham wrote:
            "Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dewrote in message
            news:g1rpes$mp7 $1@online.de...
            >
            [snip]
            >
            >while (num--) fputc(a, fp);
            >fclose(fp);
            >return 0;
            >}
            >>
            >Still needs some error checking (num>=0, fp != NULL, fclose and fput
            >worked)
            >>
            >>Bill
            >Bye, Jojo
            >
            While(num--) What's that? Is that the same as what I was thinking
            here.
            int c;
            for(c=1;c<num;c ++)
            for(c=1;c<=num; c++) /* if you want to loop num times */
            It's equivalent, just counting backwards and not needing yet another
            variable

            Bye, Jojo


            Comment

            • Bill Cunningham

              #7
              Re: function


              "Joachim Schmitz" <nospam.jojo@sc hmitz-digital.dewrote in message
              news:g1rq2v$nf9 $1@online.de...

              [snip]
              >>Still needs some error checking (num>=0, fp != NULL, fclose and fput
              >>worked)
              >>
              > I can get that part. I think.
              Yep, left as an excercise to the reader 8-)
              Now here's what I tried in error checking that gave me a warning. I
              must've messed up.

              if((fputc(a,fp) )!=NULL)
              if((fclose(fp)) !=NULL)

              comparing argument with a without a cast or something like that the compiler
              said. Without cast and comparing was mentioned by the compiler but it
              compiled and didn't work. So I tried this.

              if((fputc(a,fp) )==NULL)
              puts("fputc error");

              When I ran the binary fputc error was mentioned 10 times. My 2nd arg was 10.

              Bill


              Comment

              • CBFalconer

                #8
                Re: function

                Bill Cunningham wrote:
                >
                .... snip ...
                >
                While(num--) What's that? Is that the same as what I was thinking here.
                That means while the expression num-- is non-zero, do the
                following. The expression value is num before decrementing it.

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.


                ** Posted from http://www.teranews.com **

                Comment

                • CBFalconer

                  #9
                  Re: function

                  Bill Cunningham wrote:
                  >
                  .... snip ...
                  >
                  comparing argument with a without a cast or something like that
                  the compiler said. Without cast and comparing was mentioned by
                  the compiler but it compiled and didn't work. So I tried this.
                  >
                  if((fputc(a,fp) )==NULL)
                  puts("fputc error");
                  >
                  When I ran the binary fputc error was mentioned 10 times. My
                  2nd arg was 10.
                  Look up what a function does and returns before you use it. In
                  this case:

                  7.19.7.3 The fputc function

                  Synopsis
                  [#1]
                  #include <stdio.h>
                  int fputc(int c, FILE *stream);

                  Description

                  [#2] The fputc function writes the character specified by c
                  (converted to an unsigned char) to the output stream pointed
                  to by stream, at the position indicated by the associated
                  file position indicator for the stream (if defined), and
                  advances the indicator appropriately. If the file cannot
                  support positioning requests, or if the stream was opened
                  with append mode, the character is appended to the output
                  stream.

                  Returns

                  [#3] The fputc function returns the character written. If a
                  write error occurs, the error indicator for the stream is
                  set and fputc returns EOF.

                  Note the returned value.

                  --
                  [mail]: Chuck F (cbfalconer at maineline dot net)
                  [page]: <http://cbfalconer.home .att.net>
                  Try the download section.


                  ** Posted from http://www.teranews.com **

                  Comment

                  • santosh

                    #10
                    Re: function

                    Bill Cunningham wrote:

                    <snip>
                    Now here's what I tried in error checking that gave me a warning.
                    must've messed up.
                    >
                    if((fputc(a,fp) )!=NULL)
                    If you read your system's documentation for fputc you'll find that it
                    returns either the character passed (as an int) or EOF on error. NULL
                    is the wrong value to be comparing with. The above is better written
                    as:

                    if (fputc(ch, stream) == EOF) {
                    /* error */
                    }
                    if((fclose(fp)) !=NULL)
                    Again, fclose will return EOF on failure.

                    <snip>

                    Comment

                    • Bill Cunningham

                      #11
                      Re: function


                      "santosh" <santosh.k83@gm ail.comwrote in message
                      news:g1s4e8$igh $3@registered.m otzarella.org.. .
                      Bill Cunningham wrote:
                      >
                      <snip>
                      >
                      > Now here's what I tried in error checking that gave me a warning.
                      > must've messed up.
                      >>
                      >if((fputc(a,fp ))!=NULL)
                      >
                      If you read your system's documentation for fputc you'll find that it
                      returns either the character passed (as an int) or EOF on error. NULL
                      is the wrong value to be comparing with. The above is better written
                      as:
                      >
                      if (fputc(ch, stream) == EOF) {
                      /* error */
                      }
                      >
                      >if((fclose(fp) )!=NULL)
                      >
                      Again, fclose will return EOF on failure.
                      >
                      <snip>
                      I usually don't miss these things in error checking. I was suprised when
                      it didn't work but I guess I've had NULL on my mind. Earlier in this post I
                      wanted to write zeros to a file and found out the proper use was with the
                      string terminating character '\0' which is a byte with all the bits turned
                      off. Now a questions about the *putc functions. Do they take and write a
                      char or 8 bits, or an unsigned int? Fputc's first parameter is an int and I
                      declared an int and gave it the value of '\0'. If the char is taken from the
                      first int parameter why an int instead of a char? Is more information needed
                      than the char itself like padding or the like?

                      Bill


                      Comment

                      • Bill Cunningham

                        #12
                        Re: function


                        "CBFalconer " <cbfalconer@yah oo.comwrote in message
                        news:48417EB4.1 B3F9B56@yahoo.c om...

                        [snip]
                        That means while the expression num-- is non-zero, do the
                        following. The expression value is num before decrementing it.
                        >
                        conditionals. One of my weak points now. I can barely use while. I have
                        for and it down well. Switch is unusable and unreadable to me at this point.

                        Bill


                        Comment

                        • Richard

                          #13
                          Re: function

                          "Bill Cunningham" <nospam@nspam.c omwrites:
                          "CBFalconer " <cbfalconer@yah oo.comwrote in message
                          news:48417EB4.1 B3F9B56@yahoo.c om...
                          >
                          [snip]
                          >
                          >That means while the expression num-- is non-zero, do the
                          >following. The expression value is num before decrementing it.
                          >>
                          conditionals. One of my weak points now. I can barely use while. I have
                          for and it down well. Switch is unusable and unreadable to me at this point.
                          >
                          Bill
                          "while" is about 3x easier to understand than "for". See if you can see why.

                          Comment

                          • santosh

                            #14
                            Re: function

                            Bill Cunningham wrote:
                            >
                            "santosh" <santosh.k83@gm ail.comwrote in message
                            news:g1s4e8$igh $3@registered.m otzarella.org.. .
                            >Bill Cunningham wrote:
                            >>
                            ><snip>
                            >>
                            >> Now here's what I tried in error checking that gave me a
                            >> warning. must've messed up.
                            >>>
                            >>if((fputc(a,f p))!=NULL)
                            >>
                            >If you read your system's documentation for fputc you'll find that it
                            >returns either the character passed (as an int) or EOF on error. NULL
                            >is the wrong value to be comparing with. The above is better written
                            >as:
                            >>
                            > if (fputc(ch, stream) == EOF) {
                            > /* error */
                            > }
                            >>
                            >>if((fclose(fp ))!=NULL)
                            >>
                            >Again, fclose will return EOF on failure.
                            >>
                            ><snip>
                            >
                            I usually don't miss these things in error checking. I was
                            suprised when
                            it didn't work but I guess I've had NULL on my mind. Earlier in this
                            post I wanted to write zeros to a file and found out the proper use
                            was with the string terminating character '\0' which is a byte with
                            all the bits turned off.
                            Not necessarily. It's just a byte with value zero.
                            Now a questions about the *putc functions. Do
                            they take and write a char or 8 bits, or an unsigned int?
                            They take an int and a FILE * parameter and they attempt to write the
                            int parameter, cast to an unsigned char, to the stream indicated. A
                            char is commonly, but not necessarily eight bits. It's needs to be at
                            least eight bits, but could be more, as in the Cray or in most DSPs.
                            Fputc's
                            first parameter is an int and I declared an int and gave it the value
                            of '\0'. If the char is taken from the first int parameter why an int
                            instead of a char? Is more information needed than the char itself
                            like padding or the like?
                            This is I think just for compatibility with fgetc.

                            Comment

                            • Richard

                              #15
                              Re: function

                              santosh <santosh.k83@gm ail.comwrites:
                              Bill Cunningham wrote:
                              >
                              >>
                              >"santosh" <santosh.k83@gm ail.comwrote in message
                              >news:g1s4e8$ig h$3@registered. motzarella.org. ..
                              >>Bill Cunningham wrote:
                              >>>
                              >><snip>
                              >>>
                              >>> Now here's what I tried in error checking that gave me a
                              >>> warning. must've messed up.
                              >>>>
                              >>>if((fputc(a, fp))!=NULL)
                              >>>
                              >>If you read your system's documentation for fputc you'll find that it
                              >>returns either the character passed (as an int) or EOF on error. NULL
                              >>is the wrong value to be comparing with. The above is better written
                              >>as:
                              >>>
                              >> if (fputc(ch, stream) == EOF) {
                              >> /* error */
                              >> }
                              >>>
                              >>>if((fclose(f p))!=NULL)
                              >>>
                              >>Again, fclose will return EOF on failure.
                              >>>
                              >><snip>
                              >>
                              > I usually don't miss these things in error checking. I was
                              > suprised when
                              >it didn't work but I guess I've had NULL on my mind. Earlier in this
                              >post I wanted to write zeros to a file and found out the proper use
                              >was with the string terminating character '\0' which is a byte with
                              >all the bits turned off.
                              >
                              Not necessarily. It's just a byte with value zero.
                              Why on earth would you say that? It serves ZERO purpose whatsoever to
                              someone with such obvious difficulties as Bill.. All you will do is
                              confuse the guy more.
                              >
                              >Now a questions about the *putc functions. Do
                              >they take and write a char or 8 bits, or an unsigned int?
                              >
                              They take an int and a FILE * parameter and they attempt to write the
                              int parameter, cast to an unsigned char, to the stream indicated. A
                              char is commonly, but not necessarily eight bits. It's needs to be at
                              least eight bits, but could be more, as in the Cray or in most DSPs.
                              Ditto. Refer the man to the man page or whatever.

                              Comment

                              Working...