Thread: rewind()

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    11

    rewind()


    Code:
    void check_activity(FILE ** p_fp)
    {
    int diff = 0;
    int result = 0;
    int countA = 0, countB = 0;
    int ch;
    
       /* count the records from both files and check for the
          difference rules - currently set at 10% */
       rewind(p_fp[OUT_FILE]);
       while((ch = fgetc(p_fp[OUT_FILE])) != EOF)
          if(ch == '\n')
             countA++;
    
       while((ch = fgetc(p_fp[LAST_FILE])) != EOF)
          if(ch == '\n')
             countB++;
    
       ...
    OUT_FILE and LAST_FILE have already been opened successfully and OUT_FILE has been read, but LAST_FILE hasn't. When this function is called and I print out countA and countB, I get a 0 from countA, so I put the rewind statement in, but this has made no difference. As far as I know, p_fp[OUT_FILE] is a valid stream, is there another way I can put OUT_FILE back to the beginning so that I can count the lines ?


    tia,
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    either, a) OUTFILE is returning EOF b4 it encounters a new line, or b) there is no new line in outfile, therefor countA++ never gets executed.
    fseek(FILE, 0, SEEK_SET); will also go to beginning of the file.
    Last edited by infamous41md; September 23rd, 2003 at 09:15 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    11
    thanks infamous41md,

    I am going to have to add to this,


    I have included a printf - printf("count A, count B %d %d\n", countA, countB);

    to show the values and receiving displays like this

    count A, count B 0 5
    or
    count A, count B 0 10

    the two files currently being processed are small test files, one has 5 records and the other has 10, so if I swap them round, the results show that the text files are okay, however, it's worse than that, if I swap the two routines round, so that LAST_FILE is counted first and then OUT_FILE, the results are still exactly the same !!


    It seems as tho the first while fails no matter which file it is looking at, or which variable it is populating, and the second succeeds....

    Code:
    all these produce the same results
    
       while((ch = fgetc(p_fp[OUT_FILE])) != EOF)
          if(ch == '\n')
             countA++;
    
       while((ch = fgetc(p_fp[LAST_FILE])) != EOF)
          if(ch == '\n')
             countB++;
    
    or
    
       while((ch = fgetc(p_fp[LAST_FILE])) != EOF)
          if(ch == '\n')
             countB++;
    
       while((ch = fgetc(p_fp[OUT_FILE])) != EOF)
          if(ch == '\n')
             countA++;
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    How sure are you that the files are open? Being off by one is a famous error in C/C++.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    11
    aah


    thanks mitakeet

    :)

IMN logo majestic logo threadwatch logo seochat tools logo