By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Data Files

P: n/a
Hi,
Is it possible to make use of data files in C? If yes can someone
tell how or suggest me a site from where I can learn them.
Thanks in Advance for your patience.
Nov 14 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
be*********@yahoo.com (Xarky) wrote:
Is it possible to make use of data files in C?


No, it's not. Data files are a very advanced subject of information
theory, much too complicated for a simple language like C. In fact, even
C++, which we all know is a much stronger language than C, capable of
expressing much more advanced data types, is barely strong enough to
handle data files.
If you really need to use C, stick to simple kinds of files like binary
and text files, and keep all your data in memory or on the stack. If you
do need to use data files, my suggestion is to use a truly modern,
advanced programming language, such as ADA, ML, or Visual Basic.

Richard
Nov 14 '05 #2

P: n/a
Richard Bos wrote:
be*********@yahoo.com (Xarky) wrote:

Is it possible to make use of data files in C?

No, it's not. Data files are a very advanced subject of information
theory, much too complicated for a simple language like C. In fact, even
C++, which we all know is a much stronger language than C, capable of
expressing much more advanced data types, is barely strong enough to
handle data files.
If you really need to use C, stick to simple kinds of files like binary
and text files, and keep all your data in memory or on the stack. If you
do need to use data files, my suggestion is to use a truly modern,
advanced programming language, such as ADA, ML, or Visual Basic.

Richard


Either there is sarcasm in this reply, or we have a different
understanding of the term "data file".

My understanding is any "data", integers, text, whatever, that
is placed into a file makes the file a "data file". That file
could be as simple as a word list or as complex as a database.

According to my definition, any language that has file access
capabilities can make use of data files.

The OP should review the "Files" or "I/O" section of a good
C language reference manual.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #3

P: n/a
On Mon, 3 May 2004, Xarky wrote:
Hi,
Is it possible to make use of data files in C? If yes can someone
tell how or suggest me a site from where I can learn them.


The question is too vague to answer. I'd be inclined to say the answer is
YES. To the second part of your question, get a good text book on C
language or take a class at your local school. I have yet to see a good
online tutorial for someone with no programming experience.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #4

P: n/a
Thomas Matthews <Th****************************@sbcglobal.net> wrote:
Richard Bos wrote:
be*********@yahoo.com (Xarky) wrote:
Is it possible to make use of data files in C?


No, it's not. Data files are a very advanced subject of information
theory, much too complicated for a simple language like C.


Either there is sarcasm in this reply,


Surely not! Me, sarcastic? Inconceivable!

Richard
Nov 14 '05 #5

P: n/a

"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
Hi,
Is it possible to make use of data files in C? If yes can someone
tell how or suggest me a site from where I can learn them.
Thanks in Advance for your patience.


why wouldnt it be possible? What kind of data are you talking about. I use
C and store vertex/texture data of 3d models usinf fwrite, and load them
back using fread. No problems, nice and quick and fairly darned reliable.
Perhaps, you should re-word your question?
Allan
Nov 14 '05 #6

P: n/a
Xarky wrote:
Hi,
Is it possible to make use of data files in C?
I am afraid that is a very generic question. Having said that, there
is one thing you would like to know to begin with files in C - there are
text and binary data files and you distinguish between them while trying
to open the file.
Now, any standard C book should have something related to FILE
manipulation and the FILE data structure associated with this, that can
answer your questions.

If yes can someone
tell how or suggest me a site from where I can learn them.

There is only one site for all the fundamental questions -
http://www.google.com .

--
Karthik
Humans please 'removeme_' for my real email.
Nov 14 '05 #7

P: n/a
Richard Bos wrote:
be*********@yahoo.com (Xarky) wrote:
Is it possible to make use of data files in C?


No, it's not. Data files are a very advanced subject of information
theory, much too complicated for a simple language like C. In fact,
even C++, which we all know is a much stronger language than C,
capable of expressing much more advanced data types, is barely
strong enough to handle data files.

If you really need to use C, stick to simple kinds of files like
binary and text files, and keep all your data in memory or on the
stack. If you do need to use data files, my suggestion is to use a
truly modern, advanced programming language, such as ADA, ML, or
Visual Basic.


Very amusing. You might have borne in mind that you are replying
to some sort of newbie, who is quite likely to take you
seriously. A sarcasm wrapper, or even some smileys, would have
been appropriate.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #8

P: n/a

"Xarky" <be*********@yahoo.com> wrote in message

Is it possible to make use of data files in C? If yes can someone
tell how or suggest me a site from where I can learn them.

I suspect you are using the term "data files" to refer to some sort of
specific file format, such as a commercially available database might use.

In ANSI C you can open a file in text mode or in binary mode using the
fopen() function. You then have to know the format of the file to interpret
the data, which might be an ASCII text file, a JPEG image, a sound file, or
practically anything.

Now if someone has defined a special file format they will very often
provide C functions to help to parse it. For instance, if you are using MS
Windows, you can load a bitmap using a function that Microsoft has provided.
This will be one call rather than having to do all the work yourself. If
your "data file" is provided by a third party, then it is extremely likely
that they will have written some functions in C and will provide them to
programmers (assuming that they offer suport to developers at all).
Nov 14 '05 #9

