473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Mar 2 '07 #1
4 1772
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

http://www.c-faq.com/
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."

--
Er*********@sun.com
Mar 2 '07 #2
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.

Mar 2 '07 #3
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>
Mar 2 '07 #4
"ericunfuk" <xu***********@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.
Mar 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
6
Atran
by: Atran | last post by:
Hello: In this article: You will learn to Write or Read A Text File. Let's Begin: First Create a new project (ConsoleApp or WinApp). And Make sure your program uses these namespaces: using...
2
by: Zach | last post by:
I compiled a game client and it crashed (segmentation fault) resulting in a core file being generated. I'm trying to find out exactly what caused it to crash. Any ideas how I can do this with gdb?...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.