Data Files

X

Xarky

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.
 
R

Richard Bos

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
 
T

Thomas Matthews

Richard said:
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
 
D

Darrell Grainger

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.
 
A

Allan Bruce

Xarky said:
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
 
K

Karthik

Xarky said:
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 .
 
C

CBFalconer

Richard said:
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.
 
M

Malcolm

Xarky said:
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).
 
J

Joona I Palaste

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.
 
S

Stephen Sprunk

Xarky said:
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
 
X

Xarky

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
 
R

Richard Bos

CBFalconer said:
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
 
R

Richard Bos

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 said:
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
 
K

Keith Thompson

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.
 
C

CBFalconer

Xarky said:
.... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top