file reading...etc

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

    #1

    file reading...etc

    Hi, all

    I'm very new to C and have a few basic questions:
    I have an image file pic.tif, I want to read this image but only, say,
    2KB per time and possibily I'm going to store this 2KB in an Array or
    something then I will do something to this 2KB, then I will read
    another 2KB. My question is:

    Which function in C allows me to read a file by specifying the amount
    of data to readin each time?

    How many bytes are occupied by a "char" in C?


    Thanks for any help you could provide.

    Eric

  • Eric Sosman

    #2
    Re: file reading...etc

    ericunfuk wrote On 03/02/07 14:46,:
    Hi, all
    >
    I'm very new to C and have a few basic questions:
    I have an image file pic.tif, I want to read this image but only, say,
    2KB per time and possibily I'm going to store this 2KB in an Array or
    something then I will do something to this 2KB, then I will read
    another 2KB. My question is:
    >
    Which function in C allows me to read a file by specifying the amount
    of data to readin each time?
    fread(), or getc() in a loop. See also Question 12.37
    in the comp.lang.c Frequently Asked Questions (FAQ) list at


    How many bytes are occupied by a "char" in C?
    One, but the answer is tautological: C uses the word
    "byte" to mean "the amount of memory occupied by one `char',
    whatever that might be." (If you have a copy of "The Devil's
    Dictionary" at hand, look up the two words "magnet" and
    "magnetism. ")

    On most machines you're likely to encounter a `char'
    has eight bits, making C's "byte" the same as everyone
    else's "byte." There are, however, some exotic systems
    that use 32-bit `char', and perhaps other widths can also
    be found. On such systems, C's "byte" means something
    different from the common parlance "byte."

    --
    Eric.Sosman@sun .com

    Comment

    • santosh

      #3
      Re: file reading...etc

      ericunfuk wrote:
      Hi, all
      >
      I'm very new to C and have a few basic questions:
      I have an image file pic.tif, I want to read this image but only, say,
      2KB per time and possibily I'm going to store this 2KB in an Array or
      something then I will do something to this 2KB, then I will read
      another 2KB. My question is:
      >
      Which function in C allows me to read a file by specifying the amount
      of data to readin each time?
      >
      How many bytes are occupied by a "char" in C?
      >
      >
      Thanks for any help you could provide.
      In C, a char object is by definition one byte in size. That byte may
      or may not be of eight bits, (though that's the most common one).

      You can use either fgetc or fread in a loop to read in the required
      amount of bytes.

      Comment

      • CBFalconer

        #4
        Re: file reading...etc

        ericunfuk wrote:
        >
        .... snip ...
        >
        Which function in C allows me to read a file by specifying the
        amount of data to readin each time?
        fread.
        >
        How many bytes are occupied by a "char" in C?
        One, by definition.

        All this (and more) is available by reading your C book. Even if
        it is among the worst such as bullSchildt.

        --
        Chuck F (cbfalconer at maineline dot net)
        Available for consulting/temporary embedded and systems.
        <http://cbfalconer.home .att.net>


        Comment

        • SM Ryan

          #5
          Re: file reading...etc

          "ericunfuk" <xuwenduan2010@ gmail.comwrote:
          # Hi, all
          #
          # I'm very new to C and have a few basic questions:
          # I have an image file pic.tif, I want to read this image but only, say,
          # 2KB per time and possibily I'm going to store this 2KB in an Array or
          # something then I will do something to this 2KB, then I will read
          # another 2KB. My question is:
          #
          # Which function in C allows me to read a file by specifying the amount
          # of data to readin each time?

          fread and fwrite in stdio.h. You can read or write up to
          a specified maximum number of elements, and they return
          how many elements were actually read or written. Outside
          of stdio.h there may be additional functions like read
          and write. You may have to open the file in binary mode.

          # How many bytes are occupied by a "char" in C?

          A char is a byte; on any system you're likely to use, a
          byte is an octet, or 8 bits, so that char, byte, octet,
          and eight-bit element are all the same thing. A character
          is no longer always exactly a char, sometimes multiple
          bytes, but unless you're doing text processing, don't
          worry about that.

          --
          SM Ryan http://www.rawbw.com/~wyrmwif/
          I think that's kinda of personal; I don't think I should answer that.

          Comment

          Working...