Hi Lerner
Thank you for the declaration, but the problem still with the definition of the dynamic array, because it still when running the code gives blank screen and sending error report everytime.

what you posted is incomplete -- it contains no code to read the csv file! All that program does is allocate the array -- nothing more.

I'm really supprised your program didn't crash on that last loop that calls atoi() because the array contains uninitialized strings that contain random data. atoi() expects a null-terminated ascii string, it searches through memory until it finds either the first non-digit character or null-terminator. But since the strings are uninitialized -- garbage in, garbage out.

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
  //don't start BIG!!!!!
  int row = 2;  //number of lines
  int col = 2;  //number of fields
  int len = 10;  //maximum number of char in any given field

  int r, c; 
  ifstream fin("myFile.csv");//open file
  if(!fin)                           //confirm file opened
    cout << "unable to open file;" << endl;
  else
  {
    char *** input;        //declare an array to store input using dynamic memory
    input = new char**[row];
    for(r = 0; r < row; ++r)
    {
       input[r] = new char*[col];
       for(c = 0; c < col; ++c)
          input[r][c] = new char[len];
    }
     
    //put something into the array   
    for(r = 0; r < row; ++r)
       for(c = 0; c < col; ++c)
           fin.getline(inputl[r][c], 9, ',');

   //do something with the information in the array        
   for(r = 0; r < row; ++r)
      for(c = 0; c < col; ++c)
         cout << input[r][c] << ' ';         
 }
 return 0;
}

//=============
myFile.csv
//=============
Billy,loves
Bobby,Sue

This demonstrates how you might read a csv file into a dynamically declared table of strings separating strings by field and line and then do something with it. Once you proved the concept, then expand it. For example, try to read in just the first couple fields of your file, then the first line, then the first 10 lines, etc. adjusting the appropriate variables to expand the scope of the project. You will also want to covert the appropriate fields to the appropriate numerical type and do the appropriate calculations, but dont' try to do that at the same time you are trying to read the file and build the table. Whether you do it with a small table, say 2 or 10 lines, or later in the developmental process is up to you, but don't do it simultaneously or you won't know what's screwing up!

Are you still having problems?

If you are let me know and perhaps I may be able to help. I Did C++ for six years until I changed to Delphi.

Are you still having problems?

If you are let me know and perhaps I may be able to help. I Did C++ for six years until I changed to Delphi.

Hi Andy
thank you for your post, I get the whole idea of using vector as ancient dragon helped me with, but Lerner way is still not working with the log file I attched in the first email.
Now I may ask you a new question I built a function to find the min and the max. but I am stacking in how could I pass the value of the 3rd and 6th column in the log file.
let me post the function.

int pointerToCompareFunction(void *first, void *second)
{
double Element1, Element2;
Element1 = *(double *) first;
Element2 = *(double *) second;
return (Element1 > Element2 ? 1 : 0);
}

void MinMax(void *pointerToArray,unsigned low, unsigned high, int (*pointerToCompareFunction)(void*,void*), int &min, int &max)
{
double *TheArray =(double *) pointerToArray;

if (low!=high) {
if (pointerToCompareFunction(&TheArray[low], &TheArray[max]))
max=low;
if (pointerToCompareFunction(&TheArray[min], &TheArray[low]))
min=low;
low++;
MinMax(pointerToArray, low, high, pointerToCompareFunction, min, max);
}
}

thanks in advance

Hi,
I wanted to read text field and atoi is giving a problem. I cannot use
matrix_points[row][col] = atoi(line);
which is good for integer/float values

If I write
matrix_points[row][col] = line;
it gives an error

Please suggest me what changes could I make

Here length is static. But generally we do not know the length of the field so it sounds a bit not practical.


#include<iostream>
#include<fstream>
using namespace std;

int main()
{
  //don't start BIG!!!!!
  int row = 2;  //number of lines
  int col = 2;  //number of fields
  int len = 10;  //maximum number of char in any given field

  int r, c; 
  ifstream fin("myFile.csv");//open file
  if(!fin)                           //confirm file opened
    cout << "unable to open file;" << endl;
  else
  {
    char *** input;        //declare an array to store input using dynamic memory
    input = new char**[row];
    for(r = 0; r < row; ++r)
    {
       input[r] = new char*[col];
       for(c = 0; c < col; ++c)
          input[r][c] = new char[len];
    }
     
    //put something into the array   
    for(r = 0; r < row; ++r)
       for(c = 0; c < col; ++c)
           fin.getline(inputl[r][c], 9, ',');

   //do something with the information in the array        
   for(r = 0; r < row; ++r)
      for(c = 0; c < col; ++c)
         cout << input[r][c] << ' ';         
 }
 return 0;
}

//=============
myFile.csv
//=============
Billy,loves
Bobby,Sue

This demonstrates how you might read a csv file into a dynamically declared table of strings separating strings by field and line and then do something with it. Once you proved the concept, then expand it. For example, try to read in just the first couple fields of your file, then the first line, then the first 10 lines, etc. adjusting the appropriate variables to expand the scope of the project. You will also want to covert the appropriate fields to the appropriate numerical type and do the appropriate calculations, but dont' try to do that at the same time you are trying to read the file and build the table. Whether you do it with a small table, say 2 or 10 lines, or later in the developmental process is up to you, but don't do it simultaneously or you won't know what's screwing up!

Hi,
I wanted to read text field and atoi is giving a problem. I cannot use
matrix_points[row][col] = atoi(line);
which is good for integer/float values

If I write
matrix_points[row][col] = line;
it gives an error

Please suggest me what changes could I make

how is matrix_points defined? what is line? you need to post more code than that. And you probably should have started a new thread instead of resurrecting this old one.

And the code Learner posted is partiallly wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.