- 1

How to read csv file cell by cell in c++?

I am still beginner, I would ask if there is a way to read a csv file with c++ cell by cell via for example two nested loop, the outer for loop iterates over the rows, and the inner for-loop iterates over the columns. e.g. int getValue(row,columm){ int value; //here should be the code for example return value;} Then one would call this, e.g. getValue(4,10) then it returns the value at the 4th row and at the 10th column My goal if I find such a function, I would create a 2D array, each 1D array of it, has two pair (row,column) then I enter my wanted paired and iterate over the function to obtain finally a list of row, column, its picked value row,column,its picked value And finally I can export it to csv file I searched in google, but it is advanced to me, I didnt found what I looked for, any help please to find such a function as described above?

3rd Jul 2018, 12:22 AM
Khaled
Khaled - avatar
9 Answers
+ 1
As promised (a little late), a very simple approach. Obviously you'll need to add error handling, etcetera, but this should give you a clue (documented): https://code.sololearn.com/cFgKSHmk8II1/?ref=app
4th Jul 2018, 12:26 PM
non
+ 2
nonzyro I don't know how to thank you, you are my champion ;) That fixed the problem, I am fond of programming and I hope that I make my knowledge better. Thank you so much :)
4th Jul 2018, 1:53 PM
Khaled
Khaled - avatar
+ 1
Bookmarking thread. Will post code tomorrow if nobody else has.
3rd Jul 2018, 1:02 AM
non
+ 1
You can save the entire csv file in a matrix of strings, and print the element at (i,j) from the matrix. Here is a sample code : https://code.sololearn.com/c8mbun90l7HR/?ref=app Here is something you can try here itself. https://code.sololearn.com/cxF7H9KwwAF3/?ref=app
3rd Jul 2018, 4:36 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
I have added an if which will prevent the runtime error. As for the warning, it can be ignored. I had, by default, disabled the option of extra ISO C++ warnings.
3rd Jul 2018, 10:26 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
nonzyro Thank you for your efforts, it looks like perfect, but it doesn't compile. This is the error: At line 74 column 24: no viable conversion from 'std::string' (aka 'basic_string<char>') to 'const char *'
4th Jul 2018, 1:24 PM
Khaled
Khaled - avatar
+ 1
Khaled That's strange, I tested it on my PC. It even compiles on the Playground. Are you using a GNU =>6 compiler? MS compilers make trouble sometimes and I've no Windows machine to test. Anyway, just change "this->fname" to "this->fname.c_str()" -- that should fix it.
4th Jul 2018, 1:43 PM
non
0
Kinshuk Vasisht thanks for your time. it doesn't work, it gives after compiling and running: terminate called after throwing an instance of 'std::out_of_range' _ what(): basic_string :substr also there are so many warnings for your code, I provide below them all: implicit conversion change signedness: 'int' to 'size_type' (aka 'unsigned int') Line Column 37 10 39 14 39 23 39 26 41 10 43 14 43 23 43 26 46 16 49 16 comparison of integers of different signs: 'int' and 'size_type' (aka 'unsigned int') Line Column 36 18 38 20 40 18 42 20 44 18 47 18
3rd Jul 2018, 8:51 AM
Khaled
Khaled - avatar
0
Ok thanks again, I ignored all the warnings, I put my csv location to open. https://code.sololearn.com/c4TlC2qie7Kh/?ref=app I run it, it remains writing running without any result. Could you give me a example inside the code e.g. to get the value at 2nd row and at the 3rd column? in order to see how it will work? muster.csv 11,12,13,14,15,16,17,18,19 21,22,23,24,25,26,27,28,29 .... etc Thanks in advance
3rd Jul 2018, 10:48 AM
Khaled
Khaled - avatar