Can you read in excel /.csv files the same as .txt files?

When coding file streams, I know the basic format to read info from .txt files:

fInput >> data >> data; // etc...

However, I feel like that process would be different when reading from excel or .csv files because of the rows and columns. Is the process the same or is it different?
Last edited on
.csv means comma-separated variables so it is necessary to take into account the commas instead of spaces between values as your code snippet indicates.

If the file is derived from an Excel spreadsheet it will probably be necessary to extract the column labels on the first line separately.

data types are likely to vary from column to column.

Spreadsheets lend themselves to <vector> storage for subsequent processing.

None of this makes it any different from basic principles. All it requires is a good plan and knowledge of the structure of the data on the .csv file. A text reader will show you the structure and detail on the .csv file.

a specific reader for the file is the easy way.
a generic csv reader is a lot harder than you would think at first, as is detecting errors in the file (if humans were allowed to touch it).

the rows/columns thing isnt a big problem (just store it in c++ as a 2-d construct if you need to, or a 1-d container of some class that represents a row for many problems..?).

a specific reader for the file is the easy way.
Notepad or TextEdit is as complicated as that needs to be.



A truly generic csv reader has to handle quoted values and escape characters:
value 1,value 2,"Because it's quoted, this is one value",value 4
value 1,value\, 3,value 4
value 1,value 2 contains a newline\n
,value 3


You need to decide just how elaborate your csv parser needs to be.
When reading Excel files be aware that the old .xls files are in binary format, the newer .xlsx ones are zip files with xml files inside.
Topic archived. No new replies allowed.