P: n/a
Richard Bos <rl*@hoekstra-uitgeverij.nl> scribbled the following:
Thomas Matthews <Th****************************@sbcglobal.net> wrote:
Richard Bos wrote:
> be*********@yahoo.com (Xarky) wrote:
>> Is it possible to make use of data files in C?
>
> No, it's not. Data files are a very advanced subject of information
> theory, much too complicated for a simple language like C.
Either there is sarcasm in this reply,

Surely not! Me, sarcastic? Inconceivable!


"You keep using that word. I don't think it means what you think it
means."
Did I do this right? Anyway, I think that "The Holy Grail" is a much
funnier movie.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Show me a good mouser and I'll show you a cat with bad breath."
- Garfield
Nov 14 '05 #10

P: n/a
"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
Is it possible to make use of data files in C? If yes can someone
tell how or suggest me a site from where I can learn them.


Of course you can. Open a file, read or write some data, close the file.

Asking a more specific question will likely get you a more useful answer.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #11

P: n/a
be*********@yahoo.com (Xarky) wrote in message news:<bc*************************@posting.google.c om>...
Hi,
Is it possible to make use of data files in C? If yes can someone
tell how or suggest me a site from where I can learn them.
Thanks in Advance for your patience.


***
Hi,

First of all, sorry for all the inconvenience made due me to not
being clear in my query.

In my file I need to store the following (mainly for a small
database):
int position;
char *data;
int check;

Also I have to add the data mentioned at EOF or sometimes randomly
at any position in the file, or delete at any position. Can this be
done.

Can someone tell me how to do this in a simple way (if there is).

I hope it is more clear now the meaning of data files.
Thanks in Advance
Nov 14 '05 #12

P: n/a
CBFalconer <cb********@yahoo.com> wrote:
Richard Bos wrote:
If you really need to use C, stick to simple kinds of files like
binary and text files, and keep all your data in memory or on the
stack. If you do need to use data files, my suggestion is to use a
truly modern, advanced programming language, such as ADA, ML, or
Visual Basic.


Very amusing. You might have borne in mind that you are replying
to some sort of newbie, who is quite likely to take you
seriously.


Pah. Anyone who cannot spot the sarcasm in someone calling Visual Basic
and ML "modern, advanced programming languages", and those two in the
same breath, is not awake enough to be programming in C.

Richard
Nov 14 '05 #13

P: n/a
be*********@yahoo.com (Xarky) wrote:
In my file I need to store the following (mainly for a small
database):
int position;
char *data;
int check;

Also I have to add the data mentioned at EOF
Open the file in append mode. See the second parameter to fopen().

Choose whether you want:
- non-portable binary files (easy to read and write - just use fwrite()
and fread(), but be careful with that pointer - but only guaranteed to
be useable on the machine they were written on);
- portable text files (relatively easy to read and write using printf(),
fgets() and sscanf(), and if done right pointers are no problem, but
they can be relatively bulky and easy to tamper with);
- or whether you want to devise a portable binary format or choose a
portable binary format someone else invented, which can be somewhat
more complex to read and write (typically using functions which
arrange your struct members in the right byte order, and then call
fwrite(), and v.v. as well, of course), but can also be both portable
and compact.
Which of these options is right for you depends entirely on your
situation, and what the program is supposed to do. However, you might
get some inspiration at <http://www.wotsit.org/>.
or sometimes randomly at any position in the file, or delete at
any position. Can this be done.


Under most popular OSes, this cannot be done in any language. It is not
a problem of C itself. Inserting or removing bytes in the middle of a
file typically involves copying it. Most databases don't actually delete
records; they mark them with a flag saying "this record has been
deleted", and either re-use them or remove them all in one go when you
re-index the database.
If you'd been a good Usenetter and read the FAQ, you'd have known this,
btw: <http://www.eskimo.com/~scs/C-faq/q19.14.html>.

Richard
Nov 14 '05 #14

P: n/a
be*********@yahoo.com (Xarky) writes:
[...]
In my file I need to store the following (mainly for a small
database):
int position;
char *data;
int check;

Also I have to add the data mentioned at EOF or sometimes randomly
at any position in the file, or delete at any position. Can this be
done.


Dealing with the "char *data" could be complicated. Does "data" point
to a nul-terminated string, to a single character, to a (non-string)
character array of known size, or to something else? It almost
certainly doesn't make sense to write the pointer itself to a file.
If you want to write the stuff that it points to, you may have to have
variable-sized records, which means you'll have to have some way of
figuring out where each record starts and how big it is.

Inserting or deleting a record in the middle of the file is going to
be difficult. You would probably need to re-write everything after
the insertion or deletion point, or possibly the entire file. There
are data structures that let you impose a higher-level structure on a
file (so that the logical order of the records in the file doesn't
necessarily match the physical order), but that's probably a topic for
comp.programming rather than comp.lang.c.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #15

P: n/a
Xarky wrote:
.... snip ...
In my file I need to store the following (mainly for a small
database):
int position;
char *data;
int check;

Also I have to add the data mentioned at EOF or sometimes randomly
at any position in the file, or delete at any position. Can this
be done.


Modify your fundamental item to something like:

union ptrthing {
struct item *itemptr;
long int fileposn;
};

enum location {INMEM, ONDISK, DELETED};

struct item {
int position
enum location where;
union ptrthing *data;
int check;
};

and you have a chance of implementing something that can be moved
to and from disk storage.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.