2

I need to know the number of columns from a text file with floats.

I've made like this to know the number of lines:

inFile.open(pathV); 

// checks if file opened
if(inFile.fail()) {
    cout << "error loading .txt file for reading" << endl; 
    return;
}
// Count the number of lines
int NUMlines = 0;
while(inFile.peek() != EOF){
    getline(inFile, dummyLine);
    NUMlines++;
}
inFile.close();
cout << NUMlines-3 << endl; // The file has 3 lines at the beginning that I don't read

A line of the .txt :

189.53  58.867  74.254  72.931  80.354

The number of values can vary from file to file but not on the same file.

Each value have a variable number of decimal places after the "." (dot)

The values can be separated by a space or a TAB.

Thank you

JMG
  • 125
  • 1
  • 4
  • 9

3 Answers3

3

Given a line you have read, called line this works:

std::string line("189.53  58.867  74.254  72.931  80.354");
std::istringstream iss(line);
int columns = 0;
do
{
    std::string sub;
    iss >> sub;
    if (sub.length())
        ++columns;
}
while(iss);

I don't like that this reads the whole line, and then reparses it, but it works.

There are various other ways of splitting strings e.g. boost's <boost/algorithm/string.hpp> See previous post here

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

You can read one line, then split it and count number of elements.

Or you can read one line, then iterate through it as an array and count number of space and \t characters.

Community
  • 1
  • 1
inkooboo
  • 2,904
  • 1
  • 19
  • 24
0

You can do this very easily if these three assumptions are true:

  1. dummyLine is defined so that you have access to it outside the while loop scope
  2. The last line of the file has the same tab/space delimited format (Cause that's what dummyLine will contain after the while loop)
  3. One and only one tab/space occurs between numbers on each line

If all these are true, then right after the while loop you'll just need to do this:

const int numCollums = std::count( dummyLine.begin(), dummyLine.end(), '\t' ) + std::count( dummyLine.begin(), dummyLine.end(), ' ' ) + 1;
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • I got an error: Error: Function count(dummyLine.begin(),dummyLine.end(),'\t') is not defined in current scope Can you specify count()? – JMG Dec 13 '13 at 13:37
  • 1
    At the top of the file this code is in `#include ` – Jonathan Mee Dec 13 '13 at 13:44
  • I'm using root, and I can't use all the C++ functionalities, and the same error appear... – JMG Dec 13 '13 at 14:00
  • When you say you're "using root" you mean that you're `using namespace std;` or do you mean something else? Clearly you have access to std functionality cause you're using a `std::string` and `std::fstream` so as long as you include algorithm you should also have access to `std::count`. – Jonathan Mee Dec 13 '13 at 15:00
  • Don't forget to tip your waitresses, and accept correct answers. – Jonathan Mee Dec 16 '13 at 18:42