This is what i try to do, i know how it should works but i have no idea how i should write code. Probebly i must replace X, X should be size of actual line to read to malloc it right. And also i must replace Y, this should be number of lines in csv file. Can anybody help me please ? :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
case 1:
{
char *line = (char*)malloc(X*sizeof(char));
ifstream number1;
number1.open("files/cars.csv");
if (number1.is_open())
{
for(int i = 1; i <= Y;i++)
{
number1.getline (line,X);
cout << line << '\n';
}
number1.close();
}
return 0;
break;
}
In c++ usually memory is allocated using the new operator.
However, in this case, I'm not sure that it is even necessary. You could simply define line with a fixed size of say 100, that would probably he adequate. However, it would be better to just use a std::string which would take care of the memory allocation for you.
I thought i saw a previous thread where the same question was already answered to your satisfaction, so I'm not really sure what it is you want to know here. But still, I'll offer two different solutions to this.
Second version. Instead of a 2D array, I've used a struct to hold the data for each car. A single dimension array of this object would do, but here I used a vector as it means there's no need to specify the size.