Thread: Advice requested, Code makes sense to me, not compiler

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Even though you're using different names (current, ip, whatever) the pointers are all pointing at the same file structure; so if you want to re-read the file from the beginning you'll have to rewind the file pointer.

    (And if it makes you feel better, it compiles on my system, but with some unused variable warnings and a comparison between signed and unsigned in getword_max.)

  2. #2
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    Made all changes noted above, and its still not decending into the while loop, so the pointers must be screwing up somewhere, but i dont understand why.

    Each time i create a new pointer, assign it to the original file pointer, so as far as i understand it, thats two separate pointers pointing to the same original file location.

    Then i take the copy, and mess around with it, so the original should still be the same?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by andrew.bolster View Post
    Made all changes noted above, and its still not decending into the while loop, so the pointers must be screwing up somewhere, but i dont understand why.

    Each time i create a new pointer, assign it to the original file pointer, so as far as i understand it, thats two separate pointers pointing to the same original file location.

    Then i take the copy, and mess around with it, so the original should still be the same?
    Compare the following code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void readfile(FILE *fileptr) {
        FILE *ip;
        char nextfive[6];
        ip = fileptr;
        fgets(nextfive, 6, ip);
        printf("%s\n", nextfive);
    }
    
    int main(void) {
        FILE *infile;
        infile = fopen("test.txt", "r");
        readfile(infile);
        readfile(infile);
        readfile(infile);
        fclose(infile);
        return 0;
    }
    (Note the copying of file pointers inside the function.) Tell me what you think it does, then run it and see what it does.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  2. my code is messed up says the compiler
    By 0927 in forum C++ Programming
    Replies: 11
    Last Post: 01-30-2002, 01:59 PM
  3. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  4. Replies: 3
    Last Post: 11-04-2001, 03:53 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM