Hi I was trying to count rows and columns in a tab delimited flat file.

Like here in example below i should be able to detect automatically the no of rows and columns.
So I should get 5 columns and 6 rows. Something like that.

one  two  three  four  five
one  two  three  four  five
one  two  three  four  five
one  two  three  four  five
one  two  three  four  five
one  two  three  four  five

I am reading my file like this:-

string str[0];
 int i=0;
while(!myFile.eof())
{
      getline(myFile, str[i], '\n');
      vec.push_back(i);
}

Is this the correct way ?

I need to get the rows and columns for any files for which I may not know these details.

Thanks

Too many problems in your code as I see :

string str[0];
 int i=0;
while(!myFile.eof())
{
      getline(myFile, str[i], '\n');
      vec.push_back(i);
}

line 1 : string str[0] will result in some unexpected value and not the string you want it to hold so change it to string str; but as you want to use it in getline function its better you take it as character array as char str[100];
line 3 : eof() function usage is bad and is to be avoided when ever possible,you'll find a lot of discussion in this community regarding this.Instead use good() if you want.
line 5 : Proper format of getline is istream& getline (char* s, streamsize n ); istream& getline (char* s, streamsize n, char delim ); If myfile is your file handle then call getline as:
myFile.getline(str,100,'\n');

General:
The successful number of getlines you can do with a large character array will give you the number of rows directly.
And once you have a line at your disposal then the total number of columns would be number of '\t' characters in that line plus one.

You can use this to simplify your work.

Thanks for suggestions. can you give an example block so that i can understand :)

Thanks

For the file you have shown :

//includes
//defines
int main(void)
{
//Variable declarations
//File opening in input mode handler myFile say
char str[256];
myFile.getline(str,256,'\n')
//Scan through str character by character and find the number of '\t' characters say c_cnt therefore c_cnt+1 gives you number of columns.
r_cnt=1;
while(getline(str,256,'\n')) r_cnt++;
//r_cnt gives you row count.

You wanted an example and here it is ... you have to develop this module to an actual code and remove the bugs if any during implementation so that you can learn coding it on your own.

Hi tried to do something like this but it doesn't work donno why :(
Can somebody tell me where am I doing wrong ?

Here is the code

#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<map>
using namespace std;

int main(int argc, char* argv[]) {

     std::string::size_type tab_pos( 0 );
     int count(0);

     ifstream myfile("textfile.txt");

     while(! myfile.eof()){    

     getline(myfile, str,'\n');


       while ( tab_pos!=std::string::npos )
       {
               tab_pos = str.find("\t", tab_pos );
               if ( tab_pos != std::string::npos )
               {
                 ++count;
                 tab_pos += 3; // start next search after this "and"
               }
       }


   }
        cout << count+1 << endl;
     return 0;

}

Thanks

This article has been dead for over six months. Start a new discussion instead.