We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,809 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reading/writing a CSV file into array

Hello,
I am trying to write a program where i have to read a CSV file(obtained by converting an excel file having only 1 column of integers) into an array and then to display the same array as another CSV file. I am using Turbo C++ 4.5.
I've tried, but since i am a beginner with this programming, i was unable to do it.
Any advice would be really helpful.

Thanks,
Anuroop

4
Contributors
5
Replies
1 Day
Discussion Span
4 Years Ago
Last Updated
7
Views
anuroop0707
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The first thing you need is to gather the right tools. In this case can you view the CSV file that you will be reading from by opening it in Notepad so you know that it is intact? Do you know how to open/create a file? Do you know the various variable types available to you in C++? Do you know about the various options available forreading into strings using getline()? If the file you are reading has numerical fields and you wish to do calculations using the numerical values do you know how to convert strings into numerical values? Do you know how to write native types (and user defined types if needed) to the screen (which will basically be the same way you write them to file)?

If you know all of that, then you can write the program without any major problems. If not, learn a given concept in a smaller program first and then try to use that experience to do what is needed with that concept in your program.

Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 9

I would suggest getting a compiler from this century, then reading up on ifstream.

MosaicFuneral
Posting Virtuoso
1,699 posts since Nov 2008
Reputation Points: 888
Solved Threads: 118
Skill Endorsements: 7

The first thing you need is to gather the right tools. In this case can you view the CSV file that you will be reading from by opening it in Notepad so you know that it is intact? Do you know how to open/create a file? Do you know the various variable types available to you in C++? Do you know about the various options available forreading into strings using getline()? If the file you are reading has numerical fields and you wish to do calculations using the numerical values do you know how to convert strings into numerical values? Do you know how to write native types (and user defined types if needed) to the screen (which will basically be the same way you write them to file)?

If you know all of that, then you can write the program without any major problems. If not, learn a given concept in a smaller program first and then try to use that experience to do what is needed with that concept in your program.

First of all, thanks for helping me out...
I have studied c++ for almost one year.I know the basics...But i have never dealt with the file handling part. Can u please give me an example for opening an csv file and entering each element into an array. My program needs to handle only integer values, and that of one column in an Excel file.
I searched almost everywhere i could, but the examples i got was showing a lot of errors..

anuroop0707
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

We don't do 'code to order' feel free to post your code and we'll help you work through your problems.

Or ask a much more specific question like 'How do I open a file'? if you can't find it on google (or equivalent)

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
Skill Endorsements: 0

Without knowing more about your skill set and specifics about what you are trying to, and in the spirit of not doing everythnig for you, I suspect I might end up with something looking like this as the guts of the prototcol used for reading of the file:

while(getline(fin, a[i++], ','));

Now I'd suggest using the reference material you have available/acculated over the last year to figure out what that line is doing and the importance of each part of it. (Feel free to point out it's weaknesses, too, so you can improve upon it!)

To use it. or something like it, in a program you will need to declare the individual parameters/variables used and initialize/associate them with appropriate values before you can get the line to work. Posting appropriate portions of your own code and asking for help when you get stuck like Murtan said is an excellent learning opportunity.

Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 9

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0674 seconds using 2.67MB