count

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

count

 
0
  #1
Jun 8th, 2006
In c++ how do I count the number of strings for the first line of a stream?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: count

 
0
  #2
Jun 8th, 2006
When you read a string, increment a counter.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: count

 
0
  #3
Jun 8th, 2006
Originally Posted by Dave Sinkula
When you read a string, increment a counter.
Is it something like this?

  1. int main(void)
  2. {
  3. static const char filename[] = "input.txt";
  4. FILE *file = fopen(filename, "r");
  5. if ( file != NULL )
  6. {
  7. char name[20];
  8. int cnt=0;
  9.  
  10. while (fscanf(file, &name) != "\n")
  11. {
  12. cnt++;
  13. }
  14.  
  15. cout << cnt;
  16.  
  17. fclose(file);
  18.  
  19. }
  20.  
  21. return 0;
  22. }

I'm having problems doing it...
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: count

 
0
  #4
Jun 8th, 2006
Maybe tinker with this:
http://www.daniweb.com/code/snippet444.html

[edit]Or maybe this instead.
#include <stdio.h>

int main(void)
{
   static const char filename[] = "file.txt"; /* the name of a file to open */
   FILE *file = fopen(filename, "r"); /* try to open the file */
   if ( file != NULL )
   {
      char line[BUFSIZ]; /* space to read a line into */
      while ( fgets(line, sizeof line, file) != NULL )
      {
         if ( line[0] != '#' && line[0] != '\n' )
         {
            const char *ptr = line;
            char field [ 32 ];
            int n, count = 0;
            fputs(line, stdout);
            while ( sscanf(ptr, "%31s%n", field, &n) == 1 )
            {
               printf("%d : \"%s\"\n", count, field);
               ptr += n + 1;
               ++count;
            }
         }
      }
   }
   return 0;
}
(Assuming this is still related to the configuration file stuff.)
Last edited by Dave Sinkula; Jun 8th, 2006 at 12:35 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: count

 
0
  #5
Jun 8th, 2006
Sorry to be boring but I want to count the number of strings only in the first line.
I mean a string each group of one or more caracters limited by a space...

Can you help me? I tried to use the code you indicated but without sucess...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: count

 
0
  #6
Jun 8th, 2006
Originally Posted by gampalu
Sorry to be boring but I want to count the number of strings only in the first line.
I mean a string each group of one or more caracters limited by a space...

Can you help me? I tried to use the code you indicated but without sucess...
Those are called words, not strings. A string is just a colection of one or more characters of any length, such as
  1. "Hello World This Is a String".

When learning to write C or C++ programs, it is important to also learn the correct terminology to avoid ambiguity.
Last edited by Ancient Dragon; Jun 8th, 2006 at 1:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: count

 
0
  #7
Jun 8th, 2006
Originally Posted by gampalu
Sorry to be boring but I want to count the number of strings only in the first line.
I mean a string each group of one or more caracters limited by a space...

Can you help me? I tried to use the code you indicated but without sucess...
With this as the "file.txt":
  1. #
  2. # The number of inputs for each epoch
  3. # Default value if omitted: 60
  4. #
  5. InputNumber 60
  6.  
  7.  
  8. #
  9. # The maximum epochs allowed in the network to converge
  10. # Default value if omitted: 300
  11. #
  12. MaxEpochs 300
  13.  
  14.  
  15. #
  16. # The number of neurons in the input layer
  17. # Default value if omitted: 19
  18. #
  19. InputNeurons 19
  20.  
  21.  
  22. #
  23. # The number of neurons in the hidden layer
  24. # Default value if omitted: 22
  25. #
  26. HiddenNeurons 22
  27.  
  28.  
  29. #
  30. # The number of neurons in the output layer
  31. # Default value if omitted: 1
  32. #
  33. OutputNeurons 1
  34.  
  35.  
  36. #
  37. # Default value if omitted: 0.01
  38. #
  39. LearningRate 0.01
  40.  
  41.  
  42. #
  43. # The dimension of each input
  44. # Default value if omitted: 48
  45. #
  46. InputDim 48
The code I posted ought to produce this.
  1. InputNumber 60
  2. 0 : "InputNumber"
  3. 1 : "60"
  4. MaxEpochs 300
  5. 0 : "MaxEpochs"
  6. 1 : "300"
  7. InputNeurons 19
  8. 0 : "InputNeurons"
  9. 1 : "19"
  10. HiddenNeurons 22
  11. 0 : "HiddenNeurons"
  12. 1 : "22"
  13. OutputNeurons 1
  14. 0 : "OutputNeurons"
  15. 1 : "1"
  16. LearningRate 0.01
  17. 0 : "LearningRate"
  18. 1 : "0.01"
  19. InputDim 48
  20. 0 : "InputDim"
  21. 1 : "48"
Last edited by Dave Sinkula; Jun 8th, 2006 at 1:34 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC