Everyone talks about different languages having strengths and weaknesses? This is a really good example of where NOT to use C++.

The issue is, .XLS format is not a text file. The C++ code you're showing wants a text file. Think of it this way, try opening an excel file with notepad. See all the junk? That's what C++ sees.
The solution is asking the software already present on the computer to do the work. Rather than doing it directly, you have to talk to some Microsoft interface that can do the job for you. These interfaces include various database style bridges, like ODBC and OleDB, and the application object itself.
( For some reason, my Excel.Application code won't post? No worries,this is the main example. )
In vb script:
CODE
Sub TestOle(xlsFileName, rows, cols)
Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & xlsFileName _
& ";Extended Properties=""Excel 8.0;HDR=NO;"""
Set oRS = CreateObject("ADODB.Recordset")
oRS.Open "Select * From [Sheet1$]", oConn
'oRS.Open "Select * From [Sheet1$A1:D5]", oConn
col=1
For row = 1 to rows
line = ""
For col = 0 to cols - 1
line = line & oRS.Fields(col).Value
line = line & vbTab
Next
Wscript.Echo line
oRs.MoveNext
Next
oConn.Close
End Sub
This uses ADO as an interface. It has the big advantage of not launching a copy of Excel to do the work and should probably be preferred for data retrieval.
Now, why VBScript and not C++? Well, I'd never use C++ to do this kind of thing. Well, not unless I'm writing my own COM+ wrapper and had to. Even then, I'd use managed C++. Ok, to be honest, I'd use C#.
The library that was pointed to before uses this method. You could look at the code an worry out what it's doing. Basically, reproduce the last method, figure out how to hook your program into ADO and then just use the methods offered.
Hope this helps.
This post has been edited by baavgai: 7 Jan, 2008 - 10:53 AM