944,194 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1488
  • C++ RSS
Jun 8th, 2006
0

count

Expand Post »
In c++ how do I count the number of strings for the first line of a stream?
Similar Threads
Reputation Points: 21
Solved Threads: 0
Junior Poster in Training
gampalu is offline Offline
78 posts
since Mar 2006
Jun 8th, 2006
0

Re: count

When you read a string, increment a counter.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 8th, 2006
0

Re: count

Quote originally posted by Dave Sinkula ...
When you read a string, increment a counter.
Is it something like this?

C++ Syntax (Toggle Plain Text)
  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...
Reputation Points: 21
Solved Threads: 0
Junior Poster in Training
gampalu is offline Offline
78 posts
since Mar 2006
Jun 8th, 2006
0

Re: count

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 8th, 2006
0

Re: count

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...
Reputation Points: 21
Solved Threads: 0
Junior Poster in Training
gampalu is offline Offline
78 posts
since Mar 2006
Jun 8th, 2006
0

Re: count

Quote 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
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Jun 8th, 2006
0

Re: count

Quote 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":
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Complete shuffle of an array
Next Thread in C++ Forum Timeline: Help in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC