473,406 Members | 2,698 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,406 software developers and data experts.

number of words in a line

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
Jul 21 '06 #1
6 3247
Gary Wessle <ph****@yahoo.comwrites:
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
Jul 21 '06 #2
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 <ph****@yahoo.comwrites:
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
Jul 21 '06 #3
***************************

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.

as****@purdue.edu wrote:
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 <ph****@yahoo.comwrites:
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
Jul 21 '06 #4
as****@purdue.edu writes:
***************************

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.
thanks, I will try to convert it to c++ since I am learning that now,
and test it, but I understand the idea.
my solution was

****************
void No_of_Rows_Cols() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );

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

nRows = 1;
while (getline(in, line))
nRows++;
}
****************
Jul 21 '06 #5
Theoretically, all of C can be used in C++.

If you throw that into a cpp file, it should still work.

But glad it helped! If it doesnt let me know and I'll throw it into a
compiler and see what I can do.
Gary Wessle wrote:
as****@purdue.edu writes:
***************************

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.

thanks, I will try to convert it to c++ since I am learning that now,
and test it, but I understand the idea.
my solution was

****************
void No_of_Rows_Cols() {
ifstream in(file_name.c_str());
string line;
getline(in, line);
stringstream input( line.c_str() );

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

nRows = 1;
while (getline(in, line))
nRows++;
}
****************
Jul 21 '06 #6

as****@purdue.edu wrote:
Theoretically, all of C can be used in C++.
This is absolutely false.

Jul 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: johkar | last post by:
How do I ensure a number has no more than 4 digits and 2 decimal places (adds .0 or .00 as necessary) onblur using reg expressions? Hints or solutions appreciated. John
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
0
by: Tigerlily | last post by:
Hello! I need to count the number of words in a string read in from an infile, in a function, but I don't know how to do this. This is what I have so far. //Tiffany Lynn Goodseit #include...
11
by: elrondrules | last post by:
Hi Am pretty new to python and hence this question.. I have file with an output of a process. I need to search this file one line at a time and my pattern is that I am looking for the lines...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
1
by: scubber | last post by:
hi, im new (as you're going to painfully aware of in a minute) to programming and was wondering if anyone could help with this little problem: i am trying to read a number of values which are...
4
by: Robba | last post by:
I got a little problem, i have to return the number of the array of the word that is filled in. First there is filled in a sentence, and have to be split. I think i splitted it but now i cant...
2
by: alwaali | last post by:
Hi I need help please This is my project and i need a help to solve it with you A page of text is to be read and analyzed to determine number of occurrences and locations of different words. The...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.