How to read data from an excel file by C?

Q

QQ

I am trying to read data from Excel files
I use the following codes, however, it didn't work

Can anyone tell me what the format excel files have, what I should do
to read it
from C?

Thanks a lot!

#include <stdio.h>
main()
{
FILE *fp;
char StrFile[200];
int Sid;
fp = fopen( "/home/Data.xls", "r");
fscanf(fp,"%s",&StrFile);
fclose(fp);
fprintf(stdout,"First Line is %s\n",StrFile);

}
 
E

Eric Sosman

QQ said:
I am trying to read data from Excel files
I use the following codes, however, it didn't work

Can anyone tell me what the format excel files have, what I should do
to read it
from C?

The format of an Excel file is not part of the C language.
You'll need to look elsewhere (perhaps http://www.wotsit.org/)
to learn about it.

That said, a few points about your C:
Thanks a lot!

#include <stdio.h>
main()

`int main(void)'.
{
FILE *fp;
char StrFile[200];
int Sid;
fp = fopen( "/home/Data.xls", "r");

If Excel files turn out to have a "binary" (as opposed
to "text") format, use "rb" instead of "r". Also, check
whether the fopen() call succeeded.
fscanf(fp,"%s",&StrFile);

First, lose the `&'. See Question 6.12 in the comp.lang.c
Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

Second, never use plain "%s" with scanf() or fscanf().
What happens if the input source supplies more than 199
characters?

Third, check the returned value to see whether the
function succeeded or failed.

Finally, I rather suspect Excel format will turn out
to be something ill-suited to the scanf() function family,
so the advice above may wind up producing no "image" in your
eventual program. Heed it anyhow, because you won't want
to make the same errors in other programs you'll write.
fclose(fp);
fprintf(stdout,"First Line is %s\n",StrFile);

.... and here's where you provide the return value from main().
See Question 11.2 in the FAQ.
 
Q

QQ

Second, never use plain "%s" with scanf() or fscanf().
What happens if the input source supplies more than 199
characters?
So you mean I should use scanf("%nc")?
 
E

Eric Sosman

QQ said:
So you mean I should use scanf("%nc")?

No; something like scanf("%199s", ...). However, as
I wrote before, I doubt that scanf() is the right function
to use for the job at hand.

I don't mean this as an insult, QQ, but from some of
the questions you're asking I suspect your mastery of C
may not yet be great enough for the task you have set
yourself. It seems to me you are in need of more study
than you appear to have had thus far; find a good textbook
or find a classroom setting or find whatever will help you
grow more adept, but find it before lunging ahead unprepared.
Usenet is a poor source for massive transfer of knowledge;
it's great at filling in the chinks, but not very good at
laying foundations.
 
K

Keith Thompson

QQ said:
I am trying to read data from Excel files
I use the following codes, however, it didn't work

What do you mean by "it didn't work"? What did you expect it to do,
and what did it actually do?
Can anyone tell me what the format excel files have, what I should do
to read it
from C?

Thanks a lot!

#include <stdio.h>
main()
{
FILE *fp;
char StrFile[200];
int Sid;
fp = fopen( "/home/Data.xls", "r");
fscanf(fp,"%s",&StrFile);
fclose(fp);
fprintf(stdout,"First Line is %s\n",StrFile);

}

The "%s" format doesn't read a line; it skips leading white space and
then reads a sequence of non-white-space characters. For example, if
"/home/Data.xls" is a text file, and its first line is "Hello, world",
the fscanf will give you "Hello," (assuming nothing else goes wrong).

On the other hand, Excel files are in a binary format, typically with
a lot of NUL ('\0') characters, so it's unlikely that fscanf() is
going to be the right tool for the job.

The details of the Excel format are off-topic here, but wotsit.org
probably has some good information.

It might be easier to use some other tool to translate the Excel file
to a text format, and then use a C program to read the text.

You should also consider whether you want to tackle this problem with
your current level of knowledge.
 
K

karel

( . . . )
Usenet is a poor source for massive transfer of knowledge;
it's great at filling in the chinks, but not very good at
laying foundations.

Unless one has the patience for MUCH lurking...
But thank you, Mr. Sosman, these words of yours
are the most illuminating I read over usenet since very long!

KA
 
P

Paul Mesken

I am trying to read data from Excel files
I use the following codes, however, it didn't work

Can anyone tell me what the format excel files have, what I should do
to read it
from C?

I take it you want to do more with the Excel file than merely loading
it. I don't think that using knowledge of the file format (as
available from wotsit.org, as pointed out already) is the prefered
way.

I'll just quote my answer from another answer I gave to another poster
who asked the same question :

<QUOTE>

This is all very off-topic, of course :)

If you want to play around with the file itself then follow Richard's
suggestion but if you just want to read from and write to Excel files
from within your application (to make reports, for example) or even
launch Excel in your application then use OLE automation, it's far
less work.

Here are some links :

http://support.microsoft.com/kb/196776
http://support.microsoft.com/default.aspx?scid=kb;en-us;216686

It's all very much a C++ thing. There's also a mysterious C API :

http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/office97/html/SF7D9.asp

Since I use Borland Builder, I use VCL functions like
GetActiveOleObject, OlePropertySet, etc. A search for these on Google
(adding "Excel" to the search) will return Builder (and Delphi) code
examples (that's how I learned to work with Excel from within my
applications :)

</QUOTE>
 
M

Mark McIntyre

I am trying to read data from Excel files
I use the following codes, however, it didn't work

Excel files are (at the moment*) a binary format. You can't just read
them. Before progressing onwards, learn how the OLE document format
works, and how Excel spreadsheet data is stored. This is quite a topic
of its own.
Can anyone tell me what the format excel files have, what I should do
to read it from C?

Microsoft do. Ask again in an MS programming newsgroup.

* supposedly the next Office incarnation will use XML. My money's on
this being a proprietary variant on the theme using lots of binary
objects, and unintelligible to others without a (licensed) translator.
Plus it'll be 200 times larger on disk....
 
M

Malcolm

QQ said:
Can anyone tell me what the format excel files have, what I should do
to read it from C?
It's a proprietary binary format which MS may alter without notice.
The C way of exchanging data portably is to use text files. Excel supports
this by allowing you to write a file as a CSV (comma-separated value) file.

If for some reason you need to use the binary, look it up on wotsit.
 
K

Keith Thompson

Malcolm said:
It's a proprietary binary format which MS may alter without notice.
The C way of exchanging data portably is to use text files. Excel supports
this by allowing you to write a file as a CSV (comma-separated value) file.

I'm not sure I'd call it "the C way". C can handle binary files as
well as any language.

Using text files is certainly a good way to exchange data portably
(though binary files have their advantages) -- but that's true in any
language.
 

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
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top