-5

I have a file containing a line with a large number of floats. I want to seperate those numbers in lines but without loading this huge file into memory.

For example, I want to parse from the 3rd to 15th float and save it as a row in a 2-D matrix.

Any clue?

All the examples are related to read the whole line, but in my case this is out of memory

Skonitsa
  • 96
  • 1
  • 9
  • Please show your research effort till time. Please read [Ask] page first. – Sourav Ghosh Jan 15 '16 at 15:23
  • What is the actual format of the file? Is it a text file with the numbers separated by any special character? Is it a binary file where each number is stored in a standard format? – Some programmer dude Jan 15 '16 at 15:26
  • @Joachim It is a txt file. Float numbers are seperated by space. I have printed those numbers by another c++ program in float format. Thank you – Skonitsa Jan 15 '16 at 15:32
  • Simplest solution (but also very slow)? Read one number at a time, while keeping count of the numbers you have read. Then you can easily get the numbers you want. Fast for the early numbers, ***very*** slow for the later numbers. Unfortunately it's basically the only way unless all numbers are fixed-format (i.e. the numbers of characters for each number is fixed), because then you could easily `fseek` to the correct position to get the numbers you want. – Some programmer dude Jan 15 '16 at 15:39

1 Answers1

0

Yeah, typically the advice is to freadf() a line and then parse it, but if you have a really large block of data on one line then you can eat it piecemeal.

fscanf("%f ",&derp);
fscanf("%f ",&derp);
fscanf("%f ",&derp);
while(numberOfHerpLessThenFifteen)
{
  fscanf("%f ", &mrMatrixomatic[mahIndex++]);
}

Just realize that fscanf can choke and die in a horrible fire if the formatting isn't exactly correct.

Philip
  • 1,539
  • 14
  • 23