473,225 Members | 1,375 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,225 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 3235
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.