472,123 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,123 software developers and data experts.

Counting rows and colums in a flat file

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.
Expand|Select|Wrap|Line Numbers
  1. one  two  three  four  five
  2. one  two  three  four  five
  3. one  two  three  four  five
  4. one  two  three  four  five
  5. one  two  three  four  five
  6. one  two  three  four  five
  7.  
I am reading my file like this:-

Expand|Select|Wrap|Line Numbers
  1. while(!myFile.eof())
  2. {
  3.       getline(myFile, str[i], '\n');
  4. }
Thanks in advance
Oct 13 '09 #1
7 6203
Banfa
9,065 Expert Mod 8TB
You have described the problem domain but you have not actually asked a question with-in that domain.
Oct 13 '09 #2
Hi... Well I am new to C++ programming so sorry if I couldn't be objective in my question.
Well am not able to get how I can get the number of columns. I know one thing that getline can help me but how I am not able to figure out. Maybe something like this may help but not sure.

Expand|Select|Wrap|Line Numbers
  1. int i=0
  2. while(!myFile.eof())
  3. {
  4.       getline(myFile, str[i], '\n');
  5.       vec.push_back(i);
  6. }
  7.  
Then print the elements on vector

Would appreciate guidance.

Thanks
Oct 13 '09 #3
Banfa
9,065 Expert Mod 8TB
If the values are tab separated then the number of columns is the number of tabs in the line +1.

You ought to be able to use the methods

std::string::find
std::string::substr

to split your string into it's component sub-strings.
Oct 13 '09 #4
donbock
2,425 Expert 2GB
How tricky is your input file?

Sometimes field values are permitted to contain the delimiter (tab in your case) by either escaping the delimiter or enclosing the entire field value in double quotes. If you need to handle tricky input of this sort then you will need a more complicated program.

The easiest thing to do is declare a limitation that field values are not permitted to contain tabs.

What does it mean if you encounter two successive tabs? Your options include (a) treat consecutive tabs as if they were one tab; or (b) an empty field exists between the two tabs. Again, if nobody is pushing you one way or the other I would pick the one that's easiest to implement and call it a feature.
Oct 13 '09 #5
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
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. #include<sstream>
  5. #include<map>
  6. using namespace std;
  7.  
  8. int main(int argc, char* argv[]) {
  9.  
  10.      std::string::size_type tab_pos( 0 );
  11.      int count(0);
  12.  
  13.      ifstream myfile("textfile.txt");
  14.  
  15.      while(! myfile.eof()){    
  16.  
  17.          getline(myfile, str,'\n');
  18.  
  19.        while ( tab_pos!=std::string::npos )
  20.        {
  21.                tab_pos = str.find("\t", tab_pos );
  22.                if ( tab_pos != std::string::npos )
  23.                {
  24.                  ++count;
  25.                  tab_pos += 3; // start next search after this "and"
  26.                }
  27.        }
  28.  
  29.  
  30.    }
  31.         cout << count+1 << endl;
  32.      return 0;
  33.  
  34. }
Please do suggest modification :)

Also once I have got my string from the file how do I get the sepcified token. Like suppose my file has 10 fields and I want only 4 and 7 field number.

Thanks
Oct 15 '09 #6
Banfa
9,065 Expert Mod 8TB
Saying it doesn't work is less than helpful to us in diagnosing the problem.
How doesn't it work?
Did it actually compile?
What did you expect to happen?
What actually happened?
What was the input data?

Having said that this line look dubious

tab_pos += 3; // start next search after this "and"

You are looking for a single character, why don't you just increment tab_pos by 1?
Oct 15 '09 #7
Hi... sorry I had forgot to declare string str while posting this code :)

Yes this code complies and no error at runtime but the output is just 1 for count and rest is the same.

I wrote a separate program and it works perfectly as you had suggested using std::string::find but I am not just able to make it work while reading the file :( :( and need help in that.

Here is the code that works

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

int main ()
{
std::string str("Crochunter Banfa Help Bytes");
std::string::size_type tab_pos( 0 );
int count(0);
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 "\t"
}
}
cout << count+1 << endl;
return 0;
}
Howto make this logic work while reading is still something I am not able to get :(

The input File is a simple Tab delimited file.

Thanks a need your suggestions.
Oct 15 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by Daniel Tonks | last post: by
5 posts views Thread by hharry | last post: by
4 posts views Thread by hharry | last post: by
5 posts views Thread by Alfredo Barrientos | last post: by
15 posts views Thread by lxyone | last post: by
1 post views Thread by =?Utf-8?B?a0VW?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.