1,105,534 Community Members

Reading/writing a CSV file into array

Member Avatar
anuroop0707
Newbie Poster
2 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
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.

Member Avatar
MosaicFuneral
Nearly a Posting Virtuoso
1,285 posts since Nov 2008
Reputation Points: 812 [?]
Q&As Helped to Solve: 157 [?]
Skill Endorsements: 11 [?]
 
0
 

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

Member Avatar
anuroop0707
Newbie Poster
2 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

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..

Member Avatar
Murtan
Practically a Master Poster
670 posts since May 2008
Reputation Points: 317 [?]
Q&As Helped to Solve: 116 [?]
Skill Endorsements: 0 [?]
 
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)

Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article