0

I need to read one line after another of the data from .csv file. Line length may vary so I want to use the dynamic structure but I don't know how. There are only floats, no mixed types. I can use only standard libraries.

#include<iostream>
#include<fstream>
#include<cstring>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
int main(){
ifstream obraz;

obraz.open("POP_OCT.csv", ios::binary);

if(!obraz)
    cerr<<"couldn't open file"<<endl;
float length; //actually this one comes from the file

 while( !obraz.eof() ) // read lines
 {
    //read data from the verse to line[]
    for(int i=0; i<500; i++)
        {
        obraz>>line[i];
        obraz.seekg(+1, ios_base::cur);
        }
  obraz.close()
  return 0;
}

This is how I do it with normal table but it's size is defined as 500. If I read in another file length may be different so how to use dynamic table here?

Artur
  • 25
  • 5
  • You didn't mention what the problem is when you used your methods. – PaulMcKenzie Jan 04 '16 at 22:21
  • 2
    Consider using std::getline in combination with std::string to read line by line. And CSV files are not binary files, they are text files (although there is no difference between binary and text on UNIX/Linux). – vsoftco Jan 04 '16 at 22:23
  • I didn't know getline works with files read binary ( I mean use of ios::binary). – Artur Jan 04 '16 at 22:26
  • `float` for `length`? – LogicStuff Jan 04 '16 at 22:27
  • @Artur If your files is a comma-separated-value file, then open it at text, not as binary. Such files are simple text files. – vsoftco Jan 04 '16 at 22:28
  • @vsoftco It's a project that I have to do for my study and my teacher told me to use (ios::binary). – Artur Jan 04 '16 at 23:04
  • @LogicStuff It is float. I read all the data from header of the file in floats. Float has different size than int, so if float works I don't want to change it. – Artur Jan 04 '16 at 23:04

0 Answers0