.xls to array in c++

G

Gautam

Hi!
I am writing this program in visual c in which i need to take values
which are in an excel spreadsheet (or from .csv format (whichever is
easier!)) and put it into an array for doin some further computation.
I dont need to write any data into the excel sheet. I just need the
values frm there.
I have the .xls or .csv file ready with its name and location known
already.

Im not an advanced programmer or anything of the sort so please tell me
what header files to include as well!!!

Thanks in advance

Gautam
 
E

Ed

Gautam,

If it just has to be done once the easy way is to write it out from
Excel in
CSV format. Then use get() to read the file a character at a time,
putting them
into a char a[], looking for a comma. Once the comma is found,
manipulate the characters
in a[] as needed. E.g., use atoint() or something to convert to
integer.

Here is an example of reading characters.

#include <iostream.h>

#include <fstream.h>

int main()

{

ifstream inFile("test.txt", ios::in);

if(!inFile){

cerr << " File " << "test.txt" << " could not be opened for input." <<
endl;

}

ofstream outFile("out.txt", ios::blush:ut);

if(!outFile){

cerr << " File " << "out.txt" << " could not be opened for output." <<
endl;

}

char c;

while((c = inFile.get()) != EOF ){

outFile.put(c);

}

Here, the characters are just being echoed to outfile rather than
being stored in an array.

Hope this gives you some hints, but not the complete assignment!

Good luck,

Ed
 
G

Gautam

hmmm..thanks that was quite helpful!
But say if i need to import the data from an excel spreadsheet....?
If there is a way to import from excel, isnt it likely to be easier?
since i guess it would recognise the entire floating point number in a
cell instead of requiring to convert it with some command repeatedly
like atof() or smth?

Gautam
Gautam,

If it just has to be done once the easy way is to write it out from
Excel in
CSV format. Then use get() to read the file a character at a time,
putting them
into a char a[], looking for a comma. Once the comma is found,
manipulate the characters
in a[] as needed. E.g., use atoint() or something to convert to
integer.

Here is an example of reading characters.

#include <iostream.h>

#include <fstream.h>

int main()

{

ifstream inFile("test.txt", ios::in);

if(!inFile){

cerr << " File " << "test.txt" << " could not be opened for input." <<
endl;

}

ofstream outFile("out.txt", ios::blush:ut);

if(!outFile){

cerr << " File " << "out.txt" << " could not be opened for output." <<
endl;

}

char c;

while((c = inFile.get()) != EOF ){

outFile.put(c);

}

Here, the characters are just being echoed to outfile rather than
being stored in an array.

Hope this gives you some hints, but not the complete assignment!

Good luck,

Ed
Gautam said:
Hi!
I am writing this program in visual c in which i need to take values
which are in an excel spreadsheet (or from .csv format (whichever is
easier!)) and put it into an array for doin some further
computation.
I dont need to write any data into the excel sheet. I just need the
values frm there.
I have the .xls or .csv file ready with its name and location known
already.

Im not an advanced programmer or anything of the sort so please tell
me
what header files to include as well!!!

Thanks in advance

Gautam
 

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

Top