number of words in a line

Discussion in 'C++' started by Gary Wessle, Jul 21, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    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
    Gary Wessle, Jul 21, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Gary Wessle Guest

    Gary Wessle <> 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
    Gary Wessle, Jul 21, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Guest

    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 <> 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
    , Jul 21, 2006
    #3
  4. Gary Wessle

    Guest

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

    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; //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.

    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 <> 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
    , Jul 21, 2006
    #4
  5. Gary Wessle

    Gary Wessle Guest

    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; //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++;
    }
    ****************
    Gary Wessle, Jul 21, 2006
    #5
  6. Gary Wessle

    Guest

    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:
    > 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; //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, 2006
    #6
  7. Gary Wessle

    Guest

    wrote:
    > Theoretically, all of C can be used in C++.


    This is absolutely false.
    , Jul 23, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Strøiman
    Replies:
    1
    Views:
    2,072
    Peter Strøiman
    Aug 23, 2005
  2. Hugo
    Replies:
    10
    Views:
    1,293
    Matt Humphrey
    Oct 18, 2004
  3. Richard Heathfield
    Replies:
    7
    Views:
    350
    Barry Schwarz
    Oct 5, 2003
  4. utab

    Words Words

    utab, Feb 16, 2006, in forum: C++
    Replies:
    6
    Views:
    415
    Daniel T.
    Feb 16, 2006
  5. BerlinBrown
    Replies:
    6
    Views:
    4,440
Loading...

Share This Page