473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 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 6367
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,426 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

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

Similar topics

16
by: Daniel Tonks | last post by:
First, please excuse the fact that I'm a complete MySQL newbie. My site used forum software that I wrote myself (in Perl) which, up until now, has used flat files. This worked fine, however...
5
by: hharry | last post by:
Hello All, I have an issue with dupliate Contact data. Here it is: I have a Contacts table; CREATE TABLE CONTACTS ( SSN int, fname varchar(40),
4
by: hharry | last post by:
Hello All, Does the BCP utility enable you to selectively import rows from a flat file to a table ? For example: The first column in my flat file contains a record type - 1, 2..7 I only...
5
by: Alfredo Barrientos | last post by:
Hi, Is it possible editing all rows in datagrid?, i mean all fields are enabled to edit?, and update all values in just one click? Thanks, Alfredo Barrientos
1
by: sunilkumar.r80 | last post by:
Hi I have a problem in reading a .xls file using java. I am using jakarta POI ApI for that it work fine for a 9000 rows contians 25 colums. But i need to read more than 25000 rows (in a single...
10
by: deppeler | last post by:
I am wanting to return the number of records (rows) in a flat file db. My script is a resource kit library and each item has a catalog number ($catnumber), instead of the user having to remember the...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
1
by: =?Utf-8?B?a0VW?= | last post by:
Hi, I'm trying to count the number of instances a "house" for example in B1 falls within a "rent" value of between >20 and <32 in D1 and need to change instances to "flat" and ranges, please see...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.