niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C++
number of words in a line
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Fred J.
*nix forums addict


Joined: 30 Sep 2005
Posts: 96

PostPosted: Fri Jul 21, 2006 3:52 am    Post subject: number of words in a line Reply with quote

hi
I have a data file with equal number of columns for each row. I need
to get the number of rows and columns to allocate a matrix in gsl.

getline (in, line) and parse the line for the number of spaces then
add one to get the number of words then
number_of_rows = 1;
while getline (in, line) and number_of_rows++ does the number of rows

I am not sure how to go about the number of words task, is there a c++
approved regex for c++?
because
string word
while(in >> word) count++ will go through the whole file. I just want
one line.

thanks
Back to top
Fred J.
*nix forums addict


Joined: 30 Sep 2005
Posts: 96

PostPosted: Fri Jul 21, 2006 5:38 am    Post subject: Re: number of words in a line Reply with quote

Gary Wessle <phddas@yahoo.com> writes:

Quote:
hi
I have a data file with equal number of columns for each row. I need
to get the number of rows and columns to allocate a matrix in gsl.

getline (in, line) and parse the line for the number of spaces then
add one to get the number of words then
number_of_rows = 1;
while getline (in, line) and number_of_rows++ does the number of rows

I am not sure how to go about the number of words task, is there a c++
approved regex for c++?
because
string word
while(in >> word) count++ will go through the whole file. I just want
one line.

thanks

I tried
ifstream in(file_name.c_str());
string line = getline(in, line);
stringstream input( line.c_str() );

string word;
nCol = 0;
while(inpput >> word)
nCol++;

did not do it
Back to top
asoofi@purdue.edu
*nix forums beginner


Joined: 21 Jul 2006
Posts: 2

PostPosted: Fri Jul 21, 2006 1:34 pm    Post subject: Re: number of words in a line Reply with quote

Hi Gary
This is just a thought, but why don't you put a counter on every time a
space is found?

As long as you properly detect when to exit your loop, that should
suffice.

Gary Wessle wrote:
Quote:
Gary Wessle <phddas@yahoo.com> writes:

hi
I have a data file with equal number of columns for each row. I need
to get the number of rows and columns to allocate a matrix in gsl.

getline (in, line) and parse the line for the number of spaces then
add one to get the number of words then
number_of_rows = 1;
while getline (in, line) and number_of_rows++ does the number of rows

I am not sure how to go about the number of words task, is there a c++
approved regex for c++?
because
string word
while(in >> word) count++ will go through the whole file. I just want
one line.

thanks

I tried
ifstream in(file_name.c_str());
string line = getline(in, line);
stringstream input( line.c_str() );

string word;
nCol = 0;
while(inpput >> word)
nCol++;

did not do it
Back to top
asoofi@purdue.edu
*nix forums beginner


Joined: 21 Jul 2006
Posts: 2

PostPosted: Fri Jul 21, 2006 1:45 pm    Post subject: Re: number of words in a line Reply with quote

***************************

File *inFile;
char *buff;
char temp=0;
int i=0;
int ctr=0;

buff=(char *)(malloc(sizeof(1024));

inFile=fopen("input.txt","r");
fgets(buff, 1024, inFile);

while(!feof(inFile))
{
i=0;
while(temp!='\n')
{
temp=buff[i]; //Iterating through the buffered line, looking for a
space
if(temp==32) //32 is the ascii for a space
ctr++;
i++;
}
ctr++; //This accounts for the last word in the line, since it's not
//followed by a space

fgets(buff, 1024, inFile);
}

fclose(inFile);

***************************
I haven't tested that, but it should work, or should be very close to
working.
There may be minor areas.

The number of words will be in ctr at the fclose, at least
theoretically.

Hope that helps.

asoofi@purdue.edu wrote:
Quote:
Hi Gary
This is just a thought, but why don't you put a counter on every time a
space is found?

As long as you properly detect when to exit your loop, that should
suffice.

Gary Wessle wrote:
Gary Wessle <phddas@yahoo.com> writes:

hi
I have a data file with equal number of columns for each row. I need
to get the number of rows and columns to allocate a matrix in gsl.

getline (in, line) and parse the line for the number of spaces then
add one to get the number of words then
number_of_rows = 1;
while getline (in, line) and number_of_rows++ does the number of rows

I am not sure how to go about the number of words task, is there a c++
approved regex for c++?
because
string word
while(in >> word) count++ will go through the whole file. I just want
one line.

thanks

I tried
ifstream in(file_name.c_str());
string line = getline(in, line);
stringstream input( line.c_str() );

string word;
nCol = 0;
while(inpput >> word)
nCol++;

did not do it
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 8:27 pm | All times are GMT
navigation Forum index » Programming » C++
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts output number mm.omid@gmail.com C++ 1 Thu Jul 20, 2006 5:09 pm
No new posts Connection refused at /usr/lib/perl5/vendor_perl/5.8.6/Ma... DragonsLord Mandrake 0 Thu Jul 20, 2006 12:44 pm
No new posts to_char number format with optional decimal-point? Martin T. Oracle 3 Thu Jul 20, 2006 10:53 am
No new posts entires each on separate line kevin_m shell 2 Wed Jul 19, 2006 10:51 pm
No new posts Questions about "Line too long" Jonas Meurer Exim 9 Wed Jul 19, 2006 5:29 pm

Loans | Debt Consolidation | Web Advertising | Mortgages | Adverse Credit Remortgage
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1533s ][ Queries: 16 (0.0711s) ][ GZIP on - Debug on ]