473,898 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3273
Gary Wessle <ph****@yahoo.c omwrites:
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.c omwrites:
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(sizeo f(1024));

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

while(!feof(inF ile))
{
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.e du 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.c omwrites:
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.e du writes:
*************** ************

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

buff=(char *)(malloc(sizeo f(1024));

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

while(!feof(inF ile))
{
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.e du writes:
*************** ************

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

buff=(char *)(malloc(sizeo f(1024));

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

while(!feof(inF ile))
{
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.e du 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
6172
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
4446
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 "word", or "eelttr" for "letter". Build an array of all the keys, and sort it using a bubble sort. I have to write a modified version of bubble sort that maintains also the array of the primary words. For instance, if I had , our initial array of keys...
20
5163
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 to do it word by word. I am confused as to how to do this. Typically, I would think it would make sense to try and input the words into strings, but for this application I need to use character arrays and pointers. So what's the best way to go...
0
2388
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 <iostream> #include <fstream> #include <string> using namespace std; void diffwords(string &line); int main()
11
11837
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 that has the word 'event' and the word 'new'. Note that i need lines that has both the words only and not either one of them..
19
3626
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 starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
1
2079
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 separated by newlines, and then manipulate each number until a number =<0 is observed. is there a way i can use something like fgets() to read the line, then once the manipulation is over, print the result and for the process to be repeated with the...
4
1532
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 return the number of the index, i only get the number of the char. I hope that some1 can help me. Its written in C# console. Like this: A line of words. Then type one of the words like : line . Than i need to get returned 1....
2
3423
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 results of the analysis are to be stored in a suitable data structure. The main task in this project is to design a suitable ADT (call it WAnalysis) to store the results and enable the following operations to be performed as fast as possible:...
0
9993
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, 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...
0
9839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10946
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10481
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 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...
0
9658
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8034
isladogs
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...
0
6076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4705
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
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.