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:- - while(!myFile.eof())
-
{
-
getline(myFile, str[i], '\n');
-
}
Thanks in advance
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.
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. -
int i=0
-
while(!myFile.eof())
-
{
-
getline(myFile, str[i], '\n');
-
vec.push_back(i);
-
}
-
Then print the elements on vector
Would appreciate guidance.
Thanks
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.
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.
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;
-
-
}
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
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?
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.
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |