473,719 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Counting rows and colums in a flat file

21 New Member
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
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 6389
9,065 Recognized Expert Moderator Expert
You have described the problem domain but you have not actually asked a question with-in that domain.
Oct 13 '09 #2
21 New Member
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. }
Then print the elements on vector

Would appreciate guidance.

Oct 13 '09 #3
9,065 Recognized Expert Moderator Expert
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::fi nd
std::string::su bstr

to split your string into it's component sub-strings.
Oct 13 '09 #4
2,426 Recognized Expert Top Contributor
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
21 New Member
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;
  8. int main(int argc, char* argv[]) {
  10.      std::string::size_type tab_pos( 0 );
  11.      int count(0);
  13.      ifstream myfile("textfile.txt");
  15.      while(! myfile.eof()){    
  17.          getline(myfile, str,'\n');
  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.        }
  30.    }
  31.         cout << count+1 << endl;
  32.      return 0;
  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.

Oct 15 '09 #6
9,065 Recognized Expert Moderator Expert
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
21 New Member
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::fi nd 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::si ze_type tab_pos( 0 );
int count(0);
while ( tab_pos!=std::s tring::npos )
tab_pos = str.find("\t", tab_pos );
if ( tab_pos != std::string::np os )
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

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 lately I've been wanting to do more stuff with user accounts, and had been eying MySQL for over a year. Finally I've decided to start off small by converting the forum's account system to a MySQL database (and convert the rest later after I'm...
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),
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 need to import types 1, 2, & 3
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
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 sheet) It shows the message " java.lang.OutOfMemoryError: Java heap space" so can any one help me in this time Thanks
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 next catalog number to enter as they add a record, I want to be able to either show them the next number or have it as a value in a field on the form. I just need to know how to return the total number of records (rows?) in the db as a number...
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 show - text boxes, input boxes, buttons, hyperlinks ie the usual. The data is not obtained directly from a database.
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 example below Type RENT House 58.98 Flat 31.50 Flat 36.50
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